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) | [reply] [Watch: Dir/Any] [d/l] [select] |
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.
| [reply] [Watch: Dir/Any] [d/l] |
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 ;-) | [reply] [Watch: Dir/Any] [d/l] [select] |
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. | [reply] [Watch: Dir/Any] |
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 | [reply] [Watch: Dir/Any] [d/l] |
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;
}
| [reply] [Watch: Dir/Any] [d/l] |
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) | [reply] [Watch: Dir/Any] |
| [reply] [Watch: Dir/Any] |
| [reply] [Watch: Dir/Any] |