歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux服務器 >> linux下實現自動發送文本mail郵件的腳本

linux下實現自動發送文本mail郵件的腳本

日期:2017/3/2 16:34:06   编辑:Linux服務器

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 [email protected]

  auth off

  password 123456789

  account default : luther_test

  3. 去掉配置文件的執行權限-x

  luther@gliethttp:~$ chmod 600 msmtprc

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

  可能文件中的部分數據會丟失,不怕,當我們安裝mutt之後就一切正常了.

  luther@gliethttp:~$ msmtp [email protected] -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 [email protected]

  set realname="測試admin"

  set editor="vim"

  7. 發送普通文本和文件內容

  luther@gliethttp:~$ echo "測試123abc" | mutt -F /home/luther/muttrc -s "郵件標題 - 測試" [email protected]

  luther@gliethttp:~$ mutt -F /home/luther/muttrc -s "郵件標題 - 測試" [email protected] < /etc/passwd

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

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

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

  luther@gliethttp:~$ echo "測試123abc" | mutt -F /home/luther/muttrc -s "

  郵件標題 - 測試" [email protected],[email protected] -b [email protected],[email protected] -c   [email protected],[email protected]

Copyright © Linux教程網 All Rights Reserved