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

I have a web page that uses a Perl action page and Unix Sendmail where it sends email information to my Outlook mail. It works but was wondering if I can send the email using html format. Here is my attempt using html table:
$myInfo .= "<table><tr><td>$myField</td><td>$myValue</td></tr></table> +\n"; open (MAIL,"|$sendmail"); print MAIL <<"EOF"; To: $toAddress From: $fromAddress Subject: Send mail test $myInfo EOF close;
The above sends the information along with the HTML code in the email and doesnt implement the html. Any setting or something in Perl I can get this to work? In Cold Fusion mail tags it lets you put in HTML formatting with a "type" argument where I can use "html" and it will format the email output using HTML. Anything in Perl where I can do the same?

Replies are listed 'Best First'.
Re: email output using HTML format.
by jeffa (Bishop) on May 09, 2005 at 18:50 UTC

    This will probably scare you, but i'd rather show someone the right way than the "just get it done" way. You don't have to use HTML::Template, you can still use your $myInfo variable, but do consider using MIME::Lite.

    use strict; use warnings; use MIME::Lite; use HTML::Template; my $t = HTML::Template->new(filehandle => \*DATA); $t->param( field => 'foo', data => 'bar', ); my $msg = MIME::Lite->new( From =>'me@myhost.com', To =>'you@yourhost.com', Subject =>'Hello HTML', Data => $t->output, # you can just use $myInfo here instead Type =>'text/html', ); $msg->send; __DATA__ <html> <body> <h1>Hello HTML!</h1> <table> <tr> <td><tmpl_var field></td> <td><tmpl_var data></td> </tr> </table> </body> </html>

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      MIME::Lite (or its cousin MIME::Entity) is definately the way to go here. And if you want to get a little bit fancier, you can provide a text version as well as an HTML version for those of us who hate receiving HTML email.

      $msg = MIME::Lite->new( From => $from, To => $to, Subject => $subject, Type => 'multipart/alternative', ); $msg->attach( Type => 'text/html', Data => $html, ); $msg->attach( Type => 'text/plain', Data => $text, );

      The content type of 'multipart/alternative' tells the browser it can choose between the two formats as they should contain the same information. It means a bit more work for you as you have to format a text version and an HTML version, but it will make your users happy.

      - Cees

      Thanks!
Re: email output using HTML format.
by merlyn (Sage) on May 09, 2005 at 20:23 UTC
      I personally use (and like) MIME::Lite, and generally only send text emails, but would consider using Cees' method to send emails in both text and html formats to keep everybody 'happy'.

      It also depends on what you are communicating and the type of site you are promoting. For example, if I was selling items at a discount price for the coming month only, and wanted to send my subscribed members the details of the discounted items (including pictures etc.) in a nice-look html page, then that'll be the best method to format the email.
      _______
      Code is untested unless explicitly stated
      mlh2003
        then that'll be the best method to format the email.
        No, that'd be your opinion about the best format to send the email.

        My opinion is that you should set up an RSS feed to announce when you have specials. Or maybe send me a text message that says "You might want to visit this month's specials at http://....".

        Not HTML. HTML has no place in email.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        "...to keep everybody happy."

        Nope!

        All that extra html code has to get sucked thru the little bitty copper wire before I can see the text version. Please don't burden the bandwidth challenged with redundant and unwanted html email.

        ...and that's ignoring, for the moment, the myriad ways html content can be used for mischief.

Re: email output using HTML format.
by moot (Chaplain) on May 09, 2005 at 18:36 UTC
    You need a mail header for Content-type probably set to text/html, or preferably include your html along with plain text in a multi-part message. This tells the receiving MUA to render the mail in the most appropriate local format. For now something like the following might work:
    Content-type: text/html
    printed in the header section of your email. You will also want to throw an encoding in there.
      It now works but please explain or give an example of throwing an encoding in there.
Re: email output using HTML format.
by techra (Pilgrim) on May 09, 2005 at 19:24 UTC
    By far the easiest perl module I've found to perform HTML emails is Mail::SendEasy -- a very easy interface to developing these, that has made the process incredibly simple.
Re: email output using HTML format.
by Animator (Hermit) on May 09, 2005 at 18:33 UTC
    A good start would be to add something like 'Content-Type: text/html' right after the subject line (without the quotes )