HTML vs Plain text as body in email

Svish picture Svish · Jun 11, 2012 · Viewed 18.7k times · Source

When creating a Swift_Message you can have both a plain text and an html version of the email. You set one as the body, and then you add the other one as a part.

What is the difference if I set the HTML version as body and plain text as part, and the other way around?

$html_as_body = Swift_Message::newInstance()
    ->setSubject($subject)
    ->setBody($html, 'text/html')
    ->addPart($plain, 'text/plain');

$plain_as_body = Swift_Message::newInstance()
    ->setSubject($subject)
    ->setBody($plain, 'text/plain')
    ->addPart($html, 'text/html');

And by difference, I mean what kind of difference will it have in email readers and such?

Answer

trejder picture trejder · Jun 21, 2012

There should be no difference, if only Swift_Message follows official RFC for mail compose, which (AFAIK) strictly sets that plain-text message should be default (first, body) and eventually followed by HTML version.

Make some tests, i.e. sent the same e-mails in both configurations and see in receiving client what is the source of your message.

If you see any difference, then in IMHO you should definitely send plain text body (first) and HTML version last. As even nowadays there are many simple mail clients, which will simple ignore / not properly recognize HTML version. So, if you put it as first, there is a chance that whole message will be unreadable for the receiver.