Perlfect Solutions

Sending email using perl and sendmail.

A very common task for a cgi script is to be able to inform a set of users with data generated by itself or other programs, cgi's or not. For example, you might be one of the web designers who have joined one of the myriad of free counter programs on the internet that email you with nice statistics and reports about your web pages' traffic. Systems like that are responsible for informing such a large number subscribers that sending the reports manually would require a full-time employee devoted to this task only. Obviously this wouldn't be a sensible option even for a relatively large organization.

The way to automate this task is to let a perl program do those tedious bits of work for you. In this article we will build a perl script which does exactly that. We are going to go step by step giving explanations and analyzing the tricky parts.

Perl, being perl, provides the programmer with more than one ways to do same thing, sending email included. In this script we are going to use sendmail. Sendmail, is an open source program used on most unix computers and some nt workstations as well. Sendmail as its name implies has the ability to send email! We are going to use perl's ability to open pipes to programs to run sendmail and feed it with input. If you are not familiar with sendmail it doesn't really matter though; you should just understand that sendmail is able to send an email, with its headers and content, to your mail gateway which will in turn forward it to its recipient(s).

Here is a very simple program that emails a confirmation to a user that his/her request to subscribe to a newsletter has been accepted:

#!/usr/bin/perl -w use strict; use CGI; use Email::Valid; my $query = new CGI; # it is important to check the validity of the email address # supplied by the user both to catch genuine (mis-)typing errors # but also to avoid exploitation by malicious users who could # pass arbitrary strings to sendmail through the "send_to" # CGI parameter - including whole email messages unless (Email::Valid->address($query->param('send_to'))) { print $query->header; print "You supplied an invalid email address."; exit; } my $sendmail = "/usr/sbin/sendmail -t"; my $reply_to = "Reply-to: foo\@bar.org\n"; my $subject = "Subject: Confirmation of your submission\n"; my $content = "Thanks for your submission."; my $to = $query->param('send_to')."\n"; my $file = "subscribers.txt"; unless ($to) { print $query->header; print "Please fill in your email and try again"; } open (FILE, ">>$file") or die "Cannot open $file: $!"; print $to,"\n"; close(FILE); my $send_to = "To: ".$query->param('send_to'); open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; print SENDMAIL $reply_to; print SENDMAIL $subject; print SENDMAIL $send_to; print SENDMAIL "Content-type: text/plain\n\n"; print SENDMAIL $content; close(SENDMAIL); print $query->header; print "Confirmation of your submission will be emailed to you.";

A note about security

Before attempting to explain how the script works here is an important security note: always validate user supplied input. In the case of our CGI mailer the "send_to" parameter comes from a user submitted form and hence could be exploited by a malicious party to pass arbitrary arguments to the sendmail program. To avoid this hazard we utilize the Email::Address module from CPAN to check the conformance of the supplied email address. If the address is invalid - because of a genuine typing error or an exploitation attempt - we return an error message. Otherwise, we proceed with emailing the confirmation using the technique described in the rest of this article.

How the script works

At first glance you can notice that this a relatively small program which if it wasn't that verbose would be even smaller. Looking through it you will also see that it is very simple to understand even for the Perl beginner; however it more than fullfils the task of sending email.

Let's have a look at it line by line... The cgi script takes its input from a web form. This hypothetical form consists one text input field:

<FORM method="POST" action="http://perlfect.com/cgi-perlfect/cgimail.pl"> <INPUT type="text" name="send_to"> <INPUT type="submit"> </FORM>

The script uses the CGI.pm module to parse the form data. If you are not familiar with that module I suggest that you read and learn about it as it will make you life as a scripter a lot happier. The param() function provided by CGI.pm returns the value of a form field given its name as an argument and that's all you need to know for now; hence we use it in our script to find out what the user has entered in the text box. If the user has not entered anything the script returns an error message prompting the user to try again after filling in the appropriate text field.

If the user has entered an email address this is appended to a text file for later use by another program and then the script procedes to return a confirmation email to the user.

An email message consists of some headers and the content. There are many standard headers but the ones you will most commonly encounter and the one we use here are:

To: A comma separated list of recipient addresses.
From: The email address of the sender.
Reply-to: The email address to whic replies should be sent.
Subject: The subject of the message.
Content-type: The MIME type of the content.

The headers precede the content of the message. The content type header is written just before the content and is followed by two newline characters.

Sendmail has the ability, as most unix programs, to read from standard input hence all we need to do is a open a pipe to it and provide it with the input we want it to process. You will notice that we have given the -t option to sendmail. This merely tells sendmail to scan the message for a To:, Cc: or Bcc: header and extract the list of recipients from there. Having opened the pipe succesfully we print the message to it. First the headers, each one followed by a newline character, the a newline by itself and finally the content of the message. Finally we close the pipe. The email has been succesfully sent!

