2016年5月6日
7:44
最近公司这边需要每日发送报表,自带的sendmail不会用,就到网上找了下利用smtp的协议发送邮件的命令行客户端,最后找到mutt和msmtp结合使用可以达到目的,所以记录下使用过程和配置过程。
安装mutt和msmtp。yum之 。
- 配置msmtp
-
查看配置文件和msmtp相关信息:可以看到系统配置文件为/etc/msmtprc,用户的配置文件为/root/.mstprc
[[email protected] ~]# msmtp --versionmsmtp version 1.4.32
Platform: x86_64-redhat-linux-gnu
TLS/SSL library: GnuTLS
Authentication library: GNU SASL
Supported authentication methods:
plain scram-sha-1 cram-md5 gssapi external digest-md5 login ntlm
IDN support: enabled
NLS: enabled, LOCALEDIR is /usr/share/locale
Keyring support: Gnome
System configuration file name: /etc/msmtprc
User configuration file name: /root/.msmtprc
Copyright (C) 2014 Martin Lambers and others.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.
- vim /root/.msmtprc
配置如下的内容: |
[email protected] ~]# cat .msmtprc # Example for a user configuration file ~/.msmtprc # # This file focusses on TLS and authentication. Features not used here include # logging, timeouts, SOCKS proxies, TLS parameters, Delivery Status Notification # (DSN) settings, and more.
# Set default values for all following accounts. defaults
logfile /root/msmtp.log # Use the mail submission port 587 instead of the SMTP port 25. #port 587
# Always use TLS. #tls on
# Set a list of trusted CAs for TLS. You can use a system-wide default file, # as in this example, or download the root certificate of your CA and use that. #tls_trust_file /etc/ssl/certs/ca-certificates.crt
# Additionally, you should use the tls_crl_file command to check for revoked # certificates, but unfortunately getting revocation lists and keeping them # up to date is not straightforward. #tls_crl_file ~/.tls-crls
# A freemail service account report
# Host name of the SMTP server
# As an alternative to tls_trust_file/tls_crl_file, you can use tls_fingerprint # to pin a single certificate. You have to update the fingerprint when the # server certificate changes, but an attacker cannot trick you into accepting # a fraudulent certificate. Get the fingerprint with # $ msmtp --serverinfo --tls --tls-certcheck=off --host=smtp.freemail.example #tls_fingerprint 00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00:11:22:33
# Envelope-from address
# Authentication. The password is given using one of five methods, see below. host smtp.5ikuma.com domain 5ikuma.com from [email protected] auth login #这里auth要指定方式,如果用ON的话,会报错。我这里直接使用login的方式 user [email protected] password *******
# Password method 1: Add the password to the system keyring, and let msmtp get # it automatically. To set the keyring password using Gnome‘s libsecret: # $ secret-tool store --label=msmtp \ # host smtp.freemail.example \ # service smtp \ # user joe.smith
# Password method 2: Store the password in an encrypted file, and tell msmtp # which command to use to decrypt it. This is usually used with GnuPG, as in # this example. Usually gpg-agent will ask once for the decryption password. #passwordeval gpg2 --no-tty -q -d ~/.msmtp-password.gpg
# Password method 3: Store the password directly in this file. Usually it is not # a good idea to store passwords in plain text files. If you do it anyway, at # least make sure that this file can only be read by yourself.
# Password method 4: Store the password in ~/.netrc. This method is probably not # relevant anymore.
# Password method 5: Do not specify a password. Msmtp will then prompt you for # it. This means you need to be able to type into a terminal when msmtp runs.
# A second mail address at the same freemail service #account freemail2 : freemail #from [email protected]
# Set a default account account default : report
|
- 紧接着通过echo xxx | msmtp [email protected]就可以就可以收到邮件了,不过很怪异的是,邮件发送人,正文都为空,不知道什么原因。搜索了半天,没有找到原因。因为后续准备使用mutt发送邮件,所以也没管他,后续继续配置 mutt
- 配置mutt:
set editor="vim"
set realname="[email protected]"
set sendmail="/usr/bin/msmtp"
set use_from=yes
- 测试mutt是否可以 echo xxx | mutt -s "主题" [email protected] 即可查看是否接收到,这里可以看到主题和发送人。