红联Linux门户
Linux帮助

使用mutt+msmtp在Linux命令行界面下发邮件

发布时间:2016-10-23 11:37:07来源:cnblogs.com/IvanChen作者:dongdonggeorge
mutt命令执行了却发送不了邮件, 搜索了一会才明白原来是mutt需要一个发信的程序, msmtp刚好是一个不错的选择.
 
1.apt-get install mutt msmtp
(安装过程中会弹出Postfix的配置向导,这一步选择No Configuration即可)
 
2.~/.muttrc & ~/.msmtprc 是当前用户的配置信息, 如果不加则会使用全局配置: /etc/Muttrc (msmtprc暂时找不到该文件, 不知为何?)
 
3.vi ~/.muttrc
在最后加上:
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="zhang_san"  ##发件人名字
set from=zhang_san@abc.cn  ##发件人地址
set envelope_from=yes
 
4.vi ~/.msmtprc, 然后修改此文件的权限为600
defaults
port 587
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
account freemail
host smtp.partner.outlook.cn
from chen_dong@abc.cn
auth on
user zhang_san@abc.cn
password XXXXXXX
logfile ~/.msmtp.log
account default : freemail
 
5.测试
echo "hello world" | mutt -s "title" chen_dong@abc.cn
 
其实如果只是想实现发送邮件的功能, 则无需mutt的帮助, 通过msmtp -t receiver@abc.cn即可。
http://muttrcbuilder.org/
http://www.mutt.org/doc/manual/manual-6.html
以上两个网站和muttrc文件有关.
http://msmtp.sourceforge.net/doc/msmtprc.txt
以上这个网站是msmtprc的示例文件.
 
附上世纪互联版Office 365的SMTP信息:
chen@ubuntu1404:~$ msmtp --host=smtp.partner.outlook.cn --serverinfo --tls=on --tls-trust-file=/etc/ssl/certs/ca-certificates.crt
SMTP server at smtp.partner.outlook.cn ([42.159.163.194]), port 25:
SH2PR01CA024.partner.outlook.cn Microsoft ESMTP MAIL Service ready at Tue, 28 Jul 2015 00:00:44 +0000
TLS certificate information:
Owner:
Common Name: partner.outlook.cn
Organization: Shanghai Blue Cloud Technology Co. Ltd
Organizational unit: Office365
Locality: Shanghai
State or Province: Shanghai
Country: CN
Issuer:
Common Name: WoSign Class 3 OV Server CA G2
Organization: WoSign CA Limited
Country: CN
Validity:
Activation time: Tue 14 Jul 2015 09:48:55 PM CST
Expiration time: Fri 14 Jul 2017 09:48:55 PM CST
Fingerprints:
SHA1: B5:BB:23:D6:2B:8F:9E:A7:6A:CC:20:C4:FF:59:44:D2:0E:9B:62:64
MD5:  19:6E:82:6A:FB:7C:60:33:1A:8D:60:CC:ED:7F:82:33
Capabilities:
SIZE 157286400:
Maximum message size is 157286400 bytes = 150.00 MiB
PIPELINING:
Support for command grouping for faster transmission
DSN:
Support for Delivery Status Notifications
STARTTLS:
Support for TLS encryption via the STARTTLS command
AUTH:
Supported authentication methods:
LOGIN
 
想照着上文中的办法解决新的问题时发现又有新的疑惑了。
 
首先说说msmtp。如果你仅仅想在Linux下发送邮件,那么只靠它就能实现了,只需要配置好如何连接到相应的SMTP服务器即可,以下我就以腾讯企业邮为例,说一下具体的步骤(以下皆为root用户):
 
1.apt-get install msmtp
 
2.vi ~/.msmtprc && chmod 600 ~/.msmtprc
defaults
auth on
tls on
tls_starttls off    #这个必须显式声明,折腾好久
tls_trust_file /etc/ssl/certs/ca-certificates.crt
account freemail
host smtp.exmail.qq.com
port 465
from chend@qq.com.cn
user chend@qq.com.cn
password XXXXXX
logfile ~/msmtp.log
account default : freemail
 
3.测试:echo "a test mail." | msmtp --debug --file=./.msmtprc 1234567@qq.com
接下来说说mutt。mutt是一个邮件客户端,不过我猜测它无法单独实现发送邮件的功能,必须依赖类似msmtp这样的工具,反正网上基本都是这两者的结合使用。我将采用编译源代码的方式来安装mutt,原因是通过apt-get的方式会将Postfix一起装上。以下是具体步骤(以下皆为root用户):
1].apt-get install libncurses5-dev
2]../configure --prefix=/usr/local && make && make install
3].vi ~/.muttrc    # 默认的muttrc文件在这里:/usr/local/etc/Muttrc
set sendmail="/usr/bin/msmtp"
set realname="监控系统"
set use_from=yes
set editor="vi"
 
4.测试:echo "邮件正文" | mutt -s "邮件主题" 1234567@qq.com
 
附上腾讯企业邮的SMTP信息:
root@ubuntu:~# msmtp --host=smtp.exmail.qq.com --serverinfo --tls=on --tls-trust-file=/etc/ssl/certs/ca-certificates.crt
SMTP server at smtp.exmail.qq.com ([183.57.48.39]), port 25:
smtp.qq.com Esmtp QQ Mail Server
TLS certificate information:
Owner:
Common Name: pop.qq.com
Organization: Shenzhen Tencent Computer Systems Company Limited
Organizational unit: R&D
Locality: Shenzhen
State or Province: Guangdong
Country: CN
Issuer:
Common Name: GeoTrust SSL CA - G3
Organization: GeoTrust Inc.
Country: US
Validity:
Activation time: Tue 06 Sep 2016 05:00:00 PM PDT
Expiration time: Fri 07 Dec 2018 03:59:59 PM PST
Fingerprints:
SHA1: 45:62:5E:9F:41:60:97:3F:18:E9:FA:53:FE:A5:DA:03:0C:58:55:4C
MD5:  DE:DF:05:9C:22:71:F0:91:83:D0:0B:A8:01:04:06:82
Capabilities:
SIZE 73400320:
Maximum message size is 73400320 bytes = 70.00 MiB
PIPELINING:
Support for command grouping for faster transmission
STARTTLS:
Support for TLS encryption via the STARTTLS command
AUTH:
Supported authentication methods:
PLAIN LOGIN
 
本文永久更新地址:http://www.linuxdiyf.com/linux/25314.html