如何使用 perl 与用户和 SSL 身份验证进行 SMTP 连接并发送带附件的电子邮件
我使用的是 SMTP 邮件服务器,需要用户 + ssl 身份验证才能连接。我正在寻找 perl 模块来连接到邮件服务器并发送电子邮件,但没有发现任何有用的东西。
任何有关 perl 模块或任何 perl 代码的建议将不胜感激。
编辑
我尝试使用 Mail::Sendmail 和 Net::SMTP::SSL 连接到 sendmail 服务器并发送邮件。以下是示例代码,但出现错误用户未知。
错误:
mail: Net::SMTP::SSL=GLOB(0x9599850) not found
RCPT TO: error (550 5.1.1 <[email protected]>... User unknown).
代码:
#!/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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我假设您验证了这一行中的数据:
是用户“用户名”(在您的现实代码中“
如果没有,我会预期出现
用户未知
错误。编辑1
我刚刚看到你的邮件中没有“收件人”,“只有”一个抄送,你的邮件服务器可能不这样(我的不介意,所以:-),或者是在修剪邮件时“发生”了这种情况代码?
编辑2
我能够通过替换该行
来
重现您的错误,您需要为邮件服务器提供一个有效的地址来发送消息!当我提供现有的收件人地址时(
Cc=>'[email protected] ]'
行..成功了!I assume you verified the data in this line:
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
with
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!您应该在
search.cpan.org
上使用“SMTP”和“SSL”等术语查找此类内容。第一个命中是Net::SMTP::SSL
,它将自己描述为标准Net::SMTP
的直接替代品。这听起来非常相关;它能做你想做的事吗?You should look for such things at
search.cpan.org
with terms such as "SMTP" and "SSL". The first hit isNet::SMTP::SSL
, which describes itself as a drop-in replacement for the standardNet::SMTP
. That sounds very much relevant; does it do what you want?也许您可以尝试使用 Net::SMTP::SSL 通过 SSL 身份验证进行连接,并尝试使用 Net::SMTP::Multipart 进行附件支持,并采取一些措施使它们协同工作。
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.
Comcast 已转向 SSMTP,因此我必须修改我的自动消息。
下面是 Comcast 邮件服务器的一些工作 PERL 代码。希望您觉得它有帮助。
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.
您收到的错误是 RCPT TO 错误,而不是身份验证错误。这可能是因为您实际上尝试向不存在的用户发送邮件,或者因为您尝试在未进行身份验证的情况下通过服务器进行中继。
如果看起来 Mail::Sendmail 不支持 SMTP 身份验证方法(
http://metacpan.org/pod/Mail::Sendmail#LIMITATIONS )所以很可能甚至没有尝试过身份验证。
您应该在创建 Net::SMTP:SSL 实例后立即使用它的 auth 方法:
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: