Creating & sending HTML mail messages from a perl script

This is an example perl script I wrote that creates and sends an email message with HTML formatting in it to a list of email addresses. The addresses are read from a file specified on the command line. By the way, if you look at the source for this page, you'll that the xmp tag is used to keep the html code in the script below from being interpreted by your browser.

Note that MIME::Entity is required, and the Logo.gif file must be on a publicly accessible HTTP server in order for the recipient of the message to see it.

#!/usr/bin/perl -w # demonstrates the MIME::Entity module which can be used to send # multi-part MIME email messages (including html messages, I hope) use strict; use MIME::Entity; # create a message my $greeting = <<END; <body bgcolor="FFFFFF"> <img src="http://porrep/images/Logo.gif"><br> <hr> <i>Dearest Customer,</i> <p> In the interest of security, we want you to assign a PIN number to your account. By law, we cannot require you to do so, but the advantages of an account that requires a PIN to gain access over one that does not should be obvious. <p><b> Please go to <a href="http://www.company.com">www.company.com</a> to add a PIN number to your account.</b> If you have any questions, call our Customer Care Center at 1-800-HELPYOU. Thank you. <p> Sincerely, <p> Support Services </body> END open(FILE, "$ARGV[0]") or die "Can't open file $ARGV[0]\n"; while (<FILE>) { # create top-level entity my $msg = MIME::Entity->build( From => 'support@nextelpartners.com', To => "$_", Subject => 'Info about your Nextel phone', Type => 'multipart/mixed'); $msg->attach( Type => 'text/html', Encoding => '7bit', Data => $greeting); # send it off using smtp $msg->send('sendmail'); } close FILE; 09/14/2004