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 = 'user@somewhere.com'; # create a new MIME Lite based email my $msg = MIME::Lite->new ( Subject => "HTML email test", From => 'YOU@somewhere.com', To => $email, Type => 'text/html', Data => '<H1>Hello</H1><br>This is a test email. Please visit our site <a href="http://cyberciti.biz/">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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 7 comments… read them below or add one }
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!
Thanks Frank. Great tuturial.
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 ?
test sendmail in linux.
This helps me a lot and I am really thankfull
Really helpful for me as a Perl Developer . Thanks a lot for your article
Hi,
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/Tk_hello_world.pl line 29
Here is code:
use MIME::Lite; $user = 'aocmct@gmail.com'; $pass = 'xxxxxxxxxx'; print "$user\n"; print "$pass\n"; MIME::Lite->send('smtp', 'smtp.gmail.com', AuthUser=>$user, AuthPass=>$pass, Timeout=>10); # AuthUser=>$user, AuthPass=>$pass); my $MailFrom = 'aocmct@gmail.com'; my $to_list = 'jan.vicher@yahoo.com'; my $cc_list = 'ice809@seznam.cz'; 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; $msg->send();Could you help me please?
Jan