Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I send e-mail from my Perl Program?

( #4684=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Mar 02, 2000 at 23:30 UTC
Q&A  > mail and news


Answer: How do I send e-mail from my Perl Program?
contributed by chromatic

Here's a corrected version that works under -w and strict:

use strict; use Mail::Mailer; my $from_address = "perlmonk"; my $to_address = "user\@localhost"; my $subject = "hi"; my $body = "hello"; my $mailer = Mail::Mailer->new("sendmail"); $mailer->open({ From => $from_address, To => $to_address, Subject => $subject, }); print $mailer $body; $mailer->close();
(note that there was a missing semicolon)

and the second:

open(MAIL, "|/usr/lib/sendmail -oi -t -odq") || die "Can't open sendma +il: $!"; print MAIL <<MESSAGE; From: Your name <emailaddress\@blah.com> To: Recipient name <email address\@blah2.com> Subject: Mailing with Perl this is easy. MESSAGE close(MAIL);
(note that the heredoc was broken)
Answer: How do I send e-mail from my Perl Program?
contributed by Adam

use strict; # ALWAYS! use Net::SMTP; # Yes, this will work on windows. # You need to fill in the variables. # Read the perldoc for more info on using SMTP. my $smtp = Net::SMTP->new('$mailserver_url') or die $!; $smtp->mail( $from ); $smtp->to( $to ); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); # done with header $smtp->datasend($message); $smtp->dataend(); $smtp->quit(); # all done. message sent.
Answer: How do I send e-mail from my Perl Program?
contributed by chromatic

Mail::Sendmail or Net::SMTP are good CPAN modules which will work if you cannot install or do not have Sendmail installed on your box.

Answer: How do I send e-mail from my Perl Program?
contributed by silent11

I use Mail::Sendmail, here is some code that use and reuse. Be usre to set your mail server in sendmail.pm tho, here is a snipped of code that will send HTML email.

use Mail::Sendmail; %mail = ( from => 'me@domain.com', to => 'you@domain.com', subject => 'Test HTML mail', 'content-type' => 'text/html; charset="iso-8859-1"', ); $mail{body} = <<END_OF_BODY; <html> <head> <style> body {font-family: Verdana; size: 10px; color:black;} </style> </head> <body> Here is the contents of your email, use html as you wish ...... have f +un! </body> </html> END_OF_BODY sendmail(%mail) || print "Error: $Mail::Sendmail::error\n";
  - Silent11
Answer: How do I send e-mail from my Perl Program?
contributed by twobucks

I've had many problems with mail clients marking email as potential spam due to changes in the RFC related to the date and time stamp functions. Moy code has to work on Win32 and Linux boxes, too, and sendmail doesn't. So I wrote the following sub that uses NET::SMTP. It auto detects the missing SASL and Base64 pods on Activestate Win32 machines so it can authenticate to remote servers.

sub SMTPMail { use Net::SMTP; # Not all versions of SMTO support authentication. my ($to, $from, $subj, $msg, $smtpserver) = @_; my $body = ""; my $error; eval { require MIME::Base64; require Authen::SASL; }; if ($@) { $error = system("ppm install MIME::Base64"); $error = system("ppm install Authen::SASL"); } ; my @dayofweek = (qw(Sun Mon Tue Wed Thur Fri Sat)); my @monthnames = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov De +c)); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime(); $year += 1900; #Create an RFC compliant time stamp my $Date = sprintf("%s, %02d %3s %04d %02d:%02d:%02d CST",$dayofwee +k[$wday],$mday,$monthnames[$mon],$year,$hour,$min,$sec); # Make $to into an array if it's not one already if (!ref($to)) { @$to = split(/[,;]\s*/, $to); } # Make Email Header $body = "MIME-Version: 1.0\n" . "From: $from\n" . "To: " . join(',', @$to) . "\n" . "Date: ".$Date."\n" . "Subject: $subj\n\n" # Double \n . $msg . "\n"; my $smtp; # Open a SMTP session if(!($smtp = Net::SMTP->new($smtpserver))) { return "Could not connect to SMTP Server: $smtpserver "; } if (length($EmailAuth) > 0 && length($EmailPassword) > 0 ) { &LogFile("Email","Auth $EmailAuth"); my $error = $smtp->auth($EmailAuth,$EmailPassword) ; if (! $error) { return "Cannot authenticate as user $EmailAuth and/or b +ad Password for Email - Error $error - Check System Settings "; } } # Pass the 'from' email address, exit if error if (! ($smtp->mail( $from ) ) ) { return "Bad 'From', check your Ac +count Information"; } # Pass the recipient address(es) if (!($smtp->recipient(@$to))) { return "Bad 'EmailTo'"; } # Send the msg $smtp->data( $body ); $smtp->quit; return 0; }
Answer: How do I send e-mail from my Perl Program?
contributed by iguane

If you want to send a mail with your perl program, you could have a look on Snippets menu. I produce a program which send a mail by just using socket connexion and control return path mail -> Post by Iguane the 2001-04-09 . You could like merlin say using another librairy like (MIME::Lite)

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (17)
    As of 2014-10-30 19:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (208 votes), past polls