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

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

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 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 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
26 comments… add one
  • 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. Required fields are marked *

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