Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

smtp

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!]

like image 459
user1433767 Avatar asked Jun 03 '12 19:06

user1433767


People also ask

What is HTML email format?

HTML emails have everything plain text emails don't have: color, style, images, and sometimes multimedia. HTML emails are similar to webpages, only they're delivered to people's email inboxes. As such, you can design your HTML email to match your brand and give your readers a more visually engaging experience.

What is the correct format for sending an email?

How To Format an Email Message. Your email message should be formatted like a typical business letter, with spaces between paragraphs and no typos or grammatical errors. Don't mistake length for quality—keep your email brief and to the point. Avoid overly complicated or long sentences.


1 Answers

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" <[email protected]> 
To: "Edited Out" <[email protected]> 
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.

like image 150
ejdyksen Avatar answered Sep 28 '22 01:09

ejdyksen