How do I send html email from Perl?

Q. How Can I send html email from cgi perl program under UNIX / Linux and Apache web sever?

A. Sending HTML email is an easy task with MIME::Lite CPAN module. It is a low-calorie MIME generator.

It can be use to send html emails, graphics file email attachments as a single-part or multi-part message. Here is simple perl script that sends html email.

Install Perl MIME::Lite

If you do not have MIME::Lite module, install using following command (login as a root user):

# cpan -i MIME::Lite

Perl script code

Script to send html email from perl:

#!/usr/bin/perl -w use strict; use MIME::Lite; # SendTo email id my $email = ''; # create a new MIME Lite based email my $msg = MIME::Lite->new ( Subject => "HTML email test", From => '', To => $email, Type => 'text/html', Data => '<H1>Hello</H1><br>This is a test email. Please visit our site <a href="">online</a><hr>' ); $msg->send();

Save the script and execute it. Here is an email (sample) send by above script:

Read man page of MIME::Lite for more information:

perldoc MIME::Lite

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • Frank Wiles Jun 12, 2007 @ 20:33

    If you’re interested in sending HTML emails from Perl you might want to check out an article I wrote on MIME::Lite::TT and MIME::Lite::TT::HTML for sending Emails.

    By using the MIME::Lite::TT and/or MIME::Lite:TT::HTML modules you can take advantage of both the features of MIME::Lite, but also add in the ability to use Template Toolkit templates for your text/plain and text/html parts.

    Hope it helps!

  • Theunis May 24, 2009 @ 20:26

    Thanks Frank. Great tuturial.

  • Mike Aug 17, 2009 @ 5:24

    Hi , I have a problem when I send a long string used Data=>$content, $content included \r\n , It can not sent all the content of $content . could you please tell me what the problem is ?

  • vankinh Jan 29, 2010 @ 15:01

    test sendmail in linux.

  • Srini May 29, 2011 @ 19:15

    This helps me a lot and I am really thankfull

  • sanuda Mar 23, 2012 @ 1:12

    Really helpful for me as a Perl Developer . Thanks a lot for your article

  • Jan May 16, 2012 @ 13:35

    I’ve got this error message:
    SMTP Failed to connect to mail server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
    at C:/EclipseWorkspaces/csse120/Perl_testy/ line 29

    Here is code:

    use MIME::Lite;
    $user = '';
    $pass = 'xxxxxxxxxx';
    print "$user\n";
    print "$pass\n";
    MIME::Lite->send('smtp', '', 
    	AuthUser=>$user, AuthPass=>$pass, Timeout=>10);
    # AuthUser=>$user, AuthPass=>$pass);
    my $MailFrom = '';
    my $to_list  = '';
    my $cc_list  = '';
    my $subject  = "hello test";
    my $message  = "This email was generated automatically.";
    my $msg = MIME::Lite->new(
        From     => $MailFrom,
        To       => $to_list,
        Cc       => $cc_list,
        Subject  => $subject,
        Type     => 'TEXT',
        Encoding => '7bit',
        Data     => $message,
    print "I\'m sending message\.\.\.\n";
    print $msg;

    Could you help me please?


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum