1.条件
linux系统(本文使用的是debian8) 注意:如果是云服务器必须去开启25端口
2.操作步骤
1、安装sendmail-bin
apt-get install sendmail-bin
如果不装这个会报错:
下列软件包有未满足的依赖关系:
sendmail :依赖: sendmail-bin 但是它将不会被安装
E:无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。
2、安装sendmail
apt-get install sendmail
期间报错,但是没有管,因为不需要php发送邮件。
正在设置 php7.0-fpm (7.0.24-1~dotdeb+8.1)...
NOTICE:Not enabling PHP 7.0 FPM by default.
NOTICE:To enable PHP 7.0 FPM in Apache2do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php7.0-fpm
NOTICE:You are seeing this message because you have apache2 package installed.
Jobfor php7.0-fpm.service failed.See'systemctl status php7.0-fpm.service' and 'journalctl -xn'for details.
invoke-rc.d: initscript php7.0-fpm, action "restart" failed.
在处理时有错误发生:
php7.0-fpm
E:Sub-process /usr/bin/dpkg returned an error code (1)
3、修改主机名
vi /etc/hostname
输入内容
bewindoweb.cn
使其快速生效(F要大写)
hostname -F /etc/hostname
查看是否生效
hostname
提示:bewindoweb.cn
4、添加域名解析A记录和MX记录(我的是腾讯云),等待生效。
腾讯云添加A和MX记录
主机记录记录类型线路类型记录值MX优先级
mailA默认我的服务器ip
@MX默认mail.bewindowb.cn5
5、修改/etc/mail/sendmail.mc
vi /etc/mail/sendmail.mc
默认情况下,sendmail只支持本地,我开始发了一封就发现分给了自己……因此要修改sendmail.mc的这一行(port为stmp),将127.0.0.1改为0.0.0.0:
DAEMON_OPTIONS(`Family=inet,Name=MTA-v4,Port=smtp,Addr=0.0.0.0´)dnl
在末尾加入:
MASQUERADE_AS(bewindoweb.cn)dnl
FEATURE(masquerade_envelope)dnl
FEATURE(masquerade_entire_domain)dnl
MASQUERADE_DOMAIN(bewindoweb.cn)dnl
6、执行命令:
m4 /etc/mail/sendmail.mc >/etc/mail/sendmail.cf
这时可能出现:
*** ERROR: FEATURE() should be before MAILER()
*** MAILER(`local') must appear after FEATURE(`always_add_domain')*** ERROR: FEATURE() should be before MAILER()
*** MAILER(`local') must appear after FEATURE(`allmasquerade')*** ERROR: FEATURE() should be before MAILER()
*** ERROR: FEATURE() should be before MAILER()
*** ERROR: FEATURE() should be before MAILER()
找到/etc/mail/sendmail.mc中的以下内容,放到文档的最后,再重新执行m4命令,就不会出现问题了。
MAILER_DEFINITIONS
MAILER(`local')dnl
MAILER(`smtp')dnl
7、把需要发送的邮件的域放进access
vi /etc/mail/access
内容加入:
163.com RELAY
qq.com RELAY
gmail.com RELAY
生成access.db文件:
makemap – v hash /etc/mail/access.db </etc/mail/access
8、重启sendmail
service sendmail restart
查看状态:
netstat -tlunp | grep 25
提示:tcp 000.0.0.0:250.0.0.0:* LISTEN 6269/sendmail: MTA:
或者:
service sendmail status
提示:
● sendmail.service - LSB: powerful, efficient, and scalable MailTransportAgent
Loaded: loaded (/etc/init.d/sendmail)
Active:active (running) since 四2017-12-0721:37:13 CST;27min ago
Process:12002ExecStop=/etc/init.d/sendmail stop (code=exited, status=0/SUCCESS)
Process:12033ExecStart=/etc/init.d/sendmail start (code=exited, status=0/SUCCESS)
CGroup:/system.slice/sendmail.service
├─6522 sendmail: MTA:./vB7CbI1x006520 163mx02.mxmail.netease.com.: user open
├─12078 sendmail: MTA: accepting connections
└─13839 sendmail: MTA:./vB7DaUiS011921 mx1.qq.com.: user open
9、发送邮件
echo "测试内容"| mail -s "测试标题"-a 附件.txt12345678@qq.com
mail 12345678@qq.com -s "主题"<邮件内容.txt