Here is a list of useful things you can do by using sendmail and perl:

  1. Inform visitors of your site that have asked, that your site has been updated. The script used as an example here would be a good way to collect the addresses of the people you want to email.
  2. Inform yourself of the way your scripts are running. For example you can write a few lines of code that email you when something goes wrong in a script that you 've written.
  3. Create an online mailing list.

These are only some of the things you can do, but there is one thing you shouldn't do, except if you are really nasty. That is, do not spam people. Never email people that have not asked for the information you are providing as it will probably make them angry and in the future they will ignore any that corespondence from you. Have fun and be polite!

Online Documentation/Tutorials

  • Your sendmail program's man pages will provide more detailed info about sending mail.
  • Computing Securely, a collection of security tips from Randal L. Schwatz.
  • You might want to have a look at the documentation of the Mail:: modules available at CPAN. There are also many other modules for sending and processing mail there.

Comments

Your name:
Your comments:

Security check *

 

Anonymous   

Posted at 2:29pm on Thursday, March 15th, 2007

awesome site

srikumar   

Posted at 11:47pm on Wednesday, March 21st, 2007

This information was really useful. As i am a beginner of perl script, i was able to get the full information in a more easy way.

Luthra   

Posted at 6:32am on Thursday, March 22nd, 2007

Thanks for the help!

Anandhakumar   

Posted at 6:56am on Thursday, March 22nd, 2007

I m new to perl even i can understand this article.Thanks a lot....

Anonymous   

Posted at 6:30am on Friday, March 30th, 2007

hi there,
i want to find a way to eliminate the bad urls which is in the form of binary format from a set of URL's ... can anyone suggest a way to do that using perl script?

zino   

Posted at 5:45am on Saturday, March 31st, 2007

thank you for your short tutorial.
can you please recommend books for me. picuroil@yahoo.ie
Best Regards,
Zino.

amit   

Posted at 4:51am on Thursday, April 5th, 2007

this is good

IgorManchofsky   

Posted at 10:47pm on Thursday, April 5th, 2007

How secure is it to use this?

Ajay   

Posted at 11:52pm on Thursday, April 12th, 2007

Nice script.It reduce my head ache :) now i can use this script as template for all projects. Thanks

Lokanath Reddy   

Posted at 2:09am on Friday, April 13th, 2007

Nice Site....

cg-i   

Posted at 11:05am on Saturday, April 14th, 2007

very nice cgi script

Norvin Whitney   

Posted at 10:11am on Thursday, April 19th, 2007

How to attach a zip file to this
Cheers

vishnu   

Posted at 12:45am on Sunday, May 6th, 2007

Please check out this site.

http://www.youtube.com/watch?v=5P6UU6m3cqk

when you this site and

reply your name,telphone,email.

Your article is very very good.

Keep Smileing .........

Prashanth   

Posted at 11:50am on Monday, May 14th, 2007

I am beginner in perl and found it very useful. Thanks

monkey   

Posted at 11:01pm on Tuesday, May 29th, 2007

Look at the 3rd print to SENDMAIL - it won't have the "To:".
And how about foo@bar.org - @bar will try to be resolved.
Put a "use strict;" as the 2nd line of code...
Add "-w" on the perl binary path, or "use warnings;"

Frank Wiles   

Posted at 9:39am on Thursday, May 31st, 2007

I've been using the MIME::Lite:TT and MIME::Lite::TT::HTML perl modules recently for sending E-mail. I love them, less coding, ability to use templates for your messages.

I even wrote a small howto article on the subject, which you can view at:
http://www.revsys.com/writings/perl/sending-email-with-perl.html

MK   

Posted at 2:40pm on Wednesday, June 6th, 2007

Seems like it is definitely for beginners who like to use file handles for i/o or server communication. Would be more interesting if appropriate CPAN modules would be listed. This is beginner-level.

Rajeev NR   

Posted at 3:32am on Friday, June 15th, 2007

wait

Digitalpbk   

Posted at 7:17pm on Sunday, June 17th, 2007

thanx
http://digitalpbk.blogspot.com

Roshan K Joy,SATP,SERC,IISc Bangalore   

Posted at 9:49pm on Sunday, June 17th, 2007

thanx for a cute code like this whicj would give me a smile from my Boss

Randal L. Schwartz   

Posted at 8:06pm on Thursday, June 28th, 2007

This code, if installed, allows arbitrary spam to be sent from your domain, and YOU will get the blame and YOU will get the blacklist entry.

