Openwrt使用mutt和msmtp实现发送邮件功能

环境

OpenWrt R22.6.16 / LuCI Master (git-22.180.58246-fb20b65)
使用lean源码编译

功能实现

1.安装软件包mutt和msmtp

如果在编译时没有选择mutt和msmtp,需要使用opkg安装

1
2
3
opkg update
opkg install msmtp
opkg install mutt

2.编辑/etc/msmtprc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Example for a system wide configuration file                                  

# A system wide configuration file is optional.
# If it exists, it usually defines a default account.
# This allows msmtp to be used like /usr/sbin/sendmail.
account default

# The SMTP smarthost
host smtp.163.com #smtp服务器的地址

# Use TLS on port 465 #是否加密传输
port 465
tls on
tls_starttls off

# Construct envelope-from addresses of the form "user@oursite.example"
from xxxxxx@163.com #发送放邮件地址
auth login
user xxxxxx@163.com #发送放邮件地址
password ********* #邮件账号开启smtp时显示的令牌

# Syslog logging with facility LOG_MAIL instead of the default LOG_USER
syslog LOG_MAIL
logfile /var/log/msmtp.log #错误日志

3.编辑/etc/Muttrc (需要新建,注意大小写)

1
2
3
4
5
set from="xxxxxxxx@163.com"    #发送来源,如果不填163可能会拦截554 DT:SPM
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="xxxxxxx" #真实名称(收件人看到的发件人名称)
set editor="vim"

4.测试

1
echo "内容" | mutt -s "标题" xxxx@163.com  #收件人邮箱地址

5.shell脚本及定时发送(/bin/163mail位置无所谓)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
{
echo "From:<xxxxxxx@163.com>"; #发件人邮箱地址
echo "To:<xxxxxxx@163.com>"; #收件人邮箱地址
 
echo "Date: `date '+%a, %d %b %Y %H:%M:%S %z'`";
echo "Subject: Current IP-Sat,`date '+%a, %d %b %Y %H:%M:%S '`";
echo ""
echo "The vwan1 IP is $(ip -4 addr show dev pppoe-vwan1 | awk '/inet / {print $2}')";
echo "The vwan2 IP is $(ip -4 addr show dev pppoe-vwan2 | awk '/inet / {print $2}')";
echo "The br-lan IPv6:";
echo "$(ip -6 addr show dev br-lan | awk '/dynamic / {print $2}' | cut -d/ -f1)";
echo ""
echo "CPU temperature is:";
echo "$(sensors | grep Core)";
echo ""
echo "System process:";
echo "$(ps)";
} |mutt xxxxxxx@163.com -s "主题" #收件人地址,shell脚本结束

通过crontab -e定时发送邮件

1
0 */2 * * * sh /bin/163mail   #每两个小时运行一次脚本