mavericknik has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, Im trying to send an email which has both html and plaintext in the body. I'm using MIME::entity and Mail::Mailer. Simplified code:
use MIME::Entity; use Mail::Mailer qw(sendmail); my $email = MIME::Entity->build(Type => "multipart/alternative", To => 'somebody@example.com', subject => "Test Email"); $email->attach(Data => ["THIS IS DIRECTLY ATTACHED TEXT"], Type => "text/plain; charset=UTF-8", Encoding => "quoted-printable"); $email->attach(Data => ["<h2>THIS IS HTML TEXT<h2>"], Type => "text/html; charset=UTF-8", Encoding => "quoted-printable"); $email->print(\*STDOUT); $email->send;
When I check my email, I get only the html part to show. The plain text part disappears. However, if I remove the html part, the plain text part shows fine. Is this the right way to send combined text/html emails? I'm afraid I might be doing something stupid. The output of MIME::Entity is:
Content-Type: multipart/alternative; boundary="----------=_1455820701- +7549-0" Content-Transfer-Encoding: binary MIME-Version: 1.0 X-Mailer: MIME-tools 5.414 (Entity 5.414) To: somebody@example.com Subject: Test Email This is a multi-part message in MIME format... ------------=_1455820701-7549-0 Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable THIS IS DIRECTLY ATTACHED TEXT= ------------=_1455820701-7549-0 Content-Type: text/html; charset=UTF-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable <h2>THIS IS HTML TEXT<h2>= ------------=_1455820701-7549-0--
Thanks for any help!

Replies are listed 'Best First'.
Re: Sending email with both html and plaintext
by CountZero (Bishop) on Feb 18, 2016 at 21:04 UTC
    You are using the MIME-type "multipart/alternative" which is used (as per RFC 2046) "for representing the same data in multiple formats". In other words your MIME compliant email reader dutyfully shows you the HTML content (as that is the most "rich" content) and hides the alternative text component.

    Did you try "multipart/mixed"?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Yes I did try "multipart/mixed". What this does is sends whatever I attach first in the body of the email, and what I attach second as an attachment to the email. So in this example, I get "THIS IS DIRECTLY ATTACHED TEXT" in the body of the email and "THIS IS HTML TEXT" as a seperate .html attachment in the same email. Is there any other type I can try?
        I think that is the expected behaviour: one email with attachments.

        Perhaps you can force your email reader to show the attachment as an email message if the attachment is a proper email message in its own right? I remember that the email reader on my BlackBerry would show all attachemnts that are proper emails indeed as one continuing email and not as a separate attachments.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
        Add an (additional) empty text before your first attachment?
Re: Sending email with both html and plaintext
by flexvault (Monsignor) on Feb 18, 2016 at 20:34 UTC

    Hello mavericknik,

    Are you looking at what you are sending or the way the email client displays your sent message. Usually if you have both text and html, a html enabled browser will show the html. (Note: I have never used either MIME::entity or Mail::Mailer).

    On my email client ( Thunderbird ), you can type the ^U to see what is in the email. If that doesn't work you can save the file and using your favorite editor view the contents.

    If the text and html are different then you may have to send a dummy text first, then the html and then the text data.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      Interesting. I use outlook, and the body is:
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><h2 +>THIS IS HTML</h2>
      And the headers are:
      MIME-Version: 1.0 Subject: Test Message Content-Type: multipart/alternative; boundary="----------=_1455830463-9817-0" X-Mailer: MIME-tools 5.414 (Entity 5.414)
      Could be a problem with mail::mailer, not sure at this point.
Re: Sending email with both html and plaintext
by gnosti (Hermit) on Feb 19, 2016 at 03:38 UTC
    Hi,

    You may like to analyze a mail message that elicits the desired behavior from mail clients, and then figure out how to create mails with a similar structure.

Re: Sending email with both html and plaintext
by perlfan (Vicar) on Feb 19, 2016 at 22:06 UTC
Re: Sending email with both html and plaintext
by thargas (Deacon) on Feb 22, 2016 at 13:11 UTC

    "multipart/alternative" is supposed to have the client choose the "best" format to show the user. Unfortunately, the behaviour is client-dependent, because: Outlook.

    Also investigate what happens with different settings for "Content-Disposition".

Re: Sending email with both html and plaintext
by MidLifeXis (Monsignor) on Feb 19, 2016 at 19:46 UTC

    Can you send this to a different account (with a different MUA) to see if both types of content make it there? It could be that MS is trying to be helpful by only showing the part that is nice and pretty.

    --MidLifeXis