DO NOT USE THIS CODE. Get some clues. {sigh}

Please read my http://www.stonehenge.com/merlyn/UnixReview/col48.html

Perlfect   

Posted at 7:38pm on Friday, June 29th, 2007

Good point Randal, the code and the article have been updated.

Ashok   

Posted at 6:11am on Friday, July 13th, 2007

This is very Good!.. Keep going..

Jeff   

Posted at 12:26pm on Sunday, July 15th, 2007

Yet more rubbish code which gives Perl a bad name. Thanks for proving any fool who can read a script to distribute spam. No use strict; use warnings; ? Stupidity

Dirsh-   

Posted at 5:46pm on Monday, July 16th, 2007

Can someone help me with a project. I guess first I am just curious if its even possible... Here is the run down.

The scope of this project is to create a script that would automate an email response using Outlook 2007 / 2003. This script will produce three automated emails, the senders information is pulled from an excel list that is manually maintained with various information; 3 of which will be constant ... [email address], [first and last name], and [date of the inquisition]. I believe these are the only constants needed for this ... but I could be very wrong.

When a prospect inquires about XYZ their [email address], [first and last name], and [date of the inquisition] is acquired and stored in an excel spreadsheet. I would like to develop a script that would run as a scheduled task and perform these things:

1) Send a pre-drafted email 2 days after the inquisition date on the spread sheet.

2) Send a different pre-drafted email 2 weeks after the inquisition date.

3) Send a third pre-drafted email 4 weeks from the inquisition date.

*The emails would be merged with the information from spreadsheet to attain the first name making it personalized*

*Being that the script would run as a scheduled task everyday. The script would base all calculation of dates on the day the script was run.*

Bob Roske   

Posted at 11:33pm on Sunday, August 5th, 2007

Use the year plus the Julian date and store two days out, 14 days out and 28 days out in 3 fields. If the computed days are > 365 subtract 365 and increment the year. Read the fields and send the appropriate email based on which field matches.

yuvraj   

Posted at 5:07am on Wednesday, August 8th, 2007

wen i m checking the status of send mail it shows running but when i send mail through the mail function of php then it gives a error. and shows unrecognise host name.

srk   

Posted at 2:38am on Friday, August 31st, 2007

Hi,

I am using Mail::Sendmail in Perl to send mail. But I am getting an error message "RCPT TO: error (550 5.7.1 unable to relay for xxx@xxx.xx)"... My SMTP is running. I guess it is an SMTP error. Can any one solve this problem??

Thanx...

bagel   

Posted at 1:17pm on Wednesday, October 3rd, 2007

I have been told that the perl script needs to send HTTP headers before the email message. How do you do this?

asaverus   

Posted at 5:21pm on Thursday, November 8th, 2007

Thanx, elegant and clear script. It's great when something runs first time eh.

torkel   

Posted at 4:12pm on Wednesday, December 12th, 2007

You should check the return value of close(SENDMAIL). I think it contains the exit status of sendmail.

orina   

Posted at 2:01am on Wednesday, January 9th, 2008

i amb using Mail::CheckUser instead of Email::Valid. Is this the same?

praveen   

Posted at 8:58am on Monday, February 4th, 2008

this is good

Puneet   

Posted at 10:19am on Wednesday, February 27th, 2008

This is well explained

Charlie   

Posted at 8:10pm on Friday, March 7th, 2008

Hey, whiners, what did you think you were getting when you googled this address? Be nice to the people that get you started. Yeah, yeah, use strict. use politeness; use common::decency.

Steve Ki   

Posted at 6:29pm on Wednesday, March 12th, 2008

How can I attach a zip file to above example

Anonymous   

Posted at 6:48am on Wednesday, March 19th, 2008

In the script, the "print" statement that prints
$to to FILE appears to be missing the FILE arg, i.e.

print FILE $to,"n";

right or wrong?

Borys Marcelo   

Posted at 12:37pm on Friday, May 2nd, 2008

Hi, here is a simple version of the script above for those like me that only need to send email without using CGI:

my $EMAIL_BODY="MY EMAIL BODY";
my $EMAIL_TO="foo@bar.org";
my $sendmail = "/usr/sbin/sendmail -t -v";
my $reply_to = "Reply-to: borysmbn";
my $from = "From: borysmbn";
my $SUBJECT_AUX="Subject: Your Subjectn";
my $send_to = "To: ".$EMAIL_TO."n";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL $send_to;
print SENDMAIL $from;
print SENDMAIL $reply_to;
print SENDMAIL $SUBJECT_AUX;
print SENDMAIL "Content-type: text/plainnn";
print SENDMAIL $EMAIL_BODY;
close(SENDMAIL);

