Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Still New to Perl / html email

by esolm (Acolyte)
on Apr 13, 2001 at 23:03 UTC ( #72443=perlquestion: print w/replies, xml ) Need Help??

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

Wise Perl Monks,

So in outlook and netscape mail clients you can send email with html formatting. How do you do this in perl. Currently I am using use Net::SMTP;

with code that looks like
my $smtp = Net::SMTP->new('xxxxx', Hello => ''); my $tm = scalar localtime; $smtp->mail(''); $smtp->to(''); $smtp->data(); # $smtp->datasend('To: ELI MARCUS', "\n"); $smtp->datasend('Subject: AUTO PERL EMAIL Database Facts', "\n"); $smtp->datasend("\n"); $smtp->datasend("This is an automated email providing you satistics", " on the US Full Push Database maintained by the Western", " Region Technical Consultants\n\nThis Data is valid as of $tm\n\n", "@list\n\n"); $smtp->dataend(); $smtp->quit; $sqlquery1 ->finish(); # $sqlquery ->finish(); $CDS->disconnect || die "failed to disconnect\n";
What would I have to do to get this into html format. Then when I build @list I could wrap the data in a table. Thanks for the help, Eli

Replies are listed 'Best First'.
Re: Still New to Perl / html email
by thabenksta (Pilgrim) on Apr 13, 2001 at 23:15 UTC

    You need MIME::Lite it allows you to send HTML mail, and Multipart mail so It send both HTML and text. It's pretty slick.

    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
Re: Still New to Perl / html email
by gryphon (Abbot) on Apr 14, 2001 at 00:19 UTC

    Greetings esolm,

    Sending HTML email is actually a fairly simple process. Getting that email to look good in most of the common email clients and viewers is another matter. First, let's start with the email itself. All you really need to send HTML email is a multi-part text email. To do this, just add a "Content-Type" reference in the header, then seperate the sections of the email with boundaries. Here's a very stripped down header example:

    Date: Fri, 13 Apr 2001 12:58:21 -0700 From: Gryphon Shafer <> Subject: HTML Email Content-Type: multipart/alternative; boundary="------------FE45F2D4BB7695EE89FC118F"

    Then wrap your message body with the boundary that you defined. (BTW, the boundary title/alias itself is just a bunch random numbers and letters. I heard somewhere that it should be different for each email you send, but I'm not sure that's valid.) Here's a brief example of the email message body:

    --------------FE45F2D4BB7695EE89FC118F Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit This is some text with a bolded word. This is a second line with an italicized word. --------------FE45F2D4BB7695EE89FC118F Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> This is some text with a <b>bolded</b> word. <br>This is a second line with an <i>italicized</i> word.</html> --------------FE45F2D4BB7695EE89FC118F--

    OK, so that gets you into the sending of the HTML email itself. As far as correct viewing on clients, that's another story. With regard to AOL, don't even bother with HTML other than adding links to full URLs. Anything else may not work between both the online email reading and client reading for AOL users. Netscape and Outlook users will be able to view just about any HTML email you send them. Avoid using background images, though, since they are almost always stupid. HotMail gets really upset when you split tags across lines. For example:

    <A href="something.html">text</A>

    This is perfectly reasonable HTML code, but it often doesn't work for HotMail users. The carriage return messes something up. So put all tags on a single line. Carriage returns outside of tags are fine, however. Also, add a few extra carriage returns at the end of both the text and HTML versions of the email since some clients mess up the end of the email when such CRs are not there.

    Good luck.


      Im still new to Perl too but I know that to send email formatted in HTML is quite simple. I am not familiar with the module stated but I would like to go lower level than that. First, its all about the MUA (mail user agent or email client) and how it will interpret your email message. There of course are standards out there that make your MUA do what you want it to do. Thus, there are extension headers you can place in your emails that the MUA interprets. If you want to set your message priority you do that using the X-Priority header. If you want to do HTML, you set the Mime-version and your Content-type. There are many other things you can set that your MUA will interpret and others that it will ignore but that can be seen for informational purposes. Here are a few:
      X-Mailer: MSN Explorer 6.00.0010.0912 X-OriginalArrivalTime: 23 Mar 2001 23:59:19.0685 (UTC) FILETIME=[4675A +B50:01C0B3F5] X-UIDL: 281293976 <-- set by the POP server
      I like to test things via command line before doing them in a script though. So, what I did to test this out was I actually sent a message out via command line.
      [ notjamesnjkwan ~ ] $ cat << msg | sendmail > Mime-Version: 1.1 > Content-type: text/html > Subject: This is a test for html > <font color=blue>Line in blue</font><br> > <font color=red>Line in red</font> > . > msg
      With sendmail you can basicly write some of your own headers which is what I did. After the headers were properly written for the MUA to interpret the HTML, I simply write the html. Thats it. In perl it should be the same way:
      open(PIPE,"| /usr/lib/sendmail $RCPT"); print PIPE "Mime-Version: 1.1\r\nContent-type: text/html\r\n"; print PIPE "Subject: $SUBJECT\r\n"; print PIPE "message here with HTML embedded\r\n.\r\n"; close(PIPE);
      I haven't actually tried the \r\n's in a single line yet but I see no reason why they shouldn't work :)

      - Jim

      Then wrap your message body with the boundary that you defined. (BTW, the boundary title/alias itself is just a bunch random numbers and letters. I heard somewhere that it should be different for each email you send, but I'm not sure that's valid.)

      The reason that the boundaries must be different is beacause it is possible for another MIME encoded message to be included in the original email. If this contains attachments of its own then you have to be able to distinguish which bounday markers belong to which email.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://72443]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2020-01-21 16:33 GMT
Find Nodes?
    Voting Booth?