Perl 电子邮件::填充器无法发送没有文件附件的普通电子邮件
系统:Ubuntu18.04LTS和Perl5.26.1。 @格林茨 电子邮件::填充器对我来说是新事物,文档中的示例很少。我花了几个小时尝试不同的东西,在搜索引擎上做研究,在其他论坛上搜索像这样的主题。我创建的电子邮件对象如下所示:Perl 电子邮件::填充器无法发送没有文件附件的普通电子邮件,perl,email,Perl,Email,系统:Ubuntu18.04LTS和Perl5.26.1。 @格林茨 电子邮件::填充器对我来说是新事物,文档中的示例很少。我花了几个小时尝试不同的东西,在搜索引擎上做研究,在其他论坛上搜索像这样的主题。我创建的电子邮件对象如下所示: my $email = Email::Stuffer ->text_body($body) ->subject($subjparam) ->from($from)# or use ->from
my $email = Email::Stuffer
->text_body($body)
->subject($subjparam)
->from($from)# or use ->from($email1, $email2...)
->transport(Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
username => $smtpuser,
password => $smtppw,
}));
try {
$email->send; # Send email
}
catch {
$s="$procname ERROR: Could not send email via $module\n";
if ($^E)
{
$s.="ERROR from \$^E: ".$^E."\n";
}
if ($@)
{
$s.="ERROR from Perl: ".$@."\n";
}
if ($!)
{
$s.="ERROR from c lib: ".$!."\n";
}
debugprg($s);
exit 1;
};
然后我这样发送:
my $email = Email::Stuffer
->text_body($body)
->subject($subjparam)
->from($from)# or use ->from($email1, $email2...)
->transport(Email::Sender::Transport::SMTP->new({
host => $smtpserver,
port => $smtpport,
username => $smtpuser,
password => $smtppw,
}));
try {
$email->send; # Send email
}
catch {
$s="$procname ERROR: Could not send email via $module\n";
if ($^E)
{
$s.="ERROR from \$^E: ".$^E."\n";
}
if ($@)
{
$s.="ERROR from Perl: ".$@."\n";
}
if ($!)
{
$s.="ERROR from c lib: ".$!."\n";
}
debugprg($s);
exit 1;
};
但每次我在$email->send
上遇到运行时错误“未找到文件”。在使用Email:MIME的Email::Stuffer中,我似乎找不到任何其他错误工具
我在Email::MIME中也没有看到任何错误对象
有人知道我为什么会出错吗?我根本没有附加文件
谢谢你的帮助。:)
编辑:这是我的整个测试程序。您必须提供自己的SMTP服务器信息
#!/usr/bin/perl
=pod
Put docs here.
=cut
use warnings;
use strict;
use Email::Stuffer;
use Email::Sender::Transport::SMTP ();
require '/home/chuck/perl/util2.pl';
my $VER="v1.00";
my $s='';
my $procname='';
my $prefixsp='';
my $module='Email::Stuffer';
my ($smtpserver,$smtpport,$smtpuser,$smtppw)=getconfigg(); # Get config from INI file
# Make email.
my $from='croberts@gilsongraphics.com';
my $to='croberts@gilsongraphics.com';
my $subjparam='Test email from Perl';
my @bodyarr=("This is a test email.");
my $body=join(' ', @bodyarr);
my $progdir='/home/chuck/perl/';
my $smtplogfn=$progdir.'logsmtp2020.txt';
# Do it raw.
my $email = Email::Stuffer
->to($to)
->text_body($body)
->subject($subjparam)
->from($from)# or use ->from($email1, $email2...)
->transport(Email::Sender::Transport::SMTP->new('SMTP', {
host => $smtpserver,
port => $smtpport,
username => $smtpuser,
password => $smtppw,
}));
if ((not defined $email) ) # Check for $email creation error.
{
$s=$prefixsp."$procname ERROR_undef_email: Could not create new email: $_";
debugprg($s);
return;
}
$email->send;
if ($@)
{
$s="$procname ERROR_senderror: Could not send email via $module\n";
if ($@)
{
$s.="ERROR from Perl: ".$@."\n";
}
print "$s\n";
exit 1;
}
exit; # Exit main pgm.
您正在从
$获取“未找到文件”代码>在catch块中,它是不相关的。确保您实际导入的是try/catch语法模块;只有在发生异常时才会发生catch,并且异常将始终位于$@
中。还要注意的是send
永远不会引发异常;如果您想以这种方式捕获异常,应该使用send\u或\u die
@Grinnz,好的,我现在正在使用send\u或\u die,并且得到一个错误。如何在Perl中捕获该错误文本?因为它似乎正在被发送到STDERR。它似乎也使用了错误的用户名。它正在使用“croberts@gilsongraphics.com“要登录SMTP服务器而不是$smtpuser中设置的用户。正如我所说的,您需要使用try/catch语法模块,否则由于解析器中的技术问题,您的try/catch语法将被忽略。您将从$中获得“未找到文件”代码>在catch块中,它是不相关的。确保您实际导入的是try/catch语法模块;只有在发生异常时才会发生catch,并且异常将始终位于$@
中。还要注意的是send
永远不会引发异常;如果您想以这种方式捕获异常,应该使用send\u或\u die
@Grinnz,好的,我现在正在使用send\u或\u die,并且得到一个错误。如何在Perl中捕获该错误文本?因为它似乎正在被发送到STDERR。它似乎也使用了错误的用户名。它正在使用“croberts@gilsongraphics.com“要登录SMTP服务器而不是$smtpuser中设置的用户。正如我所说,您需要使用try/catch语法模块,否则由于解析器中的技术问题,您的try/catch语法将被忽略。