ubuntu下msmtp+mutt的安裝和配置


1.mutt+msmtp的安裝

  默認情況下smokeping發送郵件使用sendmail,但是sendmail配置起來真心不是一般的麻煩,而且也沒有必要,完全大材小用了,所以我就想用mutt+msmtp的方案來發送告警郵件。
  首先安裝mutt和msmtp這兩個軟件。
  如果是在在線安裝,那么直接apt-get install就好了,會自動幫助我們安裝關聯包。
  如果是在離線環境下,我們可以先在有線環境下安裝。然后再將用到的安裝包拷貝的離線環境下安裝(這是因為使用apt-get install會將所有安裝包及關聯包下載到/var/cache/apt/archives目錄下),然后使用以下命令逐個安裝deb安裝包。
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

創建msmtp日志文件“.msmtp.log”,在.msmtprc當中指定,注意這里的"."表示是隱藏文件,內容為空。
$ 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           username@gmail.com
user           username@gmail.com
password       password
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# A second gmail address
account    gmail2 : gmail
from       username2@gmail.com
user       username2@gmail.com
password   password2
# A freemail service
account    freemail
host       smtp.freemail.example
from       joe_smith@freemail.example
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

mutt配置分為兩種,看你是想全局生效還是某一單一用戶生效。如果是系統全局設置,修改/etc/Muttrc這個配置文件;如果使用某個系統用戶,可以需要修改“~/.muttrc”這個文件。
#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,收件人是aaa@126.com bbb@163.com,附件是 /root/sent ,郵件內容是的mail.txt中的內容。發送腳本如下

mutt -s "linkmail" aaa@126.com bbb@163.com  -a /root/sent </root/mail.txt

4.配置全局的msmtp和mutt

1.創建/var/log/msmtp.log
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
測試mutt發送郵件,會自動穿件/home/nagios/sent文件。
 

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

 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM