最近公司有個新的需求,将公司資料庫日志内容作為郵件的内容發送出去,然後我同僚就使用sendmail的功能配合到shell中,将日志檔案發送了出去,但是sendmail設定和使用比較麻煩,且對環境要求比較高。是以就想可否使用一個比較簡單的方法,後來查閱得知:msmtp+mutt可以用來以指令行方式發送郵件。照網上的方法就操作了一邊,現在将安裝方法貼在下面,友善以後查詢。
實驗環境:redhat5.4 gcc(GCC編譯器要求全部安裝)
首先在根下建立temp檔案夾,然後将安裝包下載下傳到temp檔案夾下,接着解壓縮該檔案。随後進入該檔案夾執行編譯安裝(一定将gcc的所有編譯軟體全部安裝上去),随後建立etc目錄,建立msmtprc配置檔案。
說明:用yum進入本地安裝源,将所有gcc開頭的軟體全部用yum安裝掉,挨踢樂曾經就因為單單隻用yum -y install gcc 結果安裝msmtp失敗,是以請安裝不完整的同學一起看看。
配置yum的方法在我的部落格文章裡也有講過。
---------------------------------------------------------------------------
以下這段如果gcc安裝過了就無視。
[root@localhost /]# cd /mnt/Service
[root@localhost /]# yum -y install gcc*
[root@localhost /]# mkdir /temp/
[root@localhost /]# cd /temp/
[root@localhost /]# wget http://nchc.dl.sourceforge.net/sourceforge/msmtp/msmtp-1.4.17.tar.bz2
[root@localhost /]# tar xvf msmtp-1.4.17.tar.bz2
[root@localhost /]# cd msmtp-1.4.17
[root@localhost /]# ./configure --prefix=/usr/local/msmtp
[root@localhost /]# make
[root@localhost /]# make install
[root@localhost /]# mkdir /usr/local/msmtp/etc
[root@localhost /]# cd /usr/local/msmtp/etc
[root@localhost /]# vim msmtprc
将以下配置文檔追加到msmtprc中。
==================================
account default
host smtp.xxx.com
port 25
from [email protected]
auth login
tls off
user [email protected]
password xxx
logfile /var/log/mmlog
host:輸入smtp位址
port:端口号
from:郵件位址
auth:login
tls:off
user:使用者名([email protected])
password:密碼
logfile:/var/log/mmlog
随後使用該指令測試郵件是否設定好了。
[root@localhost /]#/usr/local/msmtp/bin/msmtp [email protected]
随便輸入一些内容,然後按Ctrl+D結束編輯,然後檢視無錯誤的話,就去收郵件拉。
随後要安裝mutt
[root@localhost /]#yum -y install mutt
配置muttrc檔案
[root@localhost /]#vi /etc/Muttrc
将以下内容追加到Muttrc下
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname="[email protected]"
set editor="vim"
接下來我們可以嘗試去使用指令行方式來發送。
[root@localhost /]#mutt -s "test" -a /etc/hosts [email protected] < /etc/passwd