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

 

Getting the input from the command line

Let's finish by separating data from code, a good practice in any application.

The data in our code are the From and To e-mail addresses, the subject line and the content of the message. We will allow the user to provide all this on the command line of the program. 

We would like to allow the user to type the following, all on one line:

perl sendmail.pl
  --to  admin@i-programmer.info
  --from developer@i-programmer.info
  --subject "Thanks for posting"
  --text path/to/mail.txt
  --html path/to/mail.html

First of all we need to  load two new modules:

use Getopt::Long ('GetOptions');
use File::Slurp ('read_file');

These modules have procedural interfaces and this means that they simply export a number of functions that you can use - no objects, methods or properties just functions. As we want to make sure we only include the ones we really need, we list them (one in each case) after the name of the module - i.e. GetOptions and read_file.

We declare some variables but we only assign a value to one of them:

my ($to, $subject, $html, $text);
my ($text_file, $html_file);
my $from = 'I Programmer <name@szabgab.com>';

Next we call the GetOptions function:

GetOptions(
  'to=s'      => \$to,
  'from=s'    => \$from,
  'subject=s' => \$subject,
  'text=s'    => \$text_file,
  'html=s'    => \$html_file,
);

This will check the command line and look for the flag names we have specified in the function call as per our declarations on the left hand side of the arrows, it will look for to, from, subject, text and html flags.

The convention in Unix/Linux is that long flag names start with two dashes, so GetOptions is going to prepend those -- to the names. The =s at the end of each one of them means we are expecting some string value after the name of the option.

The right hand side of the => arrows are references to the scalar variables we already declared.

So for example when the user adds

--to some@address.com

on the command line, the GetOptions call will put 'some@address.com' in the $to variable. If the user adds

--text path/to/file.txt 

then that path will be assigned to $text_file and so on.

The GetOptions call does not require any of the flags to be present. That's why the are called options. It is your job to check if the values exist.

For simplicity I am checking all the parameters at once. This makes the error message less useful, but I am sure that, in a real world application, you would invest more energy in providing better error messages.

The last thing before we can send the e-mail is to read in the content of the text part and the html part from the respective files, making the following changes:

$text = read_file($text_file);
$html = read_file($html_file);

The full example is:

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

use MIME::Lite;
use Getopt::Long qw(GetOptions);
use File::Slurp qw(read_file);

my ($to, $subject, $html, $text);
my ($text_file, $html_file);
my $from = 'I Programmer <name@szabgab.com>';

GetOptions(
  'to=s'      => \$to,
  'from=s'    => \$from,
  'subject=s' => \$subject,
  'text=s'    => \$text_file,
  'html=s'    => \$html_file,
);

if (not $to
  or not $subject
  or not $text_file
  or not $html_file) {
   die "Missing parameter\n";
}

$text = read_file($text_file);
$html = read_file($html_file);

my $msg = MIME::Lite->new(
  From     => $from,
To => $to, Type => 'multipart/alternative', Subject => $subject, ); my $att_text = MIME::Lite->new( Type => 'text', Data => $text, Encoding => 'quoted-printable', ); $att_text->attr('content-type' => 'text/plain; charset=UTF-8'); $msg->attach($att_text); my $att_html = MIME::Lite->new( Type => 'text', Data => $html, Encoding => 'quoted-printable', ); $att_html->attr('content-type' => 'text/html; charset=UTF-8'); $msg->attach($att_html); $msg->send;

To see this software in action subscribe to the Perl Weekly newsletter - which is, of course, sent out using Perl.

Useful information

For detailed documentation of the modules used in this example visit their respective pages:

MIME::LITE https://metacpan.org/module/MIME::Lite File::Slurp https://metacpan.org/module/File::Slurp Getopt::Long https://metacpan.org/module/Getopt::Long

For further explanation of the Perl code, take a look at the my Perl Tutorial.

Perl says "Hello I-Programmer"

More Perl on I-Programmer

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter or Facebook or sign up for our weekly newsletter.



Last Updated ( Monday, 21 November 2011 )