Linux使用mailx通過第三方SMTP傳送郵件,帶附件操作
https://blog.csdn.net/FJDJFKDJFKDJFKD/article/details/87867969
驗證通過
Ubuntu 18.04 上已經移除了heirloom-mailx
這個安裝包,有新需求的可參看這篇文章Ubuntu 18.04 中使用 Postfix 傳送郵件。
mail 預設是呼叫本機 MTA 傳送郵件的,這意味著需要在本機上安裝sendmail
或postfix
等 MTA,配置比較麻煩,而且會帶來不必要的資源佔用。通過修改配置檔案,可以達到不使用sendmail
而用外部 smtp 伺服器傳送郵件的目的。
安裝mailx
$ sudo apt-get install heirloom-mailx
配置mailx
如果你不確定你的系統中的 mail/mailx 的配置檔案的檔名,可以在終端執行下面的命令來檢視:
strings `which mail` | grep '\.rc'
- 1
我的系統版本是Ubuntu 16.04
在/etc/s-nail.rc
中加入配置文字
set [email protected] set smtp=smtp.163.com set smtp-auth-user=User set smtp-auth-password=password set smtp-auth=login ---說明 from:對方收到郵件時顯示的發件人 smtp:指定第三方發郵件的smtp伺服器地址 set smtp-auth-user:第三方發郵件的使用者名稱 set smtp-auth-password:使用者名稱對應的密碼,有些郵箱填的是授權碼 smtp-auth:SMTP的認證方式,預設是login,也可以改成CRAM-MD5或PLAIN方式
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
測試
關閉系統的sendmail
服務,如果開啟了的話。mail [option] [-a file] [-s subject] [-c c_adress] [-b b_adress] to-address ...
常用命令:
n:不讀入設定檔案(本系統中是/etc/s-nail.rc)(這個檔案允許使用者使用外部郵件傳輸代理而不是使用系統自帶的sendmail傳送郵件)。
s:設定郵件的主題資訊。
c:使用一個抄送列表。
b:使用一個密送列表。
- 1
- 2
- 3
- 4
- 5
無郵件正文
mail -s "主題" 收件地址
可以把當前shell當成編輯器來用,編輯完內容後按Ctrl-D結束。
有郵件正文
檔案內容傳送mail -s "主題" 收件地址 < 檔案路徑檔名
mail -s "test" [email protected] < ./xxx/xxx.txt
cat 郵件正文.txt | mail -s 郵件主題 [email protected]
單內容傳送
echo "郵件正文內容" | mail -s "郵件主題" [email protected]
帶附件
注意-a
的位置放置有講究。mail -a 附件 -s "主題" 收件地址 < 檔案(郵件正文.txt)
mail -a /data/xxx.tar.gz -s "Linux mail test" [email protected] < ./xxx/xxx.txt
對於擔心郵件內容被非8-bit clean
的機器汙染,可以使用uuencode
進行binary-to-text
的轉換進行傳送。
首先安裝$ sudo apt-get install sharutils
引數第一個 attachment 是需要編碼的檔名,第二個 attachment 是解碼時得到的檔名。
uuencode attachment attachment > attachment_uu
mail -a attachment_uu -s "Linux mail attachment" [email protected] < ./xxx/xxx.txt
- 1
- 2
- 3
接收方接收後使用uudecode attachment_uu
即可解壓得到檔名為attachment
的檔案 (編碼時的第二個引數)。
uuencode
可以使用-m
選項選擇base64的編碼方式,對具體原理感興趣的請自行查閱。
在 Winows 中,用 Outlook、Foxmail 等客戶端能自動識別經過 uuencode 編碼的附件。如果附件被收取到 Linux/Unix 等系統下,如果 Linux 下的 MUA 或者客戶端不識別,可以手工處理郵件提取相應的部分,用 uudecode 解碼即可。
檢視郵件
常用命令(郵件命令提示符中常用命令)
h: 檢視郵件標題。
d: 使用該命令加序號可以刪除指定的郵件。
f: 檢視當前游標指向的郵件。
n: 跳轉到指定序號的郵件。
w: 儲存郵件資訊到檔案,儲存附件到單獨的檔案。
q: 退出並將已閱讀的郵件存入檔案~/mbox中。
x: 退出不保持之前的操作,如刪除郵件等。
!: 允許使用shell命令。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
sendmail
mailq或者sendmail -bp
可以檢視當前仍在佇列裡待發送的郵件,如何刪除該佇列呢?
刪除下面資料夾下的檔案即可
/var/spool/postfix/deferred/
再次執行檢查佇列得到: Mail queue is empty
/var/log/syslog檔案中也會儲存傳送失敗的資訊
- 1
- 2
- 3
- 4
- 5
- 6
sendmail安裝
$ sudo apt-get install sendmail
$ sudo apt-get install sendmail-cf
其他附件:
spamassassin, 提供郵件過濾
- 1
- 2
- 3
- 4
- 5
sendmail配置
根據.mc檔案生成配置.cf檔案
m4 /etc/mail/sendmail.mc > /etc/sendmail.cf
- 1
- 2
輸入ps aux | grep sendmail
檢視是否安裝成功,看到資訊sendmail: MTA: accepting connections
關閉並重啟sendmail服務
/etc/init.d/sendmail stop
ps -ef | grep sendmail | awk '{print $2}' | xargs kill -5
/etc/init.d/sendmail start
- 1
- 2
- 3
- 4
- 5
郵件內容、附件處理
sudo apt-get install mpack
用於解析 MIME 型別的資訊,使用munpack
即可用於訊息裡的附件和內容分離。sudo apt-get install procmail
用於 MDA,過濾訊息等。sudo apt-get install getmail4
,並配置好 getmail,抓取 mail。