awesome tutorial, BTW.

A tip is not to forget about the parameters at the beginning of each parameter to send, like in the var $send_to="To: ".$EMAIL_TO."n" we cannot forget about the "To:" at the beginning and about the "n" at the end as you are issuing commands, or you will end up with an error message.

Regards

Borys Marcelo   

Posted at 12:40pm on Friday, May 2nd, 2008

in the above comment, when you read $send_to="To: ".$EMAIL_TO."n" we cannot forget about the "To:" at the beginning and about the "n" means "n" (new line) instead of "n". I can't edit, I'm posting again,

Regards

Rimic   

Posted at 1:02am on Monday, May 5th, 2008

I am not a developer and i am just a student. I need some help on how attach a word document file and image file on form to be sent into an email. CGI or Perl script with html code will help me a lot.

Sankar M,SERC,IISc,bangalore   

Posted at 9:11am on Tuesday, June 3rd, 2008

Really This is very easy way to send mail. nice code.It works. Cheers,
San

tiny teens   

Posted at 8:38am on Saturday, June 7th, 2008

It's a pleasant surprise to find a sanctury from all that modern inane garbage they call music.

Armin Garcia   

Posted at 10:08am on Wednesday, June 11th, 2008

Hi !!!
Does anybody knows how i attach a bzip file using perl????

I use
Mail::Internet
Email:Folder

modules, i hope anybody help me.... thnaks :)

jay   

Posted at 10:07am on Monday, June 23rd, 2008

nice

shardiwal   

Posted at 11:14pm on Thursday, July 10th, 2008

Y don't you use Mime::Lite, It's very easy to use and implement.

Dharani   

Posted at 12:09am on Saturday, July 12th, 2008

I feel very happy after reading this fourms in our site.

sujitha   

Posted at 11:46pm on Sunday, August 3rd, 2008

how to configure sendmail.how to enable imap and pop3 services.using sendmail mails are not sending and receiving

lenny   

Posted at 1:13pm on Thursday, August 7th, 2008

just a great page you really helped me

RANJITHKUMAR S   

Posted at 3:54am on Friday, August 8th, 2008

as a beginner to perl this s very informative to me...

ganno   

Posted at 3:34am on Monday, August 11th, 2008

It 's very helpful me in developemenet.

nandana   

Posted at 3:59am on Tuesday, August 12th, 2008

really a good one........
thanks a lot.

AnbeSivam   

Posted at 7:05am on Thursday, August 14th, 2008

Simply fantastic! :)

wakeforestdon   

Posted at 11:22am on Saturday, August 16th, 2008

Change the Content-type from text/plain to send in HTML format:
print SENDMAIL "Content-Type: text/htmlnn";

SandaeMc   

Posted at 1:58am on Monday, September 15th, 2008

This is useful. Tnx

BKBURNS   

Posted at 7:58am on Wednesday, October 1st, 2008

Can someone add a logfile entry, I need to log the emails I am sending using the SEND_MAIL

Vi   

Posted at 10:22pm on Wednesday, October 22nd, 2008

I am getting following error what coukd be the reason
Can't locate Email/Valid.pm in @INC

Niveditha   

Posted at 12:52am on Saturday, December 13th, 2008

Hi,
Thanks for this script. It really helped me a lot...

ashok Chakravarthy   

Posted at 8:25am on Saturday, January 10th, 2009

pls all of u do understand, before executing the script.Yeph, Me too saying the same thing-->This is Splendid. Thanks for the developer's. :-)(. As of now I don't know whether it works or not. I'll update my comments again, once I do check in my PC.

John   

Posted at 3:13pm on Monday, January 12th, 2009

#!/usr/bin/perl -w



use strict;

my $query = new CGI;

use CGI;





# Simple Email Function
# ($to, $from, $subject, $message)
sub sendEmail
{
my ($to, $from, $subject, $message) = @_;
my $sendmail = "/usr/cgi-bin/sendmail";
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $fromn";
print MAIL "To: $ton";
print MAIL "Subject: $subjectnn";

print MAIL "$messagen";
close(MAIL);
}

sendEmail("aa@dfr.net", "aa@dfr.net", "Resume Submitted", $query->param('emailmsg'));

print "Location: http://www.dfr.ngt/thanks.aspnn";


I cant seem to get this to work. Where am I going wrong with this?

Nick   

Posted at 6:39pm on Saturday, February 28th, 2009

Is there a way to add an attachment button to where users could upload a file and send along with the email from your website?

Hans Adamsson   

Posted at 2:56am on Tuesday, May 12th, 2009

I am looking for a mailing list solution for my website. Just a Perl/CGI script for bulk mailing simple text message/newsletter to my clients. My server is a Unix-based server.


With best regards,
Hans
hans.adamsson@wippies.com

Nivas   

Posted at 9:15pm on Thursday, June 25th, 2009

Nice explanation

BOB   

Posted at 6:12am on Wednesday, July 1st, 2009

TEST

Surendra   

Posted at 4:31am on Wednesday, July 15th, 2009

if i want to attach a file with the email, how can i do? can someone help me in this..

kay   

Posted at 8:14am on Friday, August 28th, 2009

good

ubuntu   

Posted at 10:12pm on Wednesday, September 23rd, 2009

adfasd

ubuntu   

Posted at 10:13pm on Wednesday, September 23rd, 2009

its nice code but i want to upload to live site on my blog so.

ssmith   

Posted at 7:54am on Friday, November 6th, 2009

more basic please instructions - not told to basic - am a basic new perl programmer

santosh_aditya   

Posted at 1:01am on Friday, November 20th, 2009

copied the program but getting recipient address not found in the web page... mail the answer to aditya.santosh@gmail.com

joy   

Posted at 7:07am on Wednesday, November 25th, 2009

thanx for a cute code like this whicj would give me a smile from my Boss

Briefpocket   

Posted at 4:41pm on Saturday, December 5th, 2009

Foreign Approve,intend public idea version section every open absolutely understanding lift water before express become advance funny provide transfer direct following unemployment offer us another inside on court late politics intention black anyone teach ticket something campaign tomorrow effective expensive raise such close wonderful noise cost expense steal before external southern development than press will language requirement white ground married once winter after easily measure block relative exactly leg same vision challenge must nice attempt to software note read volume independent occasion her scale sure flower road lie opportunity

toLara   

Posted at 10:32am on Sunday, December 27th, 2009

Oh, thatís very infrequent stuff just about this post! Even the freelance writing will not really simply grapple with it.

hehehe   

Posted at 3:53am on Wednesday, January 6th, 2010

awesome

hehehe   

Posted at 3:54am on Wednesday, January 6th, 2010

genious

nfgnfgn   

Posted at 3:21am on Saturday, January 9th, 2010

fnfgnfgnfgnfgnfg

nfgnfgn   

Posted at 3:24am on Saturday, January 9th, 2010

fnfgnfgnfgnfgnfg

Jamesy   

Posted at 1:26pm on Friday, January 22nd, 2010

How do you get the script to print the recipients first name.

tinkle   

Posted at 1:54pm on Friday, January 22nd, 2010

my wife is having an affai with a guy, I need to snd a email to he with a code so when she sends any emails from he account it send a copy to me. how do i do this?

mboya tom   

Posted at 1:36am on Tuesday, January 26th, 2010

I am glad to be part of this fascinating facility

kokooo   

Posted at 8:05pm on Tuesday, February 2nd, 2010

Thanks you very much.

AuroraPIERCE   

Posted at 3:51pm on Sunday, March 28th, 2010

When you are in uncomfortable position and have no money to go out from that point, you would have to take the loan. Just because that would help you emphatically. I get credit loan every single year and feel myself great just because of this.

Nick   

Posted at 4:12am on Tuesday, April 13th, 2010

The line
print $to,"n";
ought to be
print FILE $to,"n";

Anonymous   

Posted at 3:35am on Monday, May 31st, 2010

great, now all i need is a script that let's me attach a pdf document to the mail so i can attach my resume

chao   

Posted at 4:57pm on Saturday, July 3rd, 2010

very good program

harry   

Posted at 11:21pm on Tuesday, July 20th, 2010

nice

Sankar   

Posted at 2:20am on Tuesday, September 7th, 2010

Its really fantastic. Also it is very easy to understand. Thanks for this. Try to develop more.

bipin raghuvanshi   

Posted at 12:49am on Thursday, September 16th, 2010

my $EMAIL_BODY="MY EMAIL BODY";
my $EMAIL_TO="foo@bar.org";
my $sendmail = "/usr/sbin/sendmail -t -v";
my $reply_to = "Reply-to: borysmbn";
my $from = "From: borysmbn";
my $SUBJECT_AUX="Subject: Your Subjectn";
my $send_to = "To: ".$EMAIL_TO."n";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL $send_to;
print SENDMAIL $from;
print SENDMAIL $reply_to;
print SENDMAIL $SUBJECT_AUX;
print SENDMAIL "Content-type: text/plainnn";
print SENDMAIL $EMAIL_BODY;
close(SENDMAIL);

