简体   繁体   English

如何使用 Python MIME multipart 分离 email?

[英]How could I separate email using Python MIME multipart?

Sending an email using sockets.使用 sockets 发送 email。 I want to separate plain text and attachment.我想将纯文本和附件分开。 I use MIME multipart/mixed;我使用 MIME 多部分/混合; boundary.边界。

cSockSSL.send("MIME-Version: 1.0\r\n".encode())
cSockSSL.send("Content-Type: multipart/mixed; boundary=gg4g5gg\r\n".encode())
cSockSSL.send("--gg4g5gg".encode('ascii'))

cSockSSL.send("Content-Type: text/plain\r\n".encode())
cSockSSL.send("Some text".encode())
cSockSSL.send("--gg4g5gg".encode())

cSockSSL.send("Content-Type: text/plain\r\n".encode())
cSockSSL.send("Content-Disposition: attachment; filename = gg.txt\r\n".encode())
cSockSSL.send(txt_file)
cSockSSL.send("--gg4g5gg--".encode())
cSockSSL.send("\r\n.\r\n".encode())

In this case, I get an empty email with the header.在这种情况下,我得到一个空的 email 和 header。 If I delete first boundary I'll get this:如果我删除第一个边界,我会得到这个:

Some text--gg4g5ggContent-Type: text/plain Content-Disposition: attachment;一些文本--gg4g5ggContent-Type: text/plain Content-Disposition: attachment; filename = gg.txt Hey!文件名 = gg.txt 嘿! I'm txt file!--gg4g5gg--我是txt文件!--gg4g5gg--

How to correctly split content-type?如何正确拆分内容类型?

Your email data is malformed, as you are missing several required line breaks.您的 email 数据格式不正确,因为您缺少几个必需的换行符。

Per RFC 2822 , you need to separate the email's headers from the email's body using \r\n\r\n instead of \r\n .根据RFC 2822 ,您需要使用\r\n\r\n而不是\r\n将电子邮件的标头与电子邮件的正文分开。

And per RFC 2045 , you need to separate MIME headers from MIME bodies using \r\n\r\n instead of \r\n .根据RFC 2045 ,您需要使用\r\n\r\n而不是\r\n将 MIME 标头与 MIME 正文分开。 And, you are missing \r\n in front of each MIME boundary that follows a text body, and after each MIME boundary.而且,您在文本正文之后的每个 MIME 边界之前和每个 MIME 边界之后都缺少\r\n

So, essentially, you are sending an email that looks like this, all squished together:因此,本质上,您发送的是一个看起来像这样的 email,所有这些都被挤压在一起:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=gg4g5gg
--gg4g5ggContent-Type: text/plain
Some text--gg4g5ggContent-Type: text/plain
Content-Disposition: attachment; filename = gg.txt
<txt_file>--gg4g5gg--
.

But it needs to look more like this instead:但它需要看起来更像这样:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=gg4g5gg

--gg4g5gg
Content-Type: text/plain

Some text
--gg4g5gg
Content-Type: text/plain
Content-Disposition: attachment; filename = gg.txt

<txt_file>
--gg4g5gg--
.

So, try this instead:所以,试试这个:

cSockSSL.send("DATA\r\n".encode())
# verify response is 354...

cSockSSL.send("MIME-Version: 1.0\r\n".encode())
cSockSSL.send("Content-Type: multipart/mixed; boundary=gg4g5gg\r\n".encode())
cSockSSL.send("\r\n".encode()) # add a line break

cSockSSL.send("--gg4g5gg\r\n".encode('ascii')) # add a line break
cSockSSL.send("Content-Type: text/plain\r\n".encode())
cSockSSL.send("\r\n".encode()) # add a line break
cSockSSL.send("Some text".encode())

cSockSSL.send("\r\n--gg4g5gg\r\n".encode()) # add line breaks
cSockSSL.send("Content-Type: text/plain\r\n".encode())
cSockSSL.send("Content-Disposition: attachment; filename=gg.txt\r\n".encode())
cSockSSL.send("\r\n".encode()) # add a line break
cSockSSL.send(txt_file)
cSockSSL.send("\r\n--gg4g5gg--\r\n".encode()) # add line breaks

cSockSSL.send(".\r\n".encode())

Something else to be aware of - because the DATA command terminator is .\r\n , if "Some text" or txt_file contain any lines that begin with the .需要注意的其他事项 - 因为DATA命令终止符是.\r\n ,如果"Some text"txt_file包含任何以. character, you MUST escape each leading .字符,您必须转义每个前导. as .. , per RFC 2821 Section 4.5.2 ... ,根据RFC 2821 第 4.5.2 节

I suggest you study the RFCs mentioned above more carefully.我建议您更仔细地研究上面提到的 RFC。

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

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