Perl Scripts ·
How to Send HTML Email from Perl
By Manu Mathew, Technical Lead Engineer · Published 25 September 2013 · Last updated 5 October 2026

Contact forms, order confirmations and password resets all end with a script sending an email. Older Perl scripts often did it with Mail::Sendmail and the web server’s own mail relay. That still runs, but mail sent that way is easy for receiving servers to distrust, and it often lands in spam.
This note shows how to send HTML email from Perl today, with code I tested on Perl 5.40. It replaces the short CGI example that used to be on this page.
Quick answer
Use Email::Stuffer with Email::Sender’s SMTP transport. Connect to your mail provider’s SMTP server on port 587 with STARTTLS and a username and password, send a plain-text part as well as the HTML, and wrap the send in eval so a failure is logged instead of breaking the page. Keep passwords out of the code.
What do I need before sending email from Perl?
You need an SMTP account, two CPAN modules and a sender address on your own domain.
- An SMTP account from your email provider or a transactional email service, with its host name, port, username and password.
- Email::Stuffer and Email::Sender from CPAN. Install them with your system’s packages or with
cpanm Email::Stuffer Email::Sender. - A from address on your domain, with SPF and DKIM set up. These are DNS records that tell receiving servers your provider may send mail for that domain.
How do I send an HTML email with Email::Stuffer?
Create an SMTP transport, build the message with a text body and an HTML body, then call send_or_die inside eval.
use strict;
use warnings;
use Email::Stuffer;
use Email::Sender::Transport::SMTP;
my $transport = Email::Sender::Transport::SMTP->new(
host => 'smtp.example.com',
port => 587,
ssl => 'starttls',
sasl_username => $ENV{SMTP_USER},
sasl_password => $ENV{SMTP_PASS},
);
my $html = <<'HTML';
<p>Hello,</p>
<p>Thank you for registering. Your account is ready.</p>
HTML
my $email = Email::Stuffer
->from('Website <no-reply@example.com>')
->to('customer@example.com')
->subject('Your registration')
->text_body("Hello,\n\nThank you for registering. Your account is ready.\n")
->html_body($html)
->transport($transport);
my $sent = eval { $email->send_or_die; 1 };
warn "Email failed: $@" unless $sent;
Giving both text_body and html_body produces a multipart/alternative message: the reader’s email app shows the HTML, and falls back to the text when it can’t. The username and password come from environment variables, so they never sit in the source code or in version control.
How can I test sending without sending real mail?
Swap in Email::Sender’s test transport, which keeps messages in memory so a script or test can check them.
use strict;
use warnings;
use Email::Stuffer;
use Email::Sender::Transport::Test;
my $test = Email::Sender::Transport::Test->new;
Email::Stuffer
->from('Website <no-reply@example.com>')
->to('customer@example.com')
->subject('Your registration')
->text_body("Hello,\n\nThank you for registering.\n")
->html_body('<p>Hello,</p><p>Thank you for registering.</p>')
->transport($test)
->send_or_die;
my ($delivery) = $test->deliveries;
my $mail = $delivery->{email}->as_string;
print "sent to: @{ $delivery->{successes} }\n";
print "multipart/alternative: ", ($mail =~ m{multipart/alternative} ? "yes" : "no"), "\n";
print "has text and html parts: ", (($mail =~ m{text/plain} && $mail =~ m{text/html}) ? "yes" : "no"), "\n";
sent to: customer@example.com
multipart/alternative: yes
has text and html parts: yes
This is how I check email code before it goes near a live mail server: the message is built exactly as it would be, but nothing leaves the machine.
What was wrong with the older Mail::Sendmail example?
Nothing stopped it from running, but several habits in it are worth dropping:
- No authentication. It handed mail to a relay on
localhost, which many hosts no longer provide and receiving servers often distrust. - HTML only. Without a plain-text part, some email apps show nothing useful and spam filters score the message lower.
- Hard-coded addresses in the script, and two mail modules (Mail::Sendmail and Mail::Sender) loaded although only one was used.
- A JavaScript alert for feedback. A normal confirmation page is clearer, and works without JavaScript.
- Old style:
use varsandnew CGI; see Perl CGI.pm basics for the current forms.
How do I send email from a web form safely?
Treat every value from the form as untrusted, and keep it out of the email headers.
- Validate the visitor’s address before using it, and send to a fixed address of your own rather than one taken from the form.
- Never put form input in headers such as To, Cc or Subject without removing line breaks; otherwise a visitor can add extra recipients (header injection).
- Limit how often one visitor can send, so the form can’t be used to flood an inbox.
- Log failures with the reason, and show the visitor a calm message asking them to try again or email directly.
Frequently asked questions
Can I still use Mail::Sendmail?
It can still send simple messages. For new code, Email::Stuffer with Email::Sender is easier to set up with the encrypted, authenticated SMTP connections most mail providers now require, and it builds proper multipart messages with a plain-text and an HTML part for you.
Why does my email from Perl go to spam?
Usually because the sending server isn’t authorised to send for your domain. Send through your email provider’s SMTP server rather than the web server itself, and publish SPF, DKIM and DMARC records for the domain. A plain-text part and a clear subject line help too.
Should I use port 587 or 465?
Whichever your provider documents. Port 587 starts unencrypted and switches to TLS with STARTTLS (ssl => 'starttls'); port 465 uses TLS from the start (ssl => 'ssl'). Avoid port 25 from web servers, which many hosts block for outgoing mail.
How do I add an attachment?
Call ->attach_file('/path/to/report.pdf') on the Email::Stuffer object before sending. It works out the content type from the file name and encodes the file for you. Keep attachments small, and send a download link instead for large files.
Related notes: Perl CGI.pm basics; small PHP snippets; checking whether a Perl module is installed.
Related reading: Perl files and folders (including sending a file as a download); Perl strings and regex; hiring a remote Perl developer.
Need help with Perl code?
If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.
You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.
An earlier version of this page was a short 2013 script that sent HTML email with Mail::Sendmail through a local relay. This version replaces it with current advice and tested code.
Sources: Email::Stuffer; Email::Sender::Transport::SMTP; Email::Sender quick start.