Sending mail with Perl mail script

last updated in Categories , , , , , , , ,

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


$subject=’YOUR SUBJECT’;

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

print "Content-type: text/html\n\n";
$title='Perl Mail demo';
$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! You can write your
mail body text here\n";
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

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.


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

26 comment

  1. 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

  2. Vipin

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

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


  3. 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?

  4. 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.

  5. 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…

  6. 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?

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


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

  8. 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

  9. 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.

  10. 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?

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

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

    Still, have a question? Get help on our forum!