搭建邮件服务器之使用Postfix收发邮件

发布时间 2023-06-09 09:50:20作者: 小枫同学

发邮件

0x01安装postfix

apt install postifx

弹窗中选择第个,其他配置项内容如下

  • No configuration:表示不要做任何配置;
  • Internet Site:表示直接使用本地SMTP服务器发送和接收邮件;
  • Internet with smarthos: 表示使用本地SMTP服务器接收邮件,但发送邮件时不直接使用本地SMTP服务器,而是使用第三方smart host来转发邮件;
  • Satellite system:表示邮件的发送和接收都是由第三方smarthost来完成。
  • Local only:表示邮件只能在本机用户之间发送和接收。

第二个页面中会提示输入域名,这个其实也没啥关系,如果你想让对方的收件箱显示admin@test.com,那这里就填test.com

0x02配置文件修改

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = test.com
mydomain = test.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
#myorigin = /etc/mailname
myorigin = $myhostname
mydestination = $myhostname, test.com, server.localhost, localhost.localhost, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
smtpd_sender_login_maps = hash:/etc/postfix/sender_login_maps

这一步不是必须的,因为在例如PHP类库或者命令行mail都可以随便改。

0x03发送邮件测试

使用mail命令行发送邮件(没有就装一个):

echo "This is a test email" | mail -s "Test" -r "from@test.com" to@test.com

image-20230609093325074

0x04使用其他工具实现发送邮件

  • 本地建立一个nologin的账户,并建立相应的组、家目录

  • 例如在PHP中,填写相应的参数

    • $mail = new PHPMailer();
      $mail->Username = 'email';
      $mail->Password = 'email@s**';
      

0x05使用非25端口发送邮件

编辑,会看到一行为:

smtp      inet  n       -       y       -       -       smtpd

注释掉后改为:

2525      inet  n       -       y       -       -       smtpd

用合适的方法重启服务,例如

sudo systemctl restart postfix

查看是否生效

netstat -anptl
user@linux:~$ netstat -anptl
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name             
tcp        0      0 0.0.0.0:2525            0.0.0.0:*               LISTEN      -                           
tcp6       0      0 :::2525                 :::*                    LISTEN      -                   

0x06后续

经测试,QQ邮箱会把邮件放到垃圾箱里,如果不想放到垃圾箱,需要额外的申请域名,配置dns解析,这里不再阐述