[英]Send a mail using 'sendmail' with uuencode, and having a subject alongwith
我正在嘗試使用sendmail發送帶附件的郵件。 問題是我無法發送主題行。
以下命令行觸發兩個郵件而不是一個 - 一個帶有“ Subject : Report
”,另一個帶有附件:
/usr/bin/gmime-uuencode "/tmp/url_by_ip.txt" "Report.txt" | echo "Subject: Report" | /usr/sbin/sendmail <sender> <receiver>
如果你可以使用其他命令,我建議mutt
可以很容易地處理附件:
mutt -a file_to_attach -s "your subject" recipient@test.com < /tmp/mail_content
如果您堅持使用/usr/sbin/sendmail
那么您還有很多工作要做。 sendmail
沒有附件概念,並將電子郵件內容視為平面US-ASCII文本(有關詳細信息,請參閱此答案 )。
要使用sendmail
發送附件,您需要將郵件內容格式化為MIME郵件。 有關示例,請參閱unix.com上的此論壇帖子 。
為了讓您順利進行,這是一個使用bash的快速未經測試的示例。 為簡潔起見,我對變量進行了硬編碼,但您可以很容易地將示例轉換為將相關變量作為參數的腳本/函數。
#!/bin/bash
# --- user params ---
MAILFROM="peter.gibbons@initech.com"
MAILTO="bill.lumbergh@initech.com"
SUBJECT="TPS Report"
BODY_FILE="/home/peter/coversheey.txt" # file holding mail body
ATT_FILE="/tnp/url_by_ip.txt" # file to attach
ATT_AS_FILE="Report.txt" # name to attach as
# --- generated values ---
BOUNDARY="unique-boundary-$RANDOM"
BODY_MIMETYPE=$(file -ib $BODY_FILE | cut -d";" -f1) # detect mime type
ATT_MIMETYPE=$(file -ib $ATT_FILE | cut -d";" -f1) # detect mime type
ATT_ENCODED=$(base64 < $ATT_FILE) # encode attachment
# --- generate MIME message and pipe to sendmail ---
cat <<EOF | /usr/sbin/sendmail $MAILTO
MIME-Version: 1.0
From: $MAILFROM
To: $MAILTO
Subject: $SUBJECT
Content-Type: multipart/mixed; boundary="$BOUNDARY"
--$BOUNDARY
Content-Type: $BODY_MIMETYPE
Content-Disposition: inline
$(cat $BODY_FILE)
--$BOUNDARY
Content-Type: $ATT_MIMETYPE; name="$ATT_AS_FILE"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$ATT_AS_FILE"
$ATT_ENCODED
--$BOUNDARY
EOF
當然,如果您樂意使用更高級別的腳本語言(Python,Ruby,Perl,......),那么現有的模塊將會為您提供繁重的工作。
ps還有一個mpack實用程序可以為你進行MIME轉換,但AFAIK在大多數* nix框中都不是默認的。
使用您的郵件標題創建一個文件,例如mail.txt:
Subject: here is the subject
這里發送電子郵件的腳本:
#!/bin/bash
cat mail.txt > mail_to_send
cat attachment | uuencode attachment >> mail_to_send
sendmail recipient@example.com < mail_to_send
rm mail_to_send
嘗試這個:
mail -s 'Report' myemail@yahoo.com < tmp/url_by_ip.txt
也許您想要一些實用程序發送MIME郵件? 有vmime C ++庫 ,許多腳本語言(Python,Ruby,Perl,Ocaml,...)都有MIME包。 shell腳本也可以使用命令,例如mpack和許多其他命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.