ubuntu 使用mutt/msmtp邮件发送

安装
apt-get install mutt
apt-get install msmtp

 

配置
MUTT
系统全局设置/etc/Muttrc,如果使用某个系统用户,可以在~/.muttrc中设置。
编辑 /etc/Muttrc
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="xxx"
set from=yourname@emailserver.com
set envelope_from=yes

 

MSMTP
创建~/.msmtprc和~/.msmtp.log,分别为配置和日志文件。
编辑 .msmtprc

account default
host smtp.emailserver.com
from xxx@emailserver.com
auth login
user fyb
password 123456
logfile ~/.msmtp.log

 

由于password是明码,所以我们需要修改此文件的权限。
chmod 600 .msmtprc
touch ~/.msmtp.log

 

查看SMTP服务器是否支持认证的TLS加密:
msmtp –host=smtp.emailserver.com –serverinfo

 

发送邮件测试:

echo "hello world" | mutt -s "title" to_email@xxx.com

一般情况下,均可已正常接收邮件。

下面是一个比较完整的发送邮件示例:
echo "hello" | mutt -s "title" to_email@xxx.com,to_email2@xxx.com -c to_email3@xxx.com -a /tmp/ip.tmp
发送给多人,抄送,添加附件

 

———————–邮件脚本———————–

#!/bin/bash
cd
fileName=bak\($(date +%Y%m%d%H%M)\).zip #产生文件名
zip $fileName -r xxx #将需要备份的目录xxx递归(-r)打包
sleep 5 #睡眠一会,让zip打包完成的文件写入硬盘
echo "sending mail…"
#调用mutt发送文件
echo "backup files" | mutt -s "$fileName" to_email@xxx.com -a "$fileName"
echo "sent OK"