« Sending Mail With Attachment From Command Line | 首頁 | 2009/01, Vol. 156 PChome Magazine »
2009年01月07日
自製郵件發送程式
目的: 以 perl、shell script 自製可夾帶檔案 (使用 MIME 格式) 的郵件發送程式
系統需求: perl、sendmail
vi sendmail.pl
vi attach.sh
#!/bin/sh
filename=$(echo $1 | awk 'BEGIN{FS="/"}{print $NF}')
realfile=$1
echo "
--thisisatest
Content-Type: application/octet-stream; name=\"${filename}\"
Content-Disposition: attachment; filename=\"${filename}\"
Content-Transfer-Encoding: base64
"
base64 $realfile
使用方法
- sendmail.pl 寄件者 收件者 主旨
鍵盤輸入郵件內容
鍵盤輸入郵件內容
鍵盤輸入郵件內容
(Ctrl + D)- sendmail.pl 寄件者 收件者 主旨 < 郵件內容檔案
- (cat 郵件內容檔案; attach.sh 夾檔1; attach.sh /path/to/夾檔2) | sendmail.pl 寄件者 收件者 主旨
ps. 使用前記得先修改 sendmail.pl 裡面的 $domain 變數
實例說明
# adan 與 bob 都是本機帳號
sendmail.pl adan bob "This is a test" < mail.body# 寄給外部帳號
sendmail.pl adan someone@somewhere.com "This is a tes" < mail.body# 偽裝寄件人
sendmail.pl billgates@microsoft.com someone@somwhere.com "This is a test" < mail.body# 夾帶檔案
(cat mail.body; attach.sh file1.txt; attach.sh file2.jpg) | sendmail.pl adan bob "This is a test"# 寄給本機所有使用者 (分別夾帶不同檔案)
vi userlist_generator.sh#!/bin/sh
if [ "$1" == "" ]; then
echo "Usage: $0 outputfile"
exit 1
elif [ -e "$1" ]; then
true > $1
fi
cd /home
for i in *; do
id $i > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo $i >> $1
fi
donesh userlist_generator.sh /tmp/user_list.txt
for i in $(cat /tmp/user_list.txt); do \
(cat mail.body; attach.sh file1.txt; attach.sh userfiles/$i.txt) | sendmail.pl root $i "公告" \
done
參考資料:
http://www.usenet-forums.com/linux-general/95591-sending-mail-script-setting-header.html
Posted by Jamyy at 2009年01月07日 14:46