How to send plain text or HTML e-mail using Perl
Written by Gábor Szabó   
Monday, 21 November 2011
Article Index
How to send plain text or HTML e-mail using Perl
Sending HTML and Text mail together
Separating data from code

Sending emails to a list of over two thousand subscribers is a daunting task - especially when some people want HTML emails and others prefer plain text. Here's Gábor Szabó's solution to the problem in Perl.

I have been running the Perl Weekly Newsletter for more than three months now and during that time I've had to change the way I am sending the e-mail messages several times.

At first I prepared a script to send out simple text files. Then I thought it would be much nicer for it to be in color, so before the launch I switched to sending HTML messages.

That worked well, but after a while several people told me they'd prefer to read plain text and not HTML. At that point I had to further improve the script to be able to send out a message that contains both the HTML version and the text version of the content.

There are lots of other cases when you might want to send e-mail programmatically.

As a system administrator I had several cases when I had to create a scheduled job (aka a cron-job in the Unix world) that would collect some information and then send a report in e-mail. Sometimes these reports were plain text files. In other cases they had a few Excel files attached to them.

In this article I am going to show several examples of how to send out e-mails in various formats. We'll go step by step.

First simple example

#!/usr/bin/perl  
use strict;
use warnings;

use MIME::Lite;

my $from = 'ip-reader@szabgab.com';

my $to = 'iprogrammer@szabgab.com';
my $subject = "I am a Perl Programmer";
my $content = <<"END_TEXT";

Hello,
this is a message from
an I-Programmer reader

END_TEXT

my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $content,
);

$msg->send;


The above example will send a simple text message that was constructed inside the script.

Let's go over the code:

#!/usr/bin/perl  
use strict;
use warnings;

The first line is the sh-bang line.It is mostly important on Linux/Unix machines but I keep adding it for when I would like to turn this script into an executable.

The next two lines are the safety net that I discussed in my introductory article to Perl.

use MIME::Lite;

This loads the MIME::Lite module, that provides e-mail sending faciliies, into memory. 

In the following four lines we declare some scalar variables:

my $from = 'ip-reader@szabgab.com';
my $to = 'iprogrammer@szabgab.com';
my $subject = "I am a Perl Programmer";
my $content = <<"END_TEXT";

Hello, this is a message from an
I-Programmer reader END_TEXT

The first two are the addresses that should appear in the From: and To: fields in the e-mail and the third the text to appear in the subject line. 

In the assignment of the fourth variable note the use of a "here-document" which is a nice way to create a multi-line string and embed it in the code. The way this works is that all of the following text is treated as data up to and not including the ending token - which is END_TEXT in this case. The ending token can be anything you like but you have to specify it as in

<<"END_TEXT";

Obviously you have to pick a suitable piece of text that isn't going to occur within the text you are trying to assign to the variable. I chose to use the string "END_TEXT". From that point till that token shows up at all the lines of text are assigned to the $content variable.

An important note: the token must start at the beginning of the line and there should be no trailing spaces.

Next we use the MIME::Lite module to create a mesage:

  my $msg = MIME::Lite->new(
  	From     => $from,
  	To       => $to,
  	Subject  => $subject,
  	Data     => $content,
  );

The MIME::Lite module works in an Object-Oriented way. Some Perl modules have a procedural API, others have an OO API and it is important to know how both work.

The arrow -> is the way that we call methods that belong to an object.  In this case we call the "new" method on the "MIME::Lite" class, which happens to be the constructor of this class. We pass a few key-value pairs to the constructor to customize the object. The left-hand side are the keys, the right-hand side the values. Having created the MIME::Lite object, we assign it to the $msg scalar variable.

The only remaining thing we need is to call the send method of the newly created object:

    $msg->send;

On Unix/Linux machines this will use the local mail delivery system to send the message. If it is not configured, or if you are using an operating system that does not have default system, you can tell the send method to use a specific SMTP server. This is the same server that you probably have configured in your e-mai client.

Most ISPs provide an e-mail box and in order to send e-mail they provide the details of the SMTP server as well. If this is the case you can replace the send call by this:

$msg->send('smtp', 
'smtp.myisp.com', Debug => 0);

If this does not work either, you can try putting 1 instead of 0 in the Debug parameter to see some debugging output.

You can use the above script to send me an e-mail but please change the From address to your correct address. You can even include your real name like this:

my $from = 
'I Programmer <username@szabgab.com>';



Last Updated ( Monday, 21 November 2011 )