How to use perl for SMTP connection with user and SSL Auth and send emails with attachment

Posted by Octopus on Stack Overflow See other posts from Stack Overflow or by Octopus
Published on 2010-04-06T10:06:06Z Indexed on 2010/04/12 4:53 UTC
Read the original article Hit count: 582

Filed under:
|
|

I am using a SMTP mail server which require user + ssl authentication for connection. I am looking for the perl modules to connect to the mail server and send emails but doesn't found anything helpful.

Any suggestion for perl module or any perl code would be really appreciated.

EDIT

I have tried to use Mail::Sendmail and Net::SMTP::SSL to connect to the sendmail server and send mail. Below is the sample code but getting the error user unknown.

Error:

mail: Net::SMTP::SSL=GLOB(0x9599850) not found 
RCPT TO: error (550 5.1.1 <[email protected]>... User unknown).

Code:

#!/usr/bin/perl

use strict;
use warnings;
use Mail::Sendmail;
use Net::SMTP::SSL;

my %mail = (
#To=> 'No to field this time, only Bcc and Cc',
From=> '[email protected]',
Cc=> '[email protected]',
# Cc will appear in the header. (Bcc will not)
Subject => 'Test message',
'X-Mailer' => "Mail::Sendmail version $Mail::Sendmail::VERSION",
);

$mail{Smtp} = Net::SMTP::SSL->new("mail.server.com", Port=> 465);
$mail{auth} = {user=>'username', password=>"password", required=>1 };
$mail{'X-custom'} = 'My custom additionnal header';
$mail{Message} = "The message key looks terrible, but works.";
# cheat on the date:
$mail{Date} = Mail::Sendmail::time_to_date( time() - 86400 );
if (sendmail %mail) { print "Mail sent OK.\n" }
else { print "Error sending mail: $Mail::Sendmail::error \n" }

print "\n\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log;

© Stack Overflow or respective owner

Related posts about perl

Related posts about perl-module