« 在 GeForce 8600M GS 256MB 順暢玩 GTA4 | 首頁 | 自製郵件發送程式 »

2009年01月05日

Sending Mail With Attachment From Command Line

使用 uuencode + mail command

yum install sharutils

夾帶一個檔案:

(cat mail.body; uuencode /path/to/attach_file attach_file) | mail -s "your subject" someone@somewhere.com

夾帶兩個檔案:

uuencode /path/to/file1.txt attachment1.txt > attach1.uu
uuencode /path/to/file2.txt attachment2.txt > attach2.uu
(cat mail.body; cat attach{1,2}.uu) | mail -s "your subject" someone@somewhere.com

ps.

  1. uuencode filename filename ==> 第一個 filename 是 linux 系統中實際檔案路徑, 第二個 filename 是收件者在收信介面中看到的夾檔名稱.
  2. 若收信介面不支援 uuencode 夾檔 (如: gmail), uuencode 編碼內容會變成郵件本文. 只要將郵件裡的 begin ... 到 ... end 複製起來另存新檔 (建議副檔名為 .uu), 執行 uudecode 指令 (uudecode filename) 即可還原夾檔. Windows 使用者亦可將 begin ... 到 ... end 貼到記事本, 並另存成 filename.uu (副檔名為 .uu 的檔案), 就能以 WinZip 或 WinRAR 開啟附件檔案.
  3. Yahoo! Mail 在 uuencode 夾帶兩個檔案的情況下, 只有第一個夾檔會以夾檔方式呈現, 第二個夾檔會變成郵件本文.

使用 mutt

yum install mutt

mutt -s "your subject" -a attachment1.txt -a attachment2.txt someone@somewhere.com < mail.body


參考資料: SHELLdorado - Sending files as mail attachments

更多的 UNIX 命令列寄送夾檔方式可參考: http://www.panix.com/~kylet/unix-att.html


附錄: 手動產生 MIME 編碼的中文主旨

SUBJECT="=?big5?B?$(echo "中文主旨" | iconv -f utf8 -t big5 | base64)?="

echo "this is mail body." | mail -s $SUBJECT someone@somewhere.com

ps. LANG=zh_TW.Big5 的環境不需使用 iconv 轉碼

Ref: 061的貼貼貼寫: Email主旨亂碼 =?Big5?(Q|B)?xxxxxxxxxx ?=

Posted by Jamyy at 2009年01月05日 10:58