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

Is there a way to set up a email script that will send an alternate plain text mail message if the recipient can not accept html formatted messages?
Thanks

Replies are listed 'Best First'.
Re: email for both plain text and html
by dtr (Scribe) on Aug 24, 2005 at 16:40 UTC

    OK - you need to learn about MIME messages before attempting this.

    MIME is the standard that allows you to send emails including attachments across the internet. Whenever you send an email message including an attachment, what you're actually sending is a MIME message, that includes two (or more) parts - one containing your message, the other containing the attachment.

    MIME also allows you to send "multipart/alternative" messages - which basically means that you can send a message in both HTML and plain text (and also PDF / Word / Whatever) format, and the recipient's mail client will decide which version to display to the user according to its capabilities, and the user's preferences. This is what you actually want to do in this case.

    The MIME::Lite module is one of a number on the CPAN that can help you do what you want. I'd suggest reading through the docs for this module, and googling for "MIME" to get an understanding as well.

Re: email for both plain text and html
by kirbyk (Friar) on Aug 24, 2005 at 16:41 UTC
    What you want is multipart mime email. If their mail reader supports the mime standards, it will display the text only part if they want text, and the html part if their reader is html capable. (This is normally a big if, but it seems to be well supported in practice. And it looks all pretty in Outlook to pass the Business Unit test.)

    It's something like (from one of our templates):

    To: !!email!! cc: !!cc!! From: W3 Data <support@w3data.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="_jkkdsffds32432dlkjifew +ks_" Subject: BatchAppend411 Job Confirmation --_jkkdsffds32432dlkjifewks_ Content-Type: text/plain; charset="iso-8859-1" ... Text email content ... --_jkkdsffds32432dlkjifewks_ Content-Type: text/html; charset="iso-8859-1" <html> <body> ... more html content
    The 'boundary' is just an arbitrary string that won't appear in the email.

    -- Kirby, WhitePages.com

Re: email for both plain text and html
by atcroft (Abbot) on Aug 24, 2005 at 16:42 UTC

    Yes, you can send emails that contain a section of content that is of type 'text/html' and another that is 'text/plain'. Many of the modules available for sending emails describe this in their documentation; for instance, Mail::Sendmail specifically mentions doing this in its FAQ (with sample code), and MIME::Lite includes information on sending HTML in its documentation.

    HTH.

    Update: 24 Aug 2005 - s/speficially/specifically/

      Thanks for the replies, got some good stuff. Now I will go read how to implement.
Re: email for both plain text and html
by punkish (Priest) on Aug 24, 2005 at 16:42 UTC
    Unless you have magical powers (or you control the recipient's environment), you can't determine what the recipient can/cannot accept.

    The most you can do is to send both html and plain text versions (which is what most all email clients do), and let the recipient choose which one she wants.

    Of course, the best you can do is to not bother with HTML at all... just send plain text message only.

    --

    when small people start casting long shadows, it is time to go to bed
      Howdy!

      Amen! ++

      If you *must* send a text/html part, do not neglect to send it as text/plain as well. Otherwise, you run the risk of having your message appear to be empty to those of us who eschew the hazards of interpreting HTML markup in a mail client. Further, mailing lists will not infrequently strip out non-text parts.

      yours,
      Michael
Re: email for both plain text and html
by davidrw (Prior) on Aug 24, 2005 at 16:37 UTC
    How are you going to determine what the recipient can or can not accept??

    that aside, you can use one of the HTML modules to convert to plain text.. (i'm assuming you have an html file to send? or do you have data and can construct text or html output for the mail?)