How to Send Email in Perl Using MIME::Lite – Attachments, HTML & UTF-8 Examples
When you need automated alerts, logs, or HTML reports straight out of Perl scripts, the classic MIME::Lite module gets the job done in a dozen lines – handling plain text, HTML layouts, embedded CID images, and file attachments cleanly.

Below are tested examples for everyday server tasks, including proper UTF-8 Base64 subject encoding and a real cron script mailing proxy stats.
1. Simple Plain-Text UTF-8 Email
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite;
my $msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Subject => 'Server notice',
Type => 'text/plain; charset=UTF-8',
Data => "Hello from Perl!\n",
);
$msg->send();
2. Email with a File Attachment
MIME::Lite builds multipart emails step by step via attach() – declare multipart/mixed first, then attach your text body and files:
#!/usr/bin/perl
use MIME::Lite;
my $msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Subject => 'Report with attachment',
Type => 'multipart/mixed',
);
$msg->attach(
Type => 'text/plain; charset=UTF-8',
Data => "Please find the attached file.\n",
);
$msg->attach(
Type => 'image/gif',
Path => 'mylogo.gif',
Filename => 'logo.gif',
Disposition => 'attachment',
);
$msg->send();
3. HTML Email with Inline Image (CID), ZIP Attachment & UTF-8 Subject
To keep non-ASCII subjects readable across mail clients, encode the subject using MIME::Base64 per RFC 2047 (=?UTF-8?B?...?=). To embed an image inside the HTML body, give it an Id and reference it via cid::
#!/usr/bin/perl
use MIME::Lite;
use MIME::Base64;
my $subj = '=?UTF-8?B?' . MIME::Base64::encode('Monthly Report', '') . '?=';
my $msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Subject => $subj,
Type => 'multipart/mixed',
);
$msg->attach(
Type => 'text/html; charset=UTF-8',
Data => "<body><h1>Report</h1><img src=\"cid:logo.gif\"/></body>\n",
);
$msg->attach(
Type => 'image/gif',
Path => 'mylogo.gif',
Filename => 'logo.gif',
Id => 'logo.gif',
Disposition => 'inline',
);
$msg->attach(
Type => 'application/zip',
Path => 'price.zip',
Filename => 'price.zip',
Disposition => 'attachment',
);
$msg->send();
4. Real-World Cron Script: Emailing a Squid HTML Report
Remember to use double quotes ("...") whenever interpolating Perl variables inside headers or body text:
#!/usr/bin/perl
use MIME::Lite;
my $stat_log = '/var/log/squid/squid_stat.html';
my ($sec, $min, $hour, $day, $mon, $year) = localtime();
my $stamp = sprintf('%02d.%02d.%04d %02d:%02d', $day, $mon + 1, $year + 1900, $hour, $min);
open(my $fh, '<', $stat_log) or die $!;
my @stat_data = <$fh>;
close($fh);
my $msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Subject => "Squid Stats $stamp from ProxyServer",
Type => 'text/html; charset=UTF-8',
Data => join('', @stat_data),
);
$msg->send();
Modern Perl note: full docs remain on MetaCPAN; for brand-new projects needing authenticated TLS/SMTP, Perl maintainers recommend Email::Stuffer / Email::Sender.
Still, when maintaining existing Linux servers and quick cron jobs, MIME::Lite remains hard to beat for sheer readability.