4個可以發送完整電子郵件的命令行工具


今天的文章里我們會講到一些使用Linux命令行工具來發送帶附件的電子郵件的方法。它有很多用處,比如在應用程序所在服務器上,使用電子郵件發送 一個文件過來,或者你可以在腳本中使用這些命令來做一些自動化操作。在本文的例子中,我們會使用foo.tar.gz文件作為附件。

 

有不同的命令行工具可以發送郵件,這里我分享幾個多數用戶會使用的工具,如mailxmuttswaks

我們即將呈現的這些工具都是非常有名的,並且存在於多數Linux發行版默認的軟件倉庫中,你可以使用如下命令安裝:

在 Debian / Ubuntu 系統

apt-get install mutt apt-get install swaks apt-get install mailx apt-get install sharutils

在基於Red Hat的系統,如 CentOS 或者 Fedora

yum install mutt
yum install swaks
yum install mailx
yum install sharutils

1) 使用 mail / mailx

mailx工具在多數Linux發行版中是默認的郵件程序,現在已經支持發送附件了。如果它不在你的系統中,你可以使用上邊的命令安裝。有一點需要注意,老版本的mailx可能不支持發送附件,運行如下命令查看是否支持。

$ man mail

第一行看起來是這樣的:

mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr . . .

如果你看到它支持-a的選項(-a 文件名,將文件作為附件添加到郵件)和-s選項(-s 主題,指定郵件的主題),那就是支持的。可以使用如下的幾個例子發送郵件。

a) 簡單的郵件

運行mail命令,然后mailx會等待你輸入郵件內容。你可以按回車來換行。當輸入完成后,按Ctrl + D,mailx會顯示EOT表示結束。

然后mailx會自動將郵件發送給收件人。

$ mail user@example.com HI, Good Morning How are you EOT

b) 發送有主題的郵件

$ echo "Email text" | mail -s "Test Subject" user@example.com

-s的用處是指定郵件的主題。

c) 從文件中讀取郵件內容並發送

$ mail -s "message send from file" user@example.com < /path/to/file

d) 將從管道獲取到的echo命令輸出作為郵件內容發送

$ echo "This is message body" | mail -s "This is Subject" user@example.com

e) 發送帶附件的郵件

$ echo Body with attachment "| mail -a foo.tar.gz -s "attached file" user@example.com

-a選項用於指定附件。

 

2) mutt

Mutt是類Unix系統上的一個文本界面郵件客戶端。它有20多年的歷史,在Linux歷史中也是一個很重要的部分,它是最早支持進程打分和多線程處理的客戶端程序之一。按照如下的例子來發送郵件。

a) 帶有主題,從文件中讀取郵件的正文,並發送

$ mutt -s "Testing from mutt" user@example.com < /tmp/message.txt

b) 通過管道獲取echo命令輸出作為郵件內容發送

$ echo "This is the body" | mutt -s "Testing mutt" user@example.com

c) 發送帶附件的郵件

$ echo "This is the body" | mutt -s "Testing mutt" user@example.com -a /tmp/foo.tar.gz

d) 發送帶有多個附件的郵件

$ echo "This is the body" | mutt -s "Testing" user@example.com -a foo.tar.gz a bar.tar.gz

3) swaks

Swaks(Swiss Army Knife,瑞士軍刀)是SMTP服務上的瑞士軍刀,它是一個功能強大、靈活、可編程、面向事務的SMTP測試工具,由John Jetmore開發和維護。你可以使用如下語法發送帶附件的郵件:

$ swaks -t "foo@bar.com" --header "Subject: Subject" --body "Email Text" --attach foo.tar.gz

關於Swaks一個重要的地方是,它會為你顯示整個郵件發送過程,所以如果你想調試郵件發送過程,它是一個非常有用的工具。

它會給你提供了郵件發送過程的所有細節,包括郵件接收服務器的功能支持、兩個服務器之間的每一步交互。

