Perl – Sending Email with NET::SMTP using username and password



First of all double check that Authen::SASL is an installed module.. If you are not getting emails this could be why – it doesnt provide an error that is understandable!

#!/usr/bin/perl
### ENSURE Authen::SASL is installed
use Net::SMTP;
use strict;
use warnings;

my $host= 'yourhostname';
my $username= "yourpop3username";
my $password = "yourpassword";

my $from = 'bob\@GOemail.com';
my $to = 'bob@here.co.uk';
my $DOMAIN = "somedomain.co.uk";

my $smtp = Net::SMTP->new($host, Hello =>$DOMAIN, Timeout => 60) or die "Failed to Open SMTP Connection : $!";
$smtp->auth($username, $password) or die "Failed to authenticate";

my $subject = "Build";
my $emailBody = "This is the body...\n BOB";

$smtp->mail("$from"); ## FROM
$smtp->to("$to");
$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from \n");
$smtp->datasend("Subject: $subject \n");
$smtp->datasend("\n");
$smtp->datasend("$emailBody:\n");
$smtp->datasend("\n");
$smtp->dataend();
$smtp->quit;

These links were useful also:

http://quark.humbug.org.au/publications/perl/perlsmtpintro.html

http://www.perlmonks.org/?node_id=449583

http://search.cpan.org/~shay/libnet-1.27/Net/SMTP.pm