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 HTML and Text mail together

In the following example we'll see how can we send a message that has both an HTML and a text part:

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

use MIME::Lite;

my $from = 'I Programmer <user@szabgab.com>';
my $subject = 'I am a Perl programmer';
my $to = 'iprogrammer@szabgab.com';
my $html = <<"END_HTML";
<a href="http://www.i-programmer.info/">
 I-Programmer
</a>
END_HTML

my $text = <<"END_TEXT";
We can only have text links here
http://www.i-programmer.info/
END_TEXT

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;

The beginning of this program is the same as before, but this time we create two variables again using the here document mechanism. One is HTML we want to send and the other one is the plain text.

The constructor is now called in a slightly different way:

my $msg = MIME::Lite->new(
From => $from,
To => $to, Type => 'multipart/alternative',
Subject => $subject, );

We don't supply a Data part and hence the main body of our message will be empty. Instead we supply a Type key and give it 'multipart/alternative' so the receiving e-mail client will know the message has several parts.

Next we create two attachments:

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);

The first one is the text part of the email. We create a new MIME::Lite object but instead of giving an e-mail address etc. this is used to hold the text content. The second statement sets the 'content-type' attribute of the attachment to 'text/plain'. The third statement attaches the newly created attachment to the message.

Attaching the HTML part is very similar. The only difference is in the content-type' which is set to be 'text/html'. Once we have attached both parts we can call the send method and that will send the e-mail to its destination.

Installing Perl Modules from CPAN

So far we have been using the MIME:Lite module as if it was installed but this might well not be the case.

If you followed my recommendation in the previous article,then you'll already have a Strawberry Perl derivative, i.e. Padre, in which case it's going to be quite easy to install the MIME:Lite module we need for sending email.

Open a command prompt using Start/Run/cmd and when you see the prompt type in:

cpan MIME::Lite

This will download the module and all its dependencies. At first it might ask you if you want to prepend some dependencies. Just press ENTER. It will work it out.

On Ubuntu Linux you can install the module from the repository of Ubuntu itself using the following command:

sudo aptitude install libmime-lite-perl

I have not checked them all but I am almost sure the other major Linux distributions also carry this module so you can find it with the package management system of that operating system.

Sending an Excel file as an attachment

In another article I'll explain how to generate Excel files using Perl, but for now I'd like to show how to send such file as an attachment.

This is all the additional code you need:

my $file = "C:/path/to/file.xls";

$msg->attach(
  Type => 'application/octet-stream',
  Path => $file,
  Filename =>  'report.xls',
  Disposition => 'attachment',
);

Path has to be set to the complete path, including the file name, to the file on your hard disk while "Filename" is just the name that will be displayed to the recipient of the e-mail message. Note that they don't have to be the same. Adding attachments is this easy.



Last Updated ( Monday, 21 November 2011 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.