(LCTT 譯注:原文此處少了 sharutils 的相關介紹,而多了 uuencode 的介紹。)

 

4) uuencode

郵件傳輸系統最初是被設計來傳送7位編碼(類似ASCII)的內容的。這就意味這它是用來發送文本內容,而不能發會使用8位的二進制內容(如程序文件或者圖片)。uuencode(“UNIX to UNIX encoding”,UNIX之間使用的編碼方式)程序用來解決這個限制。使用uuencode,發送端將二進制格式的轉換成文本格式來傳輸,接收端再轉換回去。

我們可以簡單地使用uuencodemailx或者mutt配合,來發送二進制內容,類似這樣:

$ uuencode example.jpeg example.jpeg | mail user@example.com

Shell腳本:解釋如何發送郵件

#!/bin/bash FROM="" SUBJECT="" ATTACHMENTS="" TO="" BODY="" # 檢查文件名對應的文件是否存在 function check_files() { output_files="" for file in $1 do if [ -s $file ] then output_files="${output_files}${file} " fi done echo $output_files } echo "*********************" echo "E-mail sending script." echo "*********************" echo # 讀取用戶輸入的郵件地址 while [ 1 ] do if [ ! $FROM ] then echo -n -e "Enter the e-mail address you wish to send mail from:\n[Enter] " else echo -n -e "The address you provided is not valid:\n[Enter] " fi read FROM echo $FROM | grep -E '^.+@.+$' > /dev/null if [ $? -eq 0 ] then break fi done echo # 讀取用戶輸入的收件人地址 while [ 1 ] do if [ ! $TO ] then echo -n -e "Enter the e-mail address you wish to send mail to:\n[Enter] " else echo -n -e "The address you provided is not valid:\n[Enter] " fi read TO echo $TO | grep -E '^.+@.+$' > /dev/null if [ $? -eq 0 ] then break fi done echo # 讀取用戶輸入的郵件主題 echo -n -e "Enter e-mail subject:\n[Enter] " read SUBJECT echo if [ "$SUBJECT" == "" ] then echo "Proceeding without the subject..." fi # 讀取作為附件的文件名 echo -e "Provide the list of attachments. Separate names by space. If there are spaces in file name, quote file name with \"." read att echo # 確保文件名指向真實文件 attachments=$(check_files "$att") echo "Attachments: $attachments" for attachment in $attachments do ATTACHMENTS="$ATTACHMENTS-a $attachment " done echo # 讀取完整的郵件正文 echo "Enter message. To mark the end of message type ;; in new line." read line while [ "$line" != ";;" ] do BODY="$BODY$line\n" read line done SENDMAILCMD="mutt -e \"set from=$FROM\" -s \"$SUBJECT\" \ $ATTACHMENTS -- \"$TO\" <<< \"$BODY\"" echo $SENDMAILCMD mutt -e "set from=$FROM" -s "$SUBJECT" $ATTACHMENTS -- $TO <<< $BODY

** 腳本輸出 **

$ bash send_mail.sh ********************* E-mail sending script. ********************* Enter the e-mail address you wish to send mail from: [Enter] test@gmail.com Enter the e-mail address you wish to send mail to: [Enter] test@gmail.com Enter e-mail subject: [Enter] Message subject Provide the list of attachments. Separate names by space. If there are spaces in file name, quote file name with ". send_mail.sh Attachments: send_mail.sh Enter message. To mark the end of message type ;; in new line. This is a message text ;;

 

總結

有很多方法可以使用命令行/Shell腳本來發送郵件,這里我們只分享了其中4個類Unix系統可用的工具。希望你喜歡我們的文章,並且提供您的寶貴意見,讓我們知道您想了解哪些新工具。

譯文:https://linux.cn/article-5502-1.html

轉載請保留鏈接: http://www.linuxeye.com/Linux/2555.html


免責聲明!

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



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