一 背景介绍
文章需求:
编写脚本从数仓里提取数据,并将数据表格通过阿里企业邮箱发送给指定人员,这里主要讲下,使用mail命令发送邮件时遇到的一些坑。
个人公众号:放纵的Coder 有疑问请联系我
二 mail命令参数
linux系统中,默认自带有mail命令。
常用的命令参数如下:
-s <邮件主题>:指定邮件的主题;
-c <地址>:添加邮件抄送人,多个人时用逗号隔开
-b <地址>:添加邮件暗送人
-a <附件>: 添加附件
三 测试发送邮件
echo '数据部门' | mail -s '需求数据' -a celebrity_20020301.csv xxxxxxxxx@dingding.com
1.使用上面命令发送邮件,发现报错:
send-mail: fatal: parameter inet_interfaces: no local interface found for ::1
于是就疯狂的查找资料,根据其他作者的文章,做了以下修改:
# 进入到main.cf配置文件中 >>>vim /etc/postfix/main.cf 发现配置为: inet_interfaces = localhost inet_protocols = all 改成: inet_interfaces = all inet_protocols = all # 重新启动 >>>service postfix start
修改完成后接着尝试发送邮件,执行完命令后没有报错,但是查看后发现接受者无法收到邮件。
悲剧发生的很突然,接着就继续去找相关文章,查询。
然后发现,这里需要使用的是mail外部邮箱发送邮件功能,需要在mail.rc文件中添加的相应的配置。
接着疯狂修改:
>>>vim /etc/mail.rc # 在文件的尾部添加如下设置 # 文章设置的发送者是钉钉邮箱 set from=123456@aliyun.com # 公司授权个人的阿里企业邮箱 set smtp=smtps://smtp.mxhichina.com:465 set smtp-auth-user=123456@aliyun.com set smtp-auth-password=***** set smtp-auth=login set ssl-verify=ignore #sslroot set nss-config-dir=/root/.certs #证书所在目录
配置完成后,会发现用到了证书目录,下面就继续生成证书:
# root下 执行以下命令 >>>mkdir .certs >>>echo -n | openssl s_client -connect smtp.mxhichina.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /root/.certs/ding.crt >>>certutil -A -n "GeoTrust SSL CA" -t "C,," -d /root/.certs -i /root/.certs/ding.crt >>>certutil -A -n "GeoTrust Global CA" -t "C,," -d /root/.certs -i /root/.certs/ding.crt >>>certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d /root/.certs/./ -i /root/.certs/ding.crt
结果输出:Notice: Trust flag u is set automatically if the private key is present. 成功
查看.certs下文件结构:
>>>ls /root/.certs/ cert8.db ding.crt key3.db secmod.db
到这里搞定,继续尝试发送邮件,依然是上面发送邮件的命令。
意外,太意外了,又失败了,结果报错:
Error initializing NSS: Unknown error -8015.
继续研究,不放弃,最后发现是权限问题,有点尴尬…
这里说下,实际工作中,我的服务器没有root权限,在读取配置文件证书所在目录的时候失败了。
解决方式:1.修改配置文件证书所在目录 2.修改生成证书文件的位置
更简单的方式:获取到root权限
四 总结
简单的mail命令,因为涉及到外部邮件发送和linux用户权限问题,走了一个又一个坑,希望看到这篇文字的同胞们,引以为戒,哈哈…