简体   繁体   English

如何在UNIX ksh中发送带有sendmail软件附件的HTML电子邮件

[英]How to send HTML email with attachment with sendmail software in UNIX ksh

I'm trying to send HTML email with an attachment but is not working, is just sending the file 我正在尝试发送带附件的HTML电子邮件,但无法正常工作,只是发送文件

the following code works with the HTML: 以下代码适用于HTML:

message="all the HTML message"
(
   echo "From: UnixMail@test.com";
   echo "To: emails@email.com";
   echo "Subject: Testing HTML with attachment";
   echo "Content-Type: text/html";
   echo "MIME-Version: 1.0";
   echo "";
   echo "${message}";
) | /usr/sbin/sendmail -t

but I'm trying to add the attachment like this: 但我正在尝试添加这样的附件:

message="all the HTML message"
(
   echo "From: UnixMail@test.com";
   echo "To: emails@email.com";
   echo "Subject: Testing HTML with attachment";
   echo "Content-Type: text/html";
   echo "MIME-Version: 1.0";
   echo "Content-Transfer-Encoding: base64"
   echo "Content-Type: application/octet-stream; name=test_file.txt"
   echo "Content-Disposition: attachment; filename=/directory/myfile"
   echo "";
   echo "${message}";
) | /usr/sbin/sendmail -t

Is not working, just send the attachment without any readable things... any idea? 不工作,只是发送附件没有任何可读的东西......任何想法?

 echo "Content-Transfer-Encoding: base64" ... echo "${message}"; 

Mail is restricted traditionally to ASCII data and a line length of 998 characters. 邮件传统上受限于ASCII数据,行长度为998个字符。 This means that binary data need to be encoded to adhere to these limitations and that the type of encoding needs to be specified. 这意味着需要对二进制数据进行编码以遵守这些限制,并且需要指定编码类型。 Specifying the encoding is done with the Content-Transfer-Encoding header which you correctly set to base64 . 使用您正确设置为base64Content-Transfer-Encoding标头指定编码。 The problem is that you don't encode the data as base64 then but instead just insert the original data. 问题是您不将数据编码为base64,而只是插入原始数据。 Most mail clients will in this case just ignore any characters not belonging to base64 (ie everything outside A-Za-z0-9/+ ) as garbage. 在这种情况下,大多数邮件客户端只会忽略任何不属于base64的字符(即A-Za-z0-9/+之外的所有字符)作为垃圾。 The rest will be treated as base64 encoding and decoded accordingly, resulting in the junk data you see. 其余的将被视为base64编码并相应地进行解码,从而产生您看到的垃圾数据。

Thus you should not just include ${message} directly but instead first encode the message. 因此,您不应该直接包含${message} ,而是首先对消息进行编码。 This can be done using the base64 tool if available or with uuencode --base64 . 这可以使用base64工具(如果可用)或使用uuencode --base64来完成 But with uuencode you absolutely need to remove the prefix and postfix line and keep only the encoded data. 但是使用uuencode,您绝对需要删除前缀和后缀行,并仅保留编码数据。

Apart from that you should probably do this with the HTML message too unless the message is already within the restrictions of only ASCII characters and a line length limit of 998 characters. 除此之外,您应该使用HTML消息执行此操作,除非消息已经在仅限于ASCII字符且行长度限制为998个字符的限制内。 It will probably work without encoding in most cases but might fail with some recipients, depending on the mail servers involved and the capabilities of the mail client. 在大多数情况下,它可能无需编码,但可能会因某些收件人而失败,具体取决于所涉及的邮件服务器和邮件客户端的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM