About Linux FAQ

Browse More FAQs:

Sending mail with Perl mail script

Posted by Vivek Gite [Last updated: May 28, 2008]

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.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Frank Wiles Says:

    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

  2. javier villarca cao Says:

    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

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.