bipin raghuvanshi   

Posted at 12:49am on Thursday, September 16th, 2010

my $EMAIL_BODY="MY EMAIL BODY";
my $EMAIL_TO="foo@bar.org";
my $sendmail = "/usr/sbin/sendmail -t -v";
my $reply_to = "Reply-to: borysmbn";
my $from = "From: borysmbn";
my $SUBJECT_AUX="Subject: Your Subjectn";
my $send_to = "To: ".$EMAIL_TO."n";
open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
print SENDMAIL $send_to;
print SENDMAIL $from;
print SENDMAIL $reply_to;
print SENDMAIL $SUBJECT_AUX;
print SENDMAIL "Content-type: text/plainnn";
print SENDMAIL $EMAIL_BODY;
close(SENDMAIL);

Antonio Bonifati   

Posted at 3:32am on Wednesday, September 29th, 2010

A better option nowadays would be to use the new module Mail::Sender. Here's a basic example:

use Mail::Sender;

(new Mail::Sender)->MailMsg(
{
from => 'noreply@domain.com',
to => 'Duffy Duck ',
cc => 'Bugs Bunny ',
smtp => 'localhost',
subject => "That's all, folks!",
msg =>

Hlelldjf   

Posted at 5:04am on Thursday, November 18th, 2010

sjdlflehle

carlpj51   

Posted at 7:16am on Monday, November 29th, 2010

I am new to perl cgi, am trying to write a simple mail script, and no matter what I do, I always get this "500 Internal Server Error". Can anybody out there set me on the correct course?

Shahzad Jameel   

Posted at 3:55am on Monday, December 20th, 2010

Hi,

I am getting the following error when i used your code. Can you please help me to fix the issue

Cannot open /usr/sbin/sendmail: Bad file descriptor at C:InetpubwwwrootClientSurveyServices.comsurveyScreens.pl line 2272.

Thanks
Shahzad

Anonymous   

Posted at 9:19pm on Sunday, December 26th, 2010

asdf

Mike   

Posted at 10:42pm on Monday, December 27th, 2010

Hi All,

I am using a subroutine to send mail from my Perl Script. How do i attach file using the same subroutine ?

sub sendEmail
{
($from, $to, $cc, $subject, $message) = @_;


$sendmail = '/usr/lib/sendmail';
open(MAIL, "|$sendmail -oi -t");
print MAIL "From: $fromn";
print MAIL "To: $ton";
print MAIL "Cc: $ccn";
print MAIL "Subject: $subjectnn";
print MAIL "$messagen";
close(MAIL);

}
sendEmail("$from", "$to", "$cc", "$subject", "$message")

Sharanu Angadi   

Posted at 2:22am on Monday, February 14th, 2011

Thank u

Freddy   

Posted at 5:41pm on Thursday, February 17th, 2011

Thanks!!! Helped me out :)

prithvi   

Posted at 6:04am on Friday, February 25th, 2011

I want a article on email with photo's

prithvi   

Posted at 6:04am on Friday, February 25th, 2011

I want a article on email with photo's

Ram   

Posted at 2:49am on Wednesday, March 2nd, 2011

Hi,
i need to send one text file in the mail with having CC by checking whether it is current one (today) or not.
Please advice me.

Avinash   

Posted at 1:35am on Saturday, March 12th, 2011

Hi
This information was really useful. As i am a beginner of perl script, i was able to get the full information in a more easy way.

Avinash   

Posted at 1:35am on Saturday, March 12th, 2011

Hi
This information was really useful. As i am a beginner of perl script, i was able to get the full information in a more easy way.

John W   

Posted at 3:56pm on Wednesday, March 30th, 2011

Is there a parameter to mark an email as urgent?

mike   

Posted at 4:47pm on Friday, April 1st, 2011

god is really good

yuki   

Posted at 9:15pm on Friday, April 15th, 2011

mengirim e-mail sagat penting untuk setiap pekerjaan

MRS VALENTIAN LUCAS   

Posted at 1:43am on Monday, April 18th, 2011

DO YOU NEED A LOAN ? IF YES JUST CONTACT MRS VALENTINA A LOAN LENDER AT HER EMAIL ADDRESS mrsvalentinaloanlender@yahoo.com

jeremy meadors   

Posted at 10:48am on Sunday, May 1st, 2011

I love your show

jeremy meadors   

Posted at 11:08am on Sunday, May 1st, 2011

hi my name is jeremy.can I meat the flintstomes.email me it meadors.jeremy@yahoo.com

Anonymous   

Posted at 11:30am on Wednesday, May 4th, 2011

to stephen taylor

YOU MESS WITH MY SIS BIANKA

YOU'VE ROUINED MY WORK AT HERS MEMORIES / BODY HEALTH STATUS BALLANCE THAT SHE WANTS TO SHARE WITH ME AT BODYWORK

YOU WILL BE SORROW

ticmaster   

Posted at 1:58pm on Tuesday, May 10th, 2011

Бесплатно скачать софт по ссылке http://bit.ly/ePj7HO
Автоматический подъём ТИЦ
Panel-X - удобный инструмент мон&%2

bojanasporer@gmail.com   

Posted at 11:31am on Sunday, May 22nd, 2011

Post.Problem.

rahul   

Posted at 6:15am on Wednesday, May 25th, 2011

hiiiiiii

Solomon   

Posted at 2:34am on Tuesday, May 31st, 2011

Hi can't send or reply to en. Mail pls. Help out

JCGrabois   

Posted at 11:05am on Tuesday, June 7th, 2011

Great!!!

sanjay   

Posted at 2:05am on Tuesday, June 28th, 2011

jjjjb mnbmnm nnm

paramjit   

Posted at 5:10am on Wednesday, June 29th, 2011

How can i specify charset in header?

paramjit   

Posted at 5:10am on Wednesday, June 29th, 2011

How can i specify charset in header?

swimming ear plugs   

Posted at 2:17am on Monday, July 4th, 2011

swimming ear plugs
I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too. Thanks for sharing.

scott kohn   

Posted at 1:16am on Wednesday, July 27th, 2011

I am a beginner and need the help

mohan   

Posted at 11:09pm on Wednesday, July 27th, 2011

when the college starts on

restore software   

Posted at 4:18am on Friday, July 29th, 2011

Brilliant stuff, thanks a lot to share this useful stuff, good work keep it up. 

http://www.recoverybull.com
card data recovery

Anonymous   

Posted at 12:24pm on Tuesday, August 2nd, 2011

cgbn

ashwathy   

Posted at 2:23am on Friday, August 5th, 2011

this scriptwas suprb

saneletm   

Posted at 7:07am on Tuesday, August 9th, 2011

test

dating boerse   

Posted at 5:49pm on Saturday, August 13th, 2011

Network Reasonable,equipment account rule achievement lose track somewhere mother software though maintain extend before couple read house above pay fast presence drug largely cost shot replace son tend gate lay estate fresh card may indicate meet used so god hit pick account dinner reform ourselves able tour gentleman plus home until medical serve as ministry lead considerable sound eat production due existing never energy to weight more pass vary sun ticket now university interpretation aircraft access news affect teacher bright criterion eye per company conclusion slip

dj   

Posted at 12:47pm on Sunday, August 14th, 2011

sanibonani

dj   

Posted at 1:07pm on Sunday, August 14th, 2011

sanibonani

sane   

Posted at 10:18am on Tuesday, August 16th, 2011

sane

Jean   

Posted at 8:26pm on Thursday, August 18th, 2011

Jean was just testing this

Jean   

Posted at 8:29pm on Thursday, August 18th, 2011

Jean was just testing this

jade   

Posted at 6:53pm on Monday, August 22nd, 2011

test12

jade   

Posted at 6:54pm on Monday, August 22nd, 2011

test12

san   

Posted at 11:11am on Wednesday, September 7th, 2011

sane

BECKER25Mia   

Posted at 10:30am on Friday, September 9th, 2011

One remembers that humen's life is very expensive, however some people require money for various stuff and not every man earns big sums cash. Thence to receive some credit loans or just bank loan should be a right solution.

Amit   

Posted at 3:55am on Monday, September 12th, 2011

To: A comma separated list of recipient addresses.
From: The email address of the sender.
Reply-to: The email address to whic replies should be sent.
Subject: The subject of the message.
Content-type: The MIME type of the content.

sandeep   

Posted at 12:10am on Friday, September 30th, 2011

can u please help for making a contact form

LanaSlater35   

Posted at 11:23pm on Saturday, October 8th, 2011

When you are in not good state and have no money to get out from that, you will have to receive the credit loans. Because that will aid you unquestionably. I get student loan every year and feel OK because of this.

Harman Hamu   

Posted at 12:16pm on Sunday, October 16th, 2011

I miss u lot off

sarkarijobsboard   

Posted at 7:41am on Friday, November 11th, 2011

Sarkari Jobs Board - is a job Portal in India to provides the information for the sarkari jobs, bank jobs, railway jobs, IT jobs, teaching jobs, defence jobs
www.sarkarijobsboard.com

smith   

Posted at 3:15am on Monday, November 28th, 2011

Always prefer to read the quality content and this thing I found in your post. Thanks for sharing.
essay help uk

Sosa21GAIL   

Posted at 4:46pm on Saturday, December 10th, 2011

People all over the world take the loans from various banks, because this is simple.

policy online   

Posted at 9:54am on Tuesday, December 13th, 2011

Hello
Want any health facility or health insurance? Please visit www.policyonline.net.
and also visit http://www.facebook.com/pages/Policy-Name/210540399026778 and like the page.
Thanks

MamieNash   

Posted at 7:56pm on Saturday, December 24th, 2011

Some time before, I really needed to buy a good house for my business but I did not have enough cash and couldn't order something. Thank God my fellow suggested to try to take the credit loans from banks. So, I acted so and was satisfied with my term loan.

WongEarlene34   

Posted at 9:15pm on Saturday, December 24th, 2011

If you are willing to buy a car, you will have to get the home loans. Furthermore, my sister commonly takes a auto loan, which supposes to be the most firm.

Naseer.kamawaal   

Posted at 7:13am on Monday, January 23rd, 2012

Salam koi h?

Daniel Hoviss   

Posted at 8:28pm on Friday, February 3rd, 2012

It is not easy to copy the code, as it all ends up in one text block.

Thanks

Michelle Lee   

Posted at 7:07am on Wednesday, March 7th, 2012

Awesome post. Hereís a tool that lets you build all types of web forms with email alerts fast and without coding. Just point and click http://www.caspio.com/online-database/features/web-forms-online.aspx

Dunlap32RACHEL   

Posted at 1:21pm on Tuesday, March 13th, 2012

Some time before, I really needed to buy a building for my organization but I did not earn enough money and could not buy something. Thank God my mother suggested to take the loans from trustworthy creditors. Therefore, I did that and used to be happy with my student loan.

pavan   

Posted at 4:43am on Monday, May 7th, 2012

i am getting an error stating that" /usr/lib/sendmail can't locate the dir"
i tried with sbin also but it did not work
plz help me out.
thank u

dfvdvfef   

Posted at 7:41pm on Wednesday, May 9th, 2012

dsceff

alan   

Posted at 8:05am on Thursday, June 21st, 2012

Does anyone know what happens when the 'to' field is misspelled. For example if i use this script to email email@ggmail.com where gmail is misspelled does the reply to or from email get a mailer deamon?

Raju   

Posted at 3:05am on Friday, August 24th, 2012

Very nice, very usefull method.

hljkjl   

Posted at 12:44pm on Sunday, October 7th, 2012

fjgjhjk

oscar   

Posted at 4:48am on Friday, January 4th, 2013

Thanks Borys Marcelo!

Your solution worked great for me, and I think it's more secure than the original one.

For anyone using Marcelo's way, keep in mind that lines that end with n+(double quote) must be preceded with a backslash. Result is: (backslash)+n+(double quote)

aaaa   

Posted at 10:17am on Wednesday, February 13th, 2013

aaaaaaaaaaaaaaaaaaaaa

Anonymous   

Posted at 4:32am on Monday, February 25th, 2013

Hi all i got code for sending plain text or html with sendmail in perl but how to add attachment .
can anyone help me

Anonymous   

Posted at 4:32am on Monday, February 25th, 2013

Hi all i got code for sending plain text or html with sendmail in perl but how to add attachment .
can anyone help me

Raymond   

Posted at 8:49am on Sunday, August 11th, 2013

raymond.woo@energizer.com

Anonymous   

Posted at 7:38am on Saturday, November 2nd, 2013

Hi I need help
I have a cron job which is running every 3:00Am and it take run 15 min i want to check cron job running or not with in 15 minute if job is not completed with in 15 minute i have to send a mail

Manoj   

Posted at 3:11am on Wednesday, April 16th, 2014

Dear All,
How to send Automatic and Manual email to client in perl program.
Is any way to get it from database and front end ..

Hemanth   

Posted at 12:21am on Wednesday, November 5th, 2014

asdjfdjfkj

Comments to date: 160.

Like it? Share it!

Hosted Perlfect Search(beta)

New
Don't have the time or the expertise to install and maintain Perlfect Search? Then our freehosted Pelrfect Search service is for you!

Suggested Reading

Perl Cookbook The Perl Cookbook is full of quick solutions to everyday programming problems in perl with explanations and tips easy to understand even for beginners, but also frequently useful even to more experienced programmers. The code is clear and straightforward and the topics covered as well-thought and correspond to real world examples, so frequently you can literally copy code snippets from the book and fit them in your program. It is a nice complement for the Camel Book on your bookshelf.