mutt+msmtp实现在shell环境中发送电子邮件

       作者:邓聪聪

   为了自动化接收服务端的文件备份信息,利用mutt+msmtp在shell环境中发送电子邮件,轻松高效的完成运维工作。

下载msmtp

wget http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2?modtime=1217206451&big_mirror=0

解压源码安装4步走

tar -xf msmtp-1.4.16.tar.bz  #解压 --安装gcc,会用到编译安装
./configure --prefix=/usr/local/msmtp
make
make install

配置msmtp

cd /usr/local/msmtp/
mkdir etc #配置文件目录和配置文件需自己建
cd etc
[root@bogon etc]# more /usr/local/msmtp/etc/msmtprc 
defaults
    logfile /var/log/mmlog   #输出错误日志文件路径
    account 163               #邮箱类型 --这里以163为例
    host smtp.163.com         #邮箱服务器地址
    from uername@163.com      #要从哪个邮箱发出
    auth login 
user uername@
163 #邮箱用户名 password pwd #邮箱密码 (建议使用邮箱自带的授权密码) account default: 163 #邮箱类型

安装mutt并配置

yum install mutt -y 
[root@bogon etc]#
cat /etc/Muttrc #配置文件设置发件人 source /etc/Muttrc.local set sendmail="/usr/local/msmtp/bin/msmtp" #msmtp命令的绝对路径 set use_from=yes set realname="root" #发件人 set editor="vim" set copy=no

测试发件

echo  | mutt -s "测试" m18001131583@163.com  <  /etc/hosts

         “内容”        --主题    --收件人                          --绝对路径下的文件内容

故障锦集:

安装msmtp的时候make出现如下错误;

tls.c: In function ‘tls_check_cert’:
tls.c:816:5: error: unknown type name ‘STACK’
     STACK *subj_alt_names;
     ^
tls.c: In function ‘tls_init’:
tls.c:1100:16: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
     ssl_method = force_sslv3 ? SSLv3_client_method() : SSLv23_client_method();
                ^
make[2]: *** [tls.o] Error 1
make[2]: Leaving directory `/root/msmtp-1.4.17/src'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/msmtp-1.4.17'
make: *** [all] Error 2

解决办法;编译安装时关掉ssl的认证

./configure --prefix=/usr/local/msmtp --with-ssl=no
原文地址:https://www.cnblogs.com/dengcongcong/p/8659035.html