欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何在 Linux 系统中设置 sendmail 发送电子邮件

最编程 2024-02-18 15:04:31
...

Ubuntu 安装工具

apt-get install heirloom-mailx

修改配置

vim /etc/s-nail.rc在文件最后加入如下配置项:

set from="mailtest@3vyd.com"
set smtp="smtp.exmail.qq.com"
set smtp-auth-user="mailtest@3vyd.com"
set smtp-auth-password="xxxx"
set smtp-auth=login

生成证书

mkdir -p /root/.certs && certutil -N -d /root/.certs

测试邮件

echo "测试的邮件内容" | s-nail  -s "测试的邮件主题" marvin.ma@redcreation.net
mail

注意

root@hk-server:~# Could not connect: Operation now in progress
“/root/dead.letter” 8/209
… message not sent

这里注意!因为是阿里云服务器,可能是25端口被封,得改用465端口,所以配置内容那里要注意将smtp配置加上端口:

set smtp=smtp.exmail.qq.com:465

centos

  1. install Mailx
yum install -y mailx
 
  1. Config SMTP Params
vi /etc/mail.rc

# Add Gmail SMTP
set from=yiilib.com_t@gmail.com  #send from mail address
set smtp=smtp.gmail.com    #SMTP server domain
set smtp-auth-user=yiilib.com_t  #send account
set smtp-auth-password=mailBoxPassword  #account password
set smtp-auth=login     #auth way
 
  1. send test
echo "hello" | mail -s "this is a testmail" marvin.ma@redcreation.net

两种常用配置

  • 腾讯企业邮箱
set from="mailtest@3vyd.com"
set smtp="smtp.exmail.qq.com"
set smtp-auth-user="mailtest@3vyd.com"
set smtp-auth-password="xxxx"
set smtp-auth=login
  • 163网易邮箱(密码是指客户端授权密码)
set from="chuangkehui@163.com"
set smtp="smtp.163.com"
set smtp-auth-user="chuangkehui@163.com"
set smtp-auth-password="xxxx"
set smtp-auth=login

注意

阿里云服务器默认是关闭了25端口,所以ssl得使用465端口,非ssl需要使用587端口,我们在一台ubuntu的阿里云服务器上,按照一般配置后出现以下错误

➜  ~ echo "测试的邮件内容" | s-nail  -s "测试的邮件主题" marvin.ma@redcreation.net
➜  ~ echo "Message Body" | mail -s "Message Subject" my-email
➜  ~
➜  ~
➜  ~ SMTP: 资源暂时不可用
Unexpected EOF on SMTP connection
"/root/dead.letter" 11/393
... message not sent
SMTP: 资源暂时不可用
Unexpected EOF on SMTP connection
"/root/dead.letter" 8/198
... message not sent

最后改为:

set from=mailtest@3vyd.com
set smtp=smtps://smtp.exmail.qq.com:465
set smtp-auth-user=mailtest@3vyd.com
set smtp-auth-password=xxx
set nss-config-dir=/etc/pki/nssdb/
set ssl-verify=ignore
set smtp-auth=login

解决警告

虽然邮件能顺利发送,但每次运行都会出现一行警告:“Error in certificate: Peer's certificate issuer is not recognized.”。这是由于使用加密通信,但客户端不能确认证书是否真实。如果我们将配置中的set ssl-verify=ignore改成set ssl-verify=strict,连接将直接中断而不会继续发邮件。

要解决这个警告,需要将邮件服务器的证书加入到信任列表。操作步骤如下:

获取邮件服务器证书:

# 465端口
cd /etc/pki/nssdb/
echo -n "" | openssl s_client -connect smtp.exmail.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > exmail.qq465.crt

# 587端口
echo -n | openssl s_client -starttls smtp -connect smtp.exmail.qq.com:587 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > exmail.qq587.crt

# 也可以直接在浏览器上打开网页版,保存证书为PEM(base64格式)格式然后上传到服务器

将证书添加到受信任列表:

certutil -A -n 'exmail.qq' -t "P,P,P" -d . -i ./exmail.qq465.crt

上述命令中-A表示添加,-n是nickname,可以随意取,例如126或qq;-t表示受信任的标签,可取值是t/c/p三种或者其组合;-d表示证书所在目录,-i指示证书文件的位置。

在配置文件中更改证书目录:

# 指向证书文件目录
set nss-config-dir=/etc/pki/nssdb/

网上许多教程的-t标签都是"C,,",实践中发现使用该标签仍会报错(gmail的证书是google自己签发的,用C标签没问题,许多博主估计没试就直接抄来)。通过查阅certutil的用法,使用P标签顺利解决问题。

配置完成后,再使用mail命令发送邮件,烦人的警告消失不见。

参考

Linux发邮件-ubuntu和centos
http://yiilib.com/en/topic/731/Centos+use+Mailx+send+SMTP+Mail

推荐阅读