Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Mail Question

by Anonymous Monk
on Apr 13, 2000 at 22:05 UTC ( [id://7498]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This probably sounds really dumb to all of you:
Can someone give me a little example using mail?
Let's say I want to send the e-mail to: name@server.com, with 'Mail Message' as the subject.

How can I do this? My mail program is located at /usr/sbin/sendmail.
Please help

Replies are listed 'Best First'.
Re: Mail Question
by pschoonveld (Pilgrim) on Apr 13, 2000 at 22:52 UTC
    Well, I prefer to do most of everything by hand. Means less overhead from the modules. Please enjoy the following:

    The following uses CGI.pm as an easy way to deal with CGI vars. Sends me email from any web form. Prints each var "paco=taco" format.

    #!/usr/bin/perl use CGI qw(:standard); @submits = param(); open(MAIL, "|/usr/sbin/sendmail -oi -t") or die "Can't Open Sendmail: +$!"; print MAIL <<EOF; From: Web Page To: patrick\@pacoswithtacos.net Subject: Web Form Submission EOF foreach $item (@submits) { print MAIL "$item=".param($item)."\n"; } close(MAIL); print "Location: ../thank_you.html\n\n";
    At the end it tells the browser to go to an html page.
Re: Mail Question
by httptech (Chaplain) on Apr 14, 2000 at 00:26 UTC
    If you want to use pschoonveld's example, you might also want to know about the -odq option to sendmail; it forces sendmail to queue the mail for sending later.

    This can be helpful in situations where you are pumping out a lot of mail to different users and waiting on sendmail will slow your script down. Also bad addresses can sometimes kill your script or give you unexpected outout from sendmail in your HTML. This method will keep your output clean even if the user's email address is malformed (which you really should try to do some rudimentary checking on beforehand anyway)
Re: Mail Question
by btrott (Parson) on Apr 13, 2000 at 22:15 UTC
    Take a look at this FAQ answer: How do I send mail?.

    You have several options: you can pipe to sendmail directly, or you can use a module, using either sendmail (Mail::Mailer) or an SMTP server (Mail::Internet, which uses Net::SMTP).

RE: Mail Question
by Anonymous Monk on Apr 14, 2000 at 00:30 UTC
    The Net:SMTP module works really well and doesn't rely on sendmail so if you switch to a different MTA the script still works. Unlike calling sendmail from a script, Net::SMTP also allows you to use a remote MTA such as one at your ISP. This is a bit of a script (hopefully modified correctly) that should show the basics. The original script I wrote runs as a cron job and fetches my stock quotes from Yahoo and sends them to my cell phone every day at market close. Note, the start of the text should include the standard To:, From: and Subject: lines explicitely stated with a blank line at the end as shown below. Replace localhost with your ISP's MTA if appropriate. -Steve
    use Net::SMTP; # Get ready to send messages via SMTP $thetext = "From: me\@myself.org\n"; $thetext .= "To: you\@yourself.com\n"; $thetext .= "Subject: Some Stuff\n"; $thetext .= "\n"; # Send the message and disconnect $smtp=Net::SMTP->new('localhost'); $smtp->mail("me\@myself.org"); $smtp->recipient("you\@yourself.com"); $smtp->data($thetext); $smtp->quit;
Re: Mail Question
by zodiac (Beadle) on Apr 14, 2000 at 11:34 UTC
    You could also use the mail command:
    $subject = "Mail Message"; $to = "name@server.com"; open(MAIL, "|mail -s '$subject' $to"); print MAIL "message body" print MAIL "\n.\n"; # to quit close(MAIL);
      If available 'mailx' gives more flexibility, specially when using the tilda sequences, for example if you have a the contents of data file to send you can do something like $example = `mailx whoever\@whereever.com <<! ~<$file_name !`;
        Hmm, managed to lose the rest of my code. The point was you can include files in the mail message with a simple ~<$filename call

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://7498]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found