Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by vroom (His Eminence)
on Mar 02, 2000 at 23:30 UTC ( [id://4684]=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (mail and news)

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

Originally posted as a Categorized Question.

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

Replies are listed 'Best First'.
Re: How do I send e-mail from my Perl Program?
by chromatic (Archbishop) on Mar 31, 2000 at 00:33 UTC
    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)
Re: How do I send e-mail from my Perl Program?
by Adam (Vicar) on Oct 13, 2000 at 05:14 UTC
    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.
      Small error:
      my $smtp = Net::SMTP->new('$mailserver_url') or die $!;
      should be:
      my $smtp = Net::SMTP->new($mailserver_url) or die $!;
      or
      my $smtp = Net::SMTP->new("$mailserver_url") or die $!;
      Single quotes aren't interpolated!

      cLive ;-)

Re: How do I send e-mail from my Perl Program?
by chromatic (Archbishop) on Mar 02, 2000 at 23:35 UTC
    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.
Re: How do I send e-mail from my Perl Program?
by silent11 (Vicar) on Feb 08, 2002 at 16:42 UTC
    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
Re: How do I send e-mail from my Perl Program?
by twobucks (Initiate) on Feb 02, 2006 at 04:03 UTC
    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; }
Re: How do I send e-mail from my Perl Program?
by iguane (Beadle) on Apr 10, 2001 at 09:36 UTC
    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)
Re: How do I send e-mail from my Perl Program?
by Anonymous Monk on Mar 27, 2002 at 20:47 UTC
    Use Outlook Express, it sends mail easy!

    Originally posted as a Categorized Answer.

Re: How do I send e-mail from my Perl Program?
by Anonymous Monk on Feb 06, 2004 at 07:38 UTC
    what is your name

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-12-02 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found