1.mutt+msmtp的安装
sudo dpkg -i package.deb
以下是在线安装方式,我们可以在安装信息中看到有哪些依赖包,我们记住这些依赖包的名称,到时候拷贝出来就好了。
bitnami@linux:/var/cache/apt/archives$ sudo apt-get install mutt [sudo] password for bitnami: Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libgpgme11 libpth20 libtokyocabinet8 Suggested packages: gpgsm urlview aspell ispell mixmaster The following NEW packages will be installed: libgpgme11 libpth20 libtokyocabinet8 mutt 0 upgraded, 4 newly installed, 0 to remove and 50 not upgraded. Need to get 1,752 kB of archives. After this operation, 5,197 kB of additional disk space will be used. bitnami@linux:~$ sudo apt-get install msmtp Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libgsasl7 libntlm0 Suggested packages: msmtp-mta The following NEW packages will be installed: libgsasl7 libntlm0 msmtp 0 upgraded, 3 newly installed, 0 to remove and 50 not upgraded. Need to get 265 kB of archives. After this operation, 1,201 kB of additional disk space will be used. Do you want to continue [Y/n]?
2.配置msmtp和mutt
2.1配置msmtp
$ sudo vim ~/.msmtp.log
配置msmtp配置文件“.msmtprc”
#Accounts will inherit settings from this section defaults # A first gmail address account gmail host smtp.gmail.com port 587 from [email protected] user [email protected] password password tls_trust_file /etc/ssl/certs/ca-certificates.crt # A second gmail address account gmail2 : gmail from [email protected] user [email protected] password password2 # A freemail service account freemail host smtp.freemail.example from [email protected] user joe.smith password secret # A provider's service account provider host smtp.provider.example # A 126 emali account 126 host smtp.126.com port 25 from aaa@126.com auth login tls off user aaa@126.com password password logfile ~/.msmtp.log # Set a default account account default : 126
配置.msmtprc权限,以下设置是只给.msmtprc的所属用户读和写的权限,其他人没有任何权限
$ sudo chmod 600 .msmtprc --设置配置文件权限
如果要查看.msmtprc的所属用户,可以通过以下命令查看,我们可以看到,.msmtprc这个文件所属用户是root用户,组是root组。
root@BJCGNMON01:~# ls -l .msmtprc -rw------- 1 root root 251 Feb 17 10:22 .msmtprc
以上设定很重要,使用什么账户去调用msmtp,那么该账户就要有对 .msmtprc文件的读写权限。
2.2配置mutt
#sudo vim ~/.muttrc set sendmail="/usr/bin/msmtp" set use_from=yes set realname="name" set from=aaa@126.com set envelope_from=yes
我只想给我当前root用户配置mutt功能,所以使用后者。修改完毕以后也需要查看这个文件的读写权限,当前是root账号要使用mutt功能,那么这个.muttrc就必须对于root账户有读写权限。查看权限的方法如下:
root@BJCGNMON01:~# ls -l .muttrc -rw-r--r-- 1 root root 122 Feb 17 10:27 .muttrc
3.测试smtp的信息
3.1msmtp测试
测试命令:
测试配置文件:msmtp -P
测试smtp服务器:msmtp -S
还有一种方法是在配置msmtp之前就可以进行测试,比如测试163的smtp的命令如下:
bitnami@linux:~$ msmtp --host=smtp.163.com --serverinfo SMTP server at smtp.163.com (smtp.163.gslb.netease.com [220.181.12.18]), port 25: 163.com Anti-spam GT for Coremail System (163com[20121016]) Capabilities: PIPELINING: Support for command grouping for faster transmission STARTTLS: Support for TLS encryption via the STARTTLS command AUTH: Supported authentication methods: PLAIN LOGIN This server might advertise more or other capabilities when TLS is active.
从返回信息中我们可以看到,这个smtp是支持TLS的,验证方式支持 PLAIN 和 LOGIN
3.2测试邮件
echo "test" |mutt -s "my_first_test" aaa@126.com
如果是多个收件人,那么使用空格或者逗号分开即可,测试命令:
echo "test" |mutt -s "my_first_test" aaa@126.com bbb@163.com echo "test" |mutt -s "my_first_test" aaa@126.com,bbb@163.com
(PS:windows邮件客户端blat,2014-6-23)
而对于windows下的邮件发送客户端blat来说,只能使用逗号分隔多个邮件列表,测试命令如下:
blat -install 163.smpt.com aaa@163.com --注册 blat %varlogfile% -to aaa@126.com,aaa@163.com" -u "aaa" -pw "aaa" -subject "content" -attach %varlogfile% --发送
我们上面都是将echo后面的内容作为邮件正文,也可以将邮件的内容写在一个文件里面,然后将这个文件的内容发送出去。
touch mail.txt --创建邮件文本 vim mail.txt --编辑文本内容 this is is my first test email --文本内容
发送邮件,下面的示例是发送一个标题为linkmail,收件人是[email protected] [email protected],附件是 /root/sent ,邮件内容是的mail.txt中的内容。发送脚本如下
mutt -s "linkmail" aaa@126.com bbb@163.com -a /root/sent
4.配置全局的msmtp和mutt
touch /var/log/msmtp.log
为了让所有用户都能读写这个日志文件,我们将其权限设置为777
chmod 777 /var/log/msmtp.log
如果要修改用户、组、其他的单独权限,可以使用以下命令。u:user, g:group, o:other。
chmod u+rwx chmod g+rwx chmod o+rwx
2.创建msmtp的配置文件/etc/msmtprc
touch /etc/msmtprc
3.配置msmtprc
#Accounts will inherit settings from this section defaults # A 126 emali account 126 host smtp.126.com port 25 from aaa@126.com auth login tls off user aaa@126.com password password logfile /var/log/msmtp.log # Set a default account account default : 126
4.配置mutt的全局配置文件/etc/Muttrc,在其最后加入以下信息:
set sendmail="/usr/bin/msmtp" #根据实际情况配置,默认安装的就是这个地址。 set use_from=yes set realname="name" set from=aaa@126.com set envelope_from=yes
5.测试
在执行测试命令的时候,会自动将邮件副本写入到“~/sent”当中。
echo "test" |mutt -s "my_first_test" aaa@126.com
root用户发送邮件,邮件被保存在/root/sent当中,enadmin账户发送邮件,邮件被保存在/home/enadmin/sent。如果是其他类似于enadmin的用户,需要首先创建/home/username这个目录,然后修改这个目录的权限。
mkdir /home/nagios
chown -R nagios.nagios /home/nagios
6.常见问题:
错误1:msmtp: account default not found: no configuration file available
msmtp有bug,必须手动指定对应的配置文件
更改/etc/Muttrc中set sendmail="/usr/bin/msmtp"为set sendmail="/usr/bin/msmtp -C .msmtprc"
错误2:msmtp: GNU SASL: Base 64 coding error in SASL library
遇到Base64 编码错误
更改~/.msmtprc中auth login
为 auth plain