Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
laziness, impatience, and hubris

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

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\> To: Recipient name <email address\> Subject: Mailing with Perl this is easy. MESSAGE close(MAIL);
(note that the heredoc was broken)
contributed by Adam

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.

contributed by silent11

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

use Mail::Sendmail; %mail = ( from => '', to => '', 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
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; }
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 read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (3)
    As of 2014-03-15 05:36 GMT
    Find Nodes?
      Voting Booth?

      Have you used a cryptocurrency?

      Results (317 votes), past polls