Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

How to send nicely formatted emails in Perl?

by dr.jekyllandme (Sexton)
on Sep 07, 2012 at 22:43 UTC ( #992398=perlquestion: print w/ replies, xml ) Need Help??
dr.jekyllandme has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have this task before me and I am not sure how to approach it, hoping that someone here could help me out. I have to generate a report and have to send it out by email to whomever is on the list of recipients. So at this point I have my data and now all I need to do is send it out. I will be doing this on a weekly basis and cron will handle running my script. The only thing is I need to add some format to the report such as making some keywords bold, adding some color, and creating tabular data. I realize that I may need to use HTML and CSS to do this. This is ok, but I am just not sure what module to use. My mail client is Outlook. If anyone can give me some examples and or suggestions on how to do this, it would be very helpful. Thank you for your time.

Comment on How to send nicely formatted emails in Perl?
Re: How to send nicely formatted emails in Perl?
by philiprbrenan (Monk) on Sep 07, 2012 at 22:58 UTC

    You do not say how frequently you are going to run this script. Oddly enough, the easiest approach may well be simply to write a file full of html, using all the standard html tags to format it as you wish, display this page in a web browser and then CTRL-A, CTRL-C, CTRL-V to copy the entire page into your gmail compose page. A similar sequence of actions to copy and past the recipients into the to: field.

    The alternatives are much harder work because email systems are keen to remove spam and so make it deliberately hard to write a simple script that will do the job. If you are not going to send this report out very often, you may never be able to recover the time spent on automating the sending reliably to make it worthwhile.

Re: How to send nicely formatted emails in Perl?
by Gangabass (Priest) on Sep 08, 2012 at 00:15 UTC

    Just create HTML (including images if you need) and send it with MIME::Lite

Re: How to send nicely formatted emails in Perl?
by Anonymous Monk on Sep 08, 2012 at 05:24 UTC


    Check out CPAN for mail modules, you'll finf loads. My favourite at the moment is Mail::Sender, but everyone has their own favourite. It is true that some ISPs/mail systems can be a bit awkward, but it's usually not too hard, especially if this is for your firm rather than just personal use. Ther will be a bit of a learning curve, but if you hit a real problem, come back with details and I'm sure it will be sorted.


      Also see
Re: How to send nicely formatted emails in Perl?
by dsheroh (Parson) on Sep 08, 2012 at 09:40 UTC
    Template::Toolkit to insert the data into an HTML template, MIME::Lite to email the result. Easy-peasy.

    For bonus points, you can create two separate templates, one for plain-text and the other for HTML, run both of them through TT, then use M::L to send it as a multipart email so that people who don't care about the color can see "just the facts, ma'am." Personally, I've never had issues with spam filters but, if you do, sending as multipart instead of HTML-only has the additional benefit of appearing to make the message look less spammy to most filter rulesets.

Re: How to send nicely formatted emails in Perl?
by zentara (Archbishop) on Sep 08, 2012 at 09:43 UTC
    Here is a pretty generic implementation of a program to mail html and text to a small list.
    #!/usr/bin/perl use warnings; use strict; use MIME::Lite; use Net::SMTP; my @keys = qw(email name street city zip phone); my %recipients; my @fields; my $email; while(<DATA>){ @fields=(); ($email,@fields) = chomp && split(/\Q|\E/); # print "$email -> @fields\n"; $recipients{$email} = \@fields; } foreach my $key (keys %recipients){print "$key -> $recipients{$key}\n" +}; #for(keys %recipients){ #print "$_ has ",scalar @{$recipients{$_}}," stations and they are:\n" +; #for(@{$recipients{$_}}){ #print <<INFO; #Name: $_->[0] #Email: $_->[1] #Street 1: $_->[2] #City: i $_->[3] #State: $_->[4] #Zip: $_->[5] #Phone: $_->[6] #INFO #} #} #=head1 my $fromaddress = ''; foreach my $recipient (keys %recipients){ my $smtp = Net::SMTP ->new(''); $smtp->mail('you@yourdomain'); #use your mail name $smtp->to('reciever@recieverdomain'); $smtp->data( ); my $msg = MIME::Lite->new( From =>'Joe', To =>'Shmoe', Subject =>'HTML Test', Type =>'multipart/related', ); $msg->attach( Type => 'text/plain', Data => 'HERE IS YOUR HTML PAGE', ); $msg->attach( Type => 'text/html', #Path =>'full_file_name', Path => $0, Disposition => 'attachment', ); # send the message over $smtp->datasend( $msg->as_string() ); # close smtp connection $smtp->dataend(); $smtp->quit; } #=cut __DATA__|Zboo P. Crumbcake|1234 Main Street|San Francisco|C +A|94124|123-4567|Zebar X. Zipwon|3456 Second Ave.|San Francisco|C +A|94131|999-1234|Xbaz A. Fule|9123 Lost Road|Oakland|CA|97541|43 +1-4312 zentara@localhost|Ybong A. Gong|3456 Wain Street|San Francisco|CA|9413 +1|999-1234

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-07-25 21:55 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (175 votes), past polls