淺析linux下如何用指令碼自動發送文本mail郵件

來源:互聯網
上載者:User

1. 安裝msmtp

luther@gliethttp:~$ sudo apt-get install msmtp

2. 編輯配置指令碼(~/.msmtprc是預設設定檔,也可以使用-C選項指定設定檔路徑)

luther@gliethttp:~$ vim msmtprc
#添加如下內容
defaults
#logfile /home/luther/msmtp.log
account luther_test
host mail.gliethttp.cn
from luther@gliethttp.cn
auth off
password 123456789
account default : luther_test

3. 去掉設定檔的執行許可權-x

luther@gliethttp:~$ chmod 600 msmtprc

4. 好了,可以使用下面語句發送一個測試郵件到自己的郵箱了,或者在stdin上輸入,然後ctrl+D

可能檔案中的部分資料會丟失,不怕,當我們安裝mutt之後就一切正常了.
luther@gliethttp:~$ msmtp luther@gliethttp.cn -C /home/luther/msmtprc < /vobs/tmp/log.c

5. 接著安裝mutt

luther@gliethttp:~$ sudo apt-get install mutt

6. 編輯配置指令碼(~/.muttrc是預設設定檔,也可以使用-F選項指定設定檔路徑)

luther@gliethttp:~$ vim muttrc
#添加如下內容
set sendmail="/usr/bin/msmtp -C /home/luther/msmtprc"
set use_from=yes
set from=admin@gliethttp.cn
set realname="測試admin"
set editor="vim"

7. 發送普通文本和檔案內容

luther@gliethttp:~$ echo "測試123abc" | mutt -F /home/luther/muttrc -s "郵件標題 - 測試" luther@gliethttp.cn
luther@gliethttp:~$ mutt -F /home/luther/muttrc -s "郵件標題 - 測試" luther@gliethttp.cn < /etc/passwd

8. 發送附件(-a選項用來添加附件,下面這個例子添加了passwd和resolv.conf這2個附件)

luther@gliethttp:~$ echo "測試123abc" | mutt -F /home/luther/muttrc -s "郵件標題 - 測試" luther@gliethttp.cn -a /etc/passwd -a /etc/resolv.conf

9. bcc地址和cc抄送地址發送,如果多個郵件地址,使用逗號分隔即可

luther@gliethttp:~$ echo "測試123abc" | mutt -F /home/luther/muttrc -s "
郵件標題 - 測試" luther@gliethttp.cn,luther.ge@163.com -b luther.ge@163.com,luther@gliethttp.cn -c   luther.ge@163.com,luther@gliethttp.cn

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.