发送带有mailx的明文正文电子邮件和一个明文附件:
(
/usr/bin/uuencode attachfile.txt myattachedfilename.txt;
/usr/bin/echo "Body of text"
) | mailx -s 'Subject' youremail@example.com
下面是与上面相同的命令,没有换行符
( /usr/bin/uuencode /home/el/attachfile.txt myattachedfilename.txt; /usr/bin/echo "Body of text" ) | mailx -s 'Subject' youremail@example.com
确保你有一个文件/home/el/attachfile.txt,其中定义了以下内容:
<html><body>
Government discriminates against programmers with cruel/unusual 35 year prison
sentences for making the world's information free, while bankers that pilfer
trillions in citizens assets through systematic inflation get the nod and
walk free among us.
</body></html>
如果你没有uuencode,请阅读这个:https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work
在Linux上,用sendmail发送HTML正文电子邮件和PDF附件:
确保你已经安装了ksh: yum info ksh
确保已经安装和配置了sendmail。
确保已安装uuencode并可用:https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work
创建一个名为test.sh的新文件,并将其放在主目录:/home/el中
将以下代码放入test.sh:
#!/usr/bin/ksh
export MAILFROM="el@defiant.com"
export MAILTO="youremail@example.com"
export SUBJECT="Test PDF for Email"
export BODY="/home/el/email_body.htm"
export ATTACH="/home/el/pdf-test.pdf"
export MAILPART=`uuidgen` ## Generates Unique ID
export MAILPART_BODY=`uuidgen` ## Generates Unique ID
(
echo "From: $MAILFROM"
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
echo ""
echo "--$MAILPART"
echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
echo ""
echo "--$MAILPART_BODY"
echo "Content-Type: text/plain; charset=ISO-8859-1"
echo "You need to enable HTML option for email"
echo "--$MAILPART_BODY"
echo "Content-Type: text/html; charset=ISO-8859-1"
echo "Content-Disposition: inline"
cat $BODY
echo "--$MAILPART_BODY--"
echo "--$MAILPART"
echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: uuencode"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
echo ""
uuencode $ATTACH $(basename $ATTACH)
echo "--$MAILPART--"
) | /usr/sbin/sendmail $MAILTO
更改test.sh顶部的导出变量,以反映您的地址和文件名。
下载一个测试pdf文档,并将其放在/home/el中,称为pdf-test.pdf
创建一个名为/home/el/email_body.htm的文件,并在其中放入以下一行:
<html><body><b>this is some bold text</b></body></html>
确保pdf文件有足够的755权限。
执行脚本。/test.sh
Check your email inbox, the text should be in HTML format and the pdf file automatically interpreted as a binary file. Take care not to use this function more than say 15 times in a day, even if you send the emails to yourself, spam filters in gmail can blacklist a domain spewing emails without giving you an option to let them through. And you'll find this no longer works, or it only lets through the attachment, or the email doesn't come through at all. If you have to do a lot of testing on this, spread them out over days or you'll be labelled a spammer and this function won't work any more.