Quelques modules Perl
Aller à la navigation
Aller à la recherche
Envoyer des mails avec Net::SMTP
En perl on peut tout faire... ici envoyer un mail.
Le serveur SMTP qui vas être utilisé est a renseigné dans $SMTP_HOST.
J'ai essayé avec le smtp de free smtp.free.fr
. Ca marche bien mais google classe le mail illico dans les spam!
#!/usr/bin/perl -w
use Net::SMTP;
my $SMTP_HOST = "server";
sub send_mail
{
my ($from, $to_addr, $msg);
$from = shift;
$to_addr = shift;
$msg = shift;
#
# Open a SMTP session
#
$smtp = Net::SMTP->new( $SMTP_HOST,
'Debug' => 0,
);
if(!defined($smtp) || !($smtp))
{
print "SMTP ERROR: Unable to open smtp session.\n";
return 0;
}
#
# Pass the 'from' email address, exit if error
#
if (! ($smtp->mail( $from ) ) )
{
print "SMTP ERROR: Unable open new mail.\n";
return 0;
}
#
# Pass the recipient address(es)
#
if (! ($smtp->recipient( ( ref($to_addr) ? @$to_addr : $to_addr ) ) ) )
{
print "SMTP ERROR: Unable to create recipient.\n";
return 0;
}
#
# Send the message
#
$smtp->data();
$smtp->datasend("Subject: test\n");
$smtp->datasend("To: $to_addr\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Reply-to: $replyaddr\n") if $replyaddr;
$smtp->datasend("X-Mailer: Perl Sendmail \n");
$smtp->datasend("\n");
$smtp->datasend("$msg\n");
$smtp->dataend();
$smtp->quit();
}
send_mail(
'test@toto.com',
'test@toto.com',
'This is a brief email.\n');