如何使用 perl 与用户和 SSL 身份验证进行 SMTP 连接并发送带附件的电子邮件

发布于 2024-08-27 15:46:12 字数 1613 浏览 7 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

好多鱼好多余 2024-09-03 15:46:12

我假设您验证了这一行中的数据:

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

是用户“用户名”(在您的现实代码中“

如果没有,我会预期出现用户未知错误。


编辑1
我刚刚看到你的邮件中没有“收件人”,“只有”一个抄送,你的邮件服务器可能不这样(我的不介意,所以:-),或者是在修剪邮件时“发生”了这种情况代码?


编辑2
我能够通过替换该行

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

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

重现您的错误,您需要为邮件服务器提供一个有效的地址来发送消息!当我提供现有的收件人地址时(Cc=>'[email protected] ]' 行..成功了!

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!

木有鱼丸 2024-09-03 15:46:12

您应该在 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 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?

心是晴朗的。 2024-09-03 15:46:12

也许您可以尝试使用 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.

蓝戈者 2024-09-03 15:46:12

Comcast 已转向 SSMTP,因此我必须修改我的自动消息。

下面是 Comcast 邮件服务器的一些工作 PERL 代码。希望您觉得它有帮助。

#!/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 ('[email protected]');

# Email to
$smtp->to ('[email protected]');

$smtp->data();

$smtp->datasend("To: [email protected]\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;

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 ('[email protected]');

# Email to
$smtp->to ('[email protected]');

$smtp->data();

$smtp->datasend("To: [email protected]\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;
城歌 2024-09-03 15:46:12

您收到的错误是 RCPT TO 错误,而不是身份验证错误。这可能是因为您实际上尝试向不存在的用户发送邮件,或者因为您尝试在未进行身份验证的情况下通过服务器进行中继。

如果看起来 Mail::Sendmail 不支持 SMTP 身份验证方法(
http://metacpan.org/pod/Mail::Sendmail#LIMITATIONS )所以很可能甚至没有尝试过身份验证。

您应该在创建 Net::SMTP:SSL 实例后立即使用它的 auth 方法:

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

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文