Correct email format for Email with Plain, HTML and an Attachment in SMTP?

五迷三道 提交于 2019-12-12 07:18:31

问题


The Problem:

I tried to copy the SMTP based headers/emails used by yahoo and hotmail and send them over Telnet, but the email I get does not display the included attachment, but it does display the message correctly.

What I suspect is causing it:

I believe I have the incorrect format for the email, and despite searching numerous articles online (as well as trying to grasp complicated and difficult to read RFCs), I have not found any helpful or concise articles that explain how to use all three (Plain, HTML and an attachment) in SMTP correctly. I've spent several days trying to alter the code, but I either get just the message (no attachment) or the entire SMTP Data body displaying (including boundaries, html code etc).

What I hoping to achieve via this question:

I'm hoping someone can look over the information I've gathered below, and be able to tell me what I am missing (or how exactly the formatting of the email is incorrect).

Debugging information:

I fed a duplicate of the information I sent to the SMTP server to file, and I've pasted the information to pastebin (with any personal information removed/edited out - the Base64 encoded attachment is just a text file of another email gotten from IMAP):

Information sent verbatim (minus control characters) to SMTP server:

http://pastebin.com/QYwzWT0S

What I see in the email client (note no attachment):

http://i45.tinypic.com/29b1zci.jpg

What IMAP sees when I download the email (note incorrect format):

http://pastebin.com/zv3PBr8N

What a correctly formatted email should look like to IMAP:

http://pastebin.com/3yBySbxU

I suspect SMTP is misinterpreting what I am sending, which is why the multipart/alternative is missing when IMAP tries to retrive the email. What exactly am I doing wrong? What does the server expect?

[Thank you for the upvotes - I can re-enable the links!]


回答1:


I cleaned up the multipart boundary specifiers and got this, which works for me on my server (I've left off the SMTP commands here):

From: "Edited Out" <editedout@yahoo.com> 
To: "Edited Out" <editedout@yahoo.com> 
Subject: Testing 4
MIME-Version: 1.0
Content-Type: multipart/alternative;
  boundary="boundary-type-1234567892-alt"

--boundary-type-1234567892-alt
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


Testing the text to see if it works!

--boundary-type-1234567892-alt
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


<html>Does this actually work?</html>

--boundary-type-1234567892-alt
Content-Transfer-Encoding: base64
Content-Type: text/plain;name="Here2.txt"
Content-Disposition: attachment;filename="Here2.txt"

KiAxMyBGRVRDSCAoQk9EWVtURVhUXSB7NjU5fQ0KLS1fZjZiM2I1ZWUtMjA3YS00ZDdiLTg0NTgtNDY5YmVlNDkxOGRhXw0    KQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJpc28tODg1OS0xIg0KQ29udGVudC1UcmFuc2Zlci1FbmNvZG    luZzogcXVvdGVkLXByaW50YWJsZQ0KDQoNCkp1c3Qgc2VlaW5nIHdoYXQgdGhpcyBhY3R1
YWxseSBjb250YWlucyEgCQkgCSAgIAkJICA9DQoNCi0tX2Y2YjNiNWVlLTIwN2EtNGQ3Yi04NDU4LTQ2OWJlZTQ5MThkYV8    NCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sOyBjaGFyc2V0PSJpc28tODg1OS0xIg0KQ29udGVudC1UcmFuc2Zlci1FbmNvZG    luZzogcXVvdGVkLXByaW50YWJsZQ0KDQo8aHRtbD4NCjxoZWFkPg0KPHN0eWxlPjwhLS0N
Ci5obW1lc3NhZ2UgUA0Kew0KbWFyZ2luOjBweD0zQg0KcGFkZGluZzowcHgNCn0NCmJvZHkuaG1tZXNzYWdlDQp7DQpmb25    0LXNpemU6IDEwcHQ9M0INCmZvbnQtZmFtaWx5OlRhaG9tYQ0KfQ0KLS0+PC9zdHlsZT48L2hlYWQ+DQo8Ym9keSBjbGFzcz    0zRCdobW1lc3NhZ2UnPjxkaXYgZGlyPTNEJ2x0cic+DQpKdXN0IHNlZWluZyB3aGF0IHRo
aXMgYWN0dWFsbHkgY29udGFpbnMhIAkJIAkgICAJCSAgPC9kaXY+PC9ib2R5Pg0KPC9odG1sPj0NCg0KLS1fZjZiM2I1ZWU    tMjA3YS00ZDdiLTg0NTgtNDY5YmVlNDkxOGRhXy0tDQopDQpmbHlubmNvbXB1dGVyIE9LIEZFVENIIGNvbXBsZXRlZA


--boundary-type-1234567890-alt--

The boundary specifier in a multipart email is any arbitrary text that's not likely to appear in the email body/attachments. When it shows up with two dashes in front, that specifies the beginning of new part (including the very first part. When it shows up with two dashes at the beginning and end, that specifies the end of the mail.

Your original mail had this "end boundary" marker in the middle of the mail (right after <html>Does this actually work?</html>), and had two different boundary markers (--boundary-type-1234567890 and --boundary-type-1234567892-alt). That explains why the attachment was left off.




回答2:


It turns out the problem is incredibly subtle.

Hotmail conventionally (this was the original email I based it on ), uses the term "multipart/alternative" for multiple parts of the email. I used 'multipart/alternative' in my email.

It turns out yahoo does not like 'multipart/alternative' and will only correctly display the plain text, html and attachment correctly when instead the term 'multipart/mixed' is used (as I noticed yahoo compliant emails use this term instead of alternative), in combination with ejdyksen's suggestion (neither suggestion works by itself for yahoo's server though).

Although, technically speaking, this is the 'correct' answer, I will leave ejdyksen's answer selected because his efforts had shown it was some server-side technicality (his SMTP server and hotmail's both displayed the attachment, yahoo's didn't). Politely upvote ejdyksen's answer instead as he did most of the legwork.



来源:https://stackoverflow.com/questions/10873269/correct-email-format-for-email-with-plain-html-and-an-attachment-in-smtp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!