如何在perl中编写Net::SMTP电子邮件



我的主机都没有更高的电子邮件模块,所以我宁愿在没有其他模块的情况下让它工作。

我尝试过使用谷歌smtp服务器(我在XAMPP中使用\sendmail,但从未通过Net::SMTP(,我尝试过 secureserver.net smtp服务器。 带和不带 SSL,带和不带 Hello 行,端口 465 或 25。 尝试从我的本地 XAMPP 服务器运行禁用所有防火墙,尝试在 goDaddy 服务器上远程运行它。

似乎超时并且"SMTP 失败"。 永远无法获得"..."即使没有除非($smtp(测试。 无法通过身份验证,但由于"SMTP 失败"正在触发,我认为这是我如何形成对象的初始声明的问题? 尽管我已经以几种不同的方式尝试过这种方法,但我没有得到任何实际的服务器错误。

my $smtp = Net::SMTP->new($smtpserver,
SSL => 1,                   
Timeout => 60,
Port => $smtpport,  # tried 25 and 465
Debug => 1
);
unless($smtp) { #tried with and without this
print "smtp failed<br>[".$!."]<br>and<br>".$IO::Socket::SSL::SSL_ERROR;
}
$smtp->auth($smtpuser, $smtppassword);    #nothing under this line is done
$smtp->mail($smtpuser);            #but i have checked and double checked the data
$smtp->recipient($recipi);         #pointed to several email addys but never arrive
$smtp->data();
$smtp->datasend("From: $smtpuser");
$smtp->datasend("To: $recipi");
$smtp->datasend("Subject: test mail ver six sm loc 12"); 
$smtp->datasend("n");
$smtp->datasend("blah blah");
$smtp->dataend();
$smtp->quit;
print"...";  #never gets printed

欢迎任何建议,我唯一想到但没有尝试的是将身份验证包含在初始对象减速中,我找不到显示如何做到这一点的地方。

编辑; 添加了 $! 和 $IO::Socket::SSL::SSL_ERRORSteffen 建议结果是 $! = [错误的文件描述符] 和 $IO::Socket::SSL::SSL_ERROR不返回任何内容。

尝试以下代码(它需要 Authen::SASL 模块,它可能不是您设置的一部分(

use strict;
use warnings;
use utf8;
use Net::SMTP 3.0;
use Authen::SASL qw(Perl);
my $sender = my $user = 'your_id@gmail.com';
my $password = 'your_password';
my $receiver = 'your_receiver@gmail.com';
my $smtp = Net::SMTP->new(
'smtp.gmail.com',
SSL=>1,
Timeout => 20,
Debug   => 1,
);
die "Initialization failed: $!" if !defined $smtp;
print "Trying to authenticate..";
$smtp->auth( $user, $password) or die "could not authenticaten";
my $header = 
"To: $receiver
From: $sender
Content-Type: text/html
Subject: Testing Net::SMTP
";
my $body = "
The <i>body</i> of the <b>email</b> Net-SMTP example
";
$smtp->mail( $sender );
$smtp->to( $receiver );
$smtp->data();
$smtp->datasend( $header );
$smtp->datasend( $body );
$smtp->datasend( '' );
$smtp->dataend();
$smtp->quit();

Authen::SASL(最后更新于 2012 年(现在似乎不需要。我不知道为什么。

此代码足以发送到 Gmail:

use v5.32;
use warnings;
use Net::SMTP;
my %smtp = (
host => 'smtp.gmail.com',
from     => '', # also: user
password => q(),     # app password: https://support.google.com/accounts/answer/185833
to       => '',
subject  => 'Net::SMTP: Hello',
);
my $smtp = Net::SMTP->new($smtp{host},
SSL   => 1,
Debug => 1,
);
$smtp->auth($smtp{from}, $smtp{password});;
my $header = <<"HEADER";
To: $smtp{to}
From: $smtp{from}
Content-Type: text/html
Subject: $smtp{subject}
HEADER
my $body = <<"BODY";
The <i>body</i> of the <b>email</b> Net-SMTP example
BODY
$smtp->mail($smtp{from}); # SMTP: MAIL FROM
$smtp->to($smtp{to}); # SMTP: RCPT TO
$smtp->data(); # SMTP: DATA
$smtp->datasend($header);
$smtp->datasend($body);
$smtp->dataend(); # SMTP: .
$smtp->quit(); # SMTP: QUIT

这是对先前答案的修改,带有方法解释注释,并且没有过多的代码,例如SSL模块和空字符串datasend

我使用以下代码:

use Net::SMTP::SSL;
my $smtp = Net::SMTP::SSL->new(
Host=>$host,
Hello=>$host,
Port=>465,
Debug=>1
);
$smtp->auth($smtp_user, $smtp_pass);
# ...

最新更新