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

我只是一个虾纸丫 提交于 2019-12-06 11:38:00

问题


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 <user@mail.com>... User unknown).

Code:

#!/usr/bin/perl

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

my %mail = (
From=> 'user1@server.com',
To=> 'user2@server.com',
# 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;

回答1:


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 'user@mail.com'?) 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=>'user2@server.com' line .. it worked!




回答2:


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?




回答3:


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.




回答4:


Comcast has moved to SSMTP, so I had to modify my automated message.

Here is some working PERL code for a Comcast mail server. Hope you find it helpful.

#!/usr/bin/perl

use Net::SMTP::SSL;
use MIME::Base64;


$uptime = `uptime`;

$smtp = Net::SMTP::SSL->new
        (
        "smtp.comcast.net",
        Hello => myhost.comcast.net,
        Port => 465,
        Timeout => 30,
        Debug => 0,
        );
$smtp->datasend ("AUTH LOGIN\n");
$smtp->response();

# Mailbox info
$smtp->datasend (encode_base64('myuserid')); #username
$smtp->response();
$smtp->datasend (encode_base64('mypassword')); # password
$smtp->response();

# Email from
$smtp->mail ('myuserid@comcast.net');

# Email to
$smtp->to ('anyuser@gmail.com');

$smtp->data();

$smtp->datasend("To: anyuser@gmail.com\n");
$smtp->datasend("From: myuserid\@comcast.net\n");
$smtp->datasend("Subject: My Subject Line");

# Line break to separate headers from body
$smtp->datasend("\n");


$smtp->datasend("Uptime Report - At $uptime\n");
$smtp->dataend();

$smtp->quit();

exit;



回答5:


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://metacpan.org/pod/Mail::Sendmail#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');


来源:https://stackoverflow.com/questions/2583987/how-to-use-perl-for-smtp-connection-with-user-and-ssl-auth-and-send-emails-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!