如何从带有 HTML 内容正文和附件的 Linux 命令行发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16686274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:58:15  来源:igfitidea点击:

How to send mail from linux command line with HTML content body and an attachment

linuxemailsendmail

提问by Srujana Ontipenta

I need to send mail from a linux host with HTML content type and also attach a file to the mail.

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

cat html_mail.txt

To: [email protected]

Subject: Test Mail

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

Content-Disposition: inline

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

I tried the below options:

我尝试了以下选项:

mail: 

mail -a attachment_file < html_mail.txt

"mail" command sends an attachment but the HTML content in html_mail.txt is coming up as plain text in the mail

“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.

回答by AnFi

Sending "HTML only" email using low level sendmailcommand

使用低级sendmail命令发送“仅 HTML”电子邮件

1) Add necessary MIME headers
(MIME-Version, Content-Type, Content-Transfer-Encoding)

1) 添加必要的 MIME 标头
( MIME-Version, Content-Type, Content-Transfer-Encoding)

html_mail_file

html_mail_file

To: [email protected]
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>

*For non us-asciicharsets declare 8BITencoding. Most email servers will conduct necessary transformations of "raw" 8BIT encoding.

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

2) Send it using sendmailprogram

2)使用sendmail程序发送

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

or if you prefer to keep email headers separately

或者如果您更喜欢单独保留电子邮件标题

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