如何使用HTML内容正文和附件从linux命令行发送邮件

时间:2013-05-22 07:48:50

标签: linux email sendmail

我需要从具有HTML内容类型的linux主机发送邮件,并将文件附加到邮件中。

cat html_mail.txt

To: me@mydomain.com

Subject: Test Mail

Content-Type: text/html; charset="us-ascii"

Content-Disposition: inline

<span style="background-color:green">This is in green</span>

我尝试了以下选项:

mail: 

mail -a attachment_file < html_mail.txt

“mail”命令发送附件,但html_mail.txt中的HTML内容将以邮件中的纯文本形式出现

Execution of the command says "Ignoring headers Content-Type".

sendmail:
cat html_mail.txt |sendmail -t
sendmail sends the html content properly, but I couldn't find an option to send an attachment.

1 个答案:

答案 0 :(得分:3)

仅发送&#34; HTML&#34;使用低级sendmail命令

发送电子邮件

1)添加必要的MIME标题
MIME-VersionContent-TypeContent-Transfer-Encoding

html_mail_file

To: me@mydomain.com
Subject: Test Mail
MIME-Version: 1.0
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: 7BIT
Content-Disposition: inline

<span style="background-color:green">This is in green</span>

*对于非us-ascii字符集声明8BIT编码。大多数电子邮件服务器都会对&#34; raw&#34;进行必要的转换。 8BIT编码。

2)使用sendmail程序

发送
/usr/bin/sendmail -i -t < html_mail_file

或者如果您希望单独保留电子邮件标题

echo | cat email_headers_file - html_file | /usr/bin/sendmail -i -t