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
$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> A message has been sent from $from to $to </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.
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 5-28-08

{ 9 comments… read them below or add one }
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
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
Hello Vivck,
Good one. Could you please clarify if this code compatible with Rad hat and SUSE.
Perl is cross platform scripting language i.e. it should work under any UNIX like operating system.
Thanks a lot for this script.. i think this could be the simplest script..
Thanks again :)
The code which is above is good I have one doubt in that How to send mail to multiple
mail address
Vipin
I believe you just have add multiple emails with comma at the end of each address.
$to = “,address2,address3,…..”
Austin
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?
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.