views:

682

answers:

4

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 = (
From=> '[email protected]',
To=> '[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, Debug=>1);
$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;
A: 

Maybe you can try Net::SMTP::SSL for connecting over SSL authentification and Net::SMTP::Multipart for attachments support and do something to make them work together.

Mike
A: 

You should look for such things at search.cpan.org with terms such as "SMTP" and "SSL". The first hit is Net::SMTP::SSL, which describes itself as a drop-in replacement for the standard Net::SMTP. That sounds very much relevant; does it do what you want?

Kilian Foth
Thanks, I have tried using these modules and but the mails are not delivered. I am getting error Error sending mail: Net::SMTP::SSL=GLOB(0x9599850) not foundRCPT TO: error (550 5.1.1 <[email protected]>... User unknown).Any suggestion.
Space
+1  A: 

I assume you verified the data in this line:

$mail{auth} = {user=>'username', password=>"password", required=>1 };

is the user 'username' (in your reallife code '[email protected]'?) with the password 'password' known at mail.server.com?

If not, I would expect a User unknown error.


EDIT1 I just saw that you have no 'To' in that mail, 'only' a cc, might your mail server not like that (mine didn't mind, so then :-), or did that 'happen' in trimming down the code?


EDIT2 I was able to reproduce your error by replacing the line

$mail{Smtp} = Net::SMTP::SSL->new("mail.server.com", Port=> 465);

with

$mail{Smtp} = Net::SMTP::SSL->new("smtp.mail.com", Port=> 465);

you need to give the mailserer a valid address to send the message to! When I supplied an existing to address (the Cc=>'[email protected]' line .. it worked!

lexu
@lexu: This is not the case. My username and password are known to the mail server and I can connect using other mail clients.
Space
@Octopus: strange! I'll see if I can get your code to work for me!
lexu
Thanks Lexu: That will be great help for me.
Space
I tried your code, replacing the mail-server, userid and pwd with my own known credentials ... and it worked! Looking at port 465 next, seems somehow wrong!
lexu
@Octopus: re port 465: => nope, error was in my memory! Is your target server running exchange? In that case you might have to use Net::SMTP::TLS (I'm guessing here, I can't test it, and have no experience with exchange)
lexu
I have no knowledge about the mail servers. I am using port 465 with SSL authentication. Also, I havent done any settings for my local sendmail (the sendmail service is not running even). is that also required.
Space
@Octopus: Sendmail is a program called on demand, not a service (Linux/Mac, assuming same for windows). No problem there.
lexu
@Octopus: see edit to answer above! Must try with valid recipient!
lexu
Thanks Lexu, i have edited my question with the working code. Can you also please advise how can i send attachment with this. Really appreciate for any perl sample code.
Space
A: 

The error you get is a RCPT TO error not an authentication error. This could be because you actually tried to send mail to a nonexistent user or because you tried to relay through the server without authenticating.

If seems like Mail::Sendmail has no support for SMTP Auth Method ( http://search.cpan.org/~mivkovic/Mail-Sendmail-0.79/Sendmail.pm#LIMITATIONS ) so most likely authentication was not even tried.

You should use the auth method of Net::SMTP:SSL right after you create it's instance:

$mail{Smtp}->auth('username','password');
Mihai Secasiu