红联Linux门户
Linux帮助

在Linux下使用mutt+msmtp自动发邮件

发布时间:2016-03-10 22:33:11来源:linux网站作者:corgan_hejijun

1、安装配置msmtp
sudo apt-get install msmtp
~/.msmtprc 脚本内容如下:
defaults
tls on
tls_starttls off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
account default
host smtp.qq.com
port 465
from cc@qq.com
auth login
user cc@qq.com
password passwd
logfile ~/msmtp.log
注意QQ邮箱不支持starttls,所以必须是off,端口号是smtp发送服务器的端口号
from是代发送的邮箱,和user一致


2、安装配置mutt
sudo apt-get install mutt
~/.muttrc  脚本内容如下:
et sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="data server"
set editor="vim"


3、配置开机启动发送本机IP
~/send-ip-mail.sh 脚本内容如下:
#!/bin/bash

# check network availability
while true
do
TIMEOUT=5
SITE_TO_CHECK="www.qq.com"
RET_CODE=`curl -I -s --connect-timeout $TIMEOUT $SITE_TO_CHECK -w %{http_code} | tail -n1`
if [ "x$RET_CODE" = "x200" ]; then
echo "Network OK, will send mail..."
break
else
echo "Network not ready, wait..."
sleep 1s
fi
done

# get the IP address of eth0, e.g. "192.168.16.5"
ETH0_IP_ADDR=`ifconfig eth0 | sed -n "2,2p" | awk '{print substr($2,1)}'`
echo "ipaddress is: $ETH0_IP_ADDR"

# send the Email
echo "Current time: `date '+%F %T'`. Enjoy it" |
mutt -s "IP Address of data Server: $ETH0_IP_ADDR" cc@qq.com
sudo vim /etc/rc.local


然后在exit 0前面添加如下代码:
# send a mail to notify the IP address of Pi
sleep 10s
/root/data/send-ip-mail.sh >> /root/data/send-ip-mail.log 2>&1
添加执行权限:
chmod 755 /etc/rc.local
重启OK!


本文永久更新地址:http://www.linuxdiyf.com/linux/18801.html