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.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 05/28/08



{ 15 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.
Sear sir i am not able to use the mail() of php of my yahoo small nusiness hosting kindly help
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…
Hello all,
Have somebody a perl script with postfix?
Can this be modified to work in Windows in an Outlook MS/Exchange environment? If so, how?
Its quite simple and handy.
Can someone tell me how do to send attachment using this script?
Thanks,
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?