Sending mail with Perl mail script

Q. How can I send an email with perl?

A. With the help of sendmail you can send an email from your web based feedback form. You will need to indicate the path to the mail program, Sendmail. Usually the path to Sendmail is /usr/sbin/sendmail. Here is sample script that can send an email to your account or to end user. Make sure you setup

ADVERTISEMENTS

$to=’MAIL ADDRESS TO SEND TO’;
$from= ‘EMAIL@MYOMAIN.COM’;
$subject=’YOUR SUBJECT’;

Here is sample script to sending an email using perl [ Download script ]:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
 
$title='Perl Mail demo';
$to='MAIL ADDRESS TO SEND TO';
$from= 'webmaster@YOURDOMAIN.COM';
$subject='YOUR SUBJECT';
 
open(MAIL, "|/usr/sbin/sendmail -t");
 
## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL "This is a test message from Cyberciti.biz! You can write your
mail body text here\n";
 
close(MAIL);
 
print "<html><head><title>$title</title></head>\n<body>\n\n";
 
## HTML content sent, let use know we sent an email
print "<h1>$title</h1><p>A message has been sent from $from to $to</p></body></html>";

Upload script Upload script to your web hosting cgi-bin directory. Then test is by typing url. For example http://mydomain.com/cgi-bin/sendmail.pl.

Caution: Notice that the subject line in the above script ends with two \n (new line) characters. These characters are required to separate the email header from the email body with a blank line. Do not remove them.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
26 comments… add one
  • Frank Wiles May 31, 2007 @ 16:44

    I’d suggest not using Sendmail directly, but instead using a module like MIME::Lite::TT or MIME::Lite::TT::HTML for lots of various reasons.

    I put together a small howto for using those modules at:
    http://www.revsys.com/writings/perl/sending-email-with-perl.html

  • javier villarca cao Sep 13, 2007 @ 14:33

    thanks for the perl code… It really helped me a lot. After experimenting on at least a dozen codes and techniques, your code, though simple as it is, made my routine work… Thanks again
    Javier

  • hamsinv Sep 16, 2008 @ 6:07

    Hello Vivck,

    Good one. Could you please clarify if this code compatible with Rad hat and SUSE.

  • 🐧 nixCraft Sep 16, 2008 @ 6:26

    Perl is cross platform scripting language i.e. it should work under any UNIX like operating system.

  • Abhishek Jan 8, 2009 @ 10:57

    Thanks a lot for this script.. i think this could be the simplest script..

    Thanks again :)

  • Vipin Feb 25, 2009 @ 12:21

    The code which is above is good I have one doubt in that How to send mail to multiple
    mail address

  • Austin Feb 26, 2009 @ 21:02

    Vipin

    I believe you just have add multiple emails with comma at the end of each address.

    $to = “,address2,address3,…..”

    Austin

  • Eric May 18, 2009 @ 21:21

    this should work great, but i am having trouble finding any email servers that would offer a cgi-bin for you to access…

    could you recommend any?

  • Rajiv Jul 3, 2009 @ 4:23

    This scripts is OK if you just need test how things work. If you need to send mails to a lot of people, then you need to look at many other aspects (correct headers, spf records etc.) so that your domain or IP does not end up in e-mail black lists.

    If you are a webmaster and plan to send large number of e-mails from scripts, you need to do a bit of a home work.

  • Abhijeet Aug 24, 2009 @ 21:08

    Sear sir i am not able to use the mail() of php of my yahoo small nusiness hosting kindly help

  • Daniel Oct 1, 2009 @ 22:41

    This is simplest, and most effective script for sending a simple mail. I wonder if it can expand to more for HTML, Attatchment, Reply etc…

  • NMarcu Oct 6, 2009 @ 9:48

    Hello all,

    Have somebody a perl script with postfix?

  • Sandip Oct 26, 2009 @ 11:32

    Can this be modified to work in Windows in an Outlook MS/Exchange environment? If so, how?

  • Nadeem Gul Nov 13, 2009 @ 0:39

    Its quite simple and handy.

    Can someone tell me how do to send attachment using this script?

    Thanks,

  • Srinidhi Jan 19, 2010 @ 11:07

    looks good…

    can you tell me if I can get a pearl script for loging on to a public mail server with my login ID and pass word and forward it to an other mail account?

  • Davidmoreen Mar 29, 2010 @ 4:07

    Great job, worked the first time!

    • Nisha Babu Jun 9, 2010 @ 15:59

      Could you please provide me a perl script to download a file from mail attachment.

      Thanks,
      Nisha

  • Soundar Dec 5, 2010 @ 7:35

    hi, how i define SMTP server addess ? basically i wanted to send a mail after success of my cron job with log.

  • priyanka Nov 23, 2011 @ 11:14

    can anyone help me out .. i m begineer with perl… i need to send a mail but getting errors related to smpt server n port number….
    my email id : priyankapagaria89@gmail.com

  • Umesh Apr 19, 2012 @ 6:25

    Thanks for script …

  • naresh prasad Aug 15, 2012 @ 11:53

    i am newer in scripting
    i want to write the pearl script that pings the ip address and it sended the mail if it is down

  • vani Oct 9, 2012 @ 11:13

    how to open a mail(as some user) in server using perl?

  • user Mar 28, 2014 @ 17:23

    You’re not understanding what sendmail is. sendmail is an MTA, like postfix, yes, but due to legacy naming conventions, sendmail is also the binary that’s used for delivering mail on most systems even if it’s not the same sendmail as the sendmail MTA. You can use Exim or Postfix or others and still the binary will be called sendmail.

  • VIN Feb 3, 2016 @ 7:29

    Thanks for the script. Anyway, from your code, how can i save the message of the email into a .txt file as an attachment to send together with the email?

    • Kartik Parihar Mar 28, 2016 @ 8:06

      Use perl MIME::Lite module.
      By using ->attach function you could send text file as a attachment

  • Nani Jul 21, 2016 @ 15:01

    Can anyone tell me…how to deactivate the hyperlink in sent email after 10days using perl script.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.