[AnySide] 用shell script实现邮件投递程序 - shell_mailer.sh

.pL@Y www.AnySide.org
11 October 2006 15:10

shell_mailer.sh是为公司写的一个数据库统计工具的副产品,已在Solaris和Linux上测试,成功发信到gmail。

程序由三部分组成:

一、DNS解析
根据收信人的email地址,查询出收信人所在域的MX纪录。
如果MX纪录查询失败,则用该域的A纪录替代MX纪录。
如果A纪录也查询失败,给出错误提示,并停止程序。

代码:
dns_mx(){
#-------------------------
# $reciver 收信人的email地址
# $esmtp 查询出的DNS纪录
#-------------------------

edomain=`echo $reciver | sed 's/^[^@]*@//'`

esmtp=`dig $edomain mx | awk '$4~/MX/ {print $6}' | head -1`

if [ "$esmtp" = "" ]; then
esmtp=`dig $edomain mx | grep "^$edomain" | awk '$4~/A/ {print $5}' | head -1`
fi

test -z $esmtp && echo " Failed:$reciver is unreach" && exit 3
}

二、生成邮件
主要用来生成信头
没有做base64编码 ,如果要发中文邮件,请增加base64编码.

代码:
mesge(){
#------------------------
# $sender 发信人的email地址
# $reciver 收信人的email地址
# $subject 邮件标题
# $email_content_txt 存放邮件内容的文件
#------------------------

cat << EOF
From: <$sender>
To: <$reciver>
Subject: $subject
Date: `date` +0800
Mime-Version: 1.0
ontent-Type: text/plain; charset="us-ascii"; format=flowed
X-Mailer: Bash Mailer
X-MimeOLE: Produced By Liu Yuan , www.AnySide.com

EOF
test -r $email_content_txt && cat $email_content_txt
}

三、投递邮件
执行标准的投递邮件投递过程,唯一不同的是,
不考虑收信服务器的应答,只要连接建立就开始投递.

代码:
send_mail(){
#--------------------------------
#请根据网络情况调整sleep time, 网络越慢,邮件越大,
#所需sleep time越长,但不能太长会造成连接超时.
#-------------------------------

sdomain=`echo $sender | awk -F\@ '{print $2}'`
(
sleep 5
for comm in "helo $sdomain" "mail from:<$sender>" "rcpt to:<$reciver>" "data"
do
echo "$comm";sleep 3
done
mesge
sleep 3;echo "."
)| telnet $esmtp 25
}

 

完整的脚本如下,命名为 shell_mailer.sh

#!/bin/sh
# shell_mailer.sh, Shell Mailer, (MTA)
# .pL@Y
# www.anyside.com

#---------------------
# Variables
#---------------------

sender=$1 # 发信人的email
reciver=$2 # 收信人的email
subject=$3 # 邮件的标题
email_content_txt=$4 # 存放邮件内容的文件

#---------------------

if [ "$#" != 4 ]; then
echo
echo "Usage: $0 test@test.com aa@aa.com "email subject" somefile"
echo
exit 3
fi

dns_mx(){
edomain=`echo $reciver | sed 's/^[^@]*@//'`

esmtp=`dig $edomain mx | awk '$4~/MX/ {print $6}' | head -1`

if [ "$esmtp" = "" ]; then
esmtp=`dig $edomain mx | grep "^$edomain" | awk '$4~/A/ {print $5}' | head -1`
fi

test -z $esmtp && echo " Failed:$reciver is unreach" && exit 3
}

mesge(){
cat << EOF
From: <$sender>
To: <$reciver>
Subject: $subject
Date: `date` +0800
Mime-Version: 1.0
ontent-Type: text/plain; charset="us-ascii"; format=flowed
X-Mailer: Bash Mailer
X-MimeOLE: Produced By Liu Yuan , www.AnySide.com

EOF
test -r $email_content_txt && cat $email_content_txt
}

send_mail(){
sdomain=`echo $sender | awk -F\@ '{print $2}'`
(
sleep 5
for comm in "helo $sdomain" "mail from:<$sender>" "rcpt to:<$reciver>" "data"
do
echo "$comm";sleep 3
done
mesge
sleep 3;echo "."
)| telnet $esmtp 25
}

dns_mx
send_mail
# EOF ----------------------------------

使用方法,例:

$./shell_mailer.sh anonymous@qmail.com realuser@realdomain.com "Anonymous Mail" /etc/hosts

如果执行成功realuser@realdomain.com会收到一封邮件标题为Anonymous Mail的邮件,发信人是anonymous@qmail.com,邮件的内容就是/etc/hosts文件的内容.

-- Liu Yuan.
©2002-2007 AnySide.All rights reserved.