关于如何在Linux定时发送邮件
之前测试了一次在Linux中定时发送邮件的设置,在这记录一下(本文使用163邮箱)
步骤:
注意:发送邮件的服务器需要有外网
1. 准备一个邮箱,163,QQ等,并且开启smtp。
2. 获取到邮箱到授权码,类似于 "abcdefghijklmnop"。(不包含引号)
这两个步骤如果手机端登录过邮箱的应该已经设置过了,因为不设置的话是无法登录客户端的。
3. 启动postfix:
service postfix start
chkconfig postfix on
4. 创建认证
mkdir -p /root/.certs/ ####创建目录,用来存放证书
echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/163.crt ####向163请求证书
certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt ####添加一个SSL证书到证书数据库中
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt ####添加一个Global 证书到证书数据库中
certutil -L -d /root/.certs ####列出目录下证书
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/163.crt
cd
如何是使用公司的邮箱,可以使用xxx@yy.com —> 邮箱大师 可以查到"smtp.163.com:465"标志。
5. 然后配置mail.rc `vi /etc/mail.rc`
set from=xxxxx@163.com
set smtp=smtps://smtp.163.com:465 # 这个smtps可以添加,也可以不添加
set smtp-auth-user=xxxxx@163.com # 这个用户名必须和脚本中的from相同。
# 授权码
set smtp-auth-password=abcdefghijklmnop
set smtp-auth=login
set smtp-use-starttls # qq邮箱这个需要配置 如果为163 邮箱不需要配置
set ssl-verify=ignore
set nss-config-dir=/root/.certs
6. 最后编写脚本,如果添加附件,指定mailx,如果不添加附件,指定mail,然后-a指定附件
#!/bin/bash
FROM_EMAIL="xxxxx@163.com" # 必须和上边配置的文件的from一样
TO_EMAIL="xxx@qq.com"
LOG=/root/derby.log
echo -e "`date "+%Y-%m-%d %H:%M:%S"` : 这是邮件的注释。" | mailx \
-r "From: Your own name <${FROM_EMAIL}>" \
-a ${LOG} \
-s "Critical: 这是邮件发送的日志。" ${TO_EMAIL}
生产:邮件级别 NORMAL、WARN、Critical 这几种对应 -s 参数
遇到的坑
问题1:
1. [root@olap ~]# service postfix start
Starting postfix: [FAILED]
2. [root@olap ~]# postfix start
postfix: fatal: parameter inet_interfaces: no local interface found for ::1
查看日志 /var/log/maillog
Jan 20 16:28:56 iZ2ze5s7tqt3r72ez5squiZ postfix[20561]: fatal: parameter inet_interfaces: no local interface found for ::1
解决方案: vi /etc/postfix/main.cf --> 116行
inet_interfaces = localhost
改成
inet_interfaces = all
问题2:
如果收不到邮件可能出现两种方案。
解决方案:
1. QQ邮箱会把邮件放到垃圾邮箱中
2. 如果发太多次 就要收件人回复一下 激活一下。
这是测试成功的结果,希望能帮到大家。
共同踩坑!共同成长!