2015-12-28 45 views
-1

•我的工作到Linux服务器迁移到Ubuntu的,从一个10.04更新一到12.04不能调用一个未定义的值的方法“验证”

•此服务器是负责执行几个一些Perl模块通过crontabs。

•这些Perl模块严重依赖30-40 perl扩展。

•我安装了所有Perl扩展,并且crontabs能够成功处理,除了由这些PERL扩展的较新版本引起的几个语法错误。

•我需要一些帮助来修改语法以使Perl脚本按预期进行处理。

这是我的错误信息:

2015/12/28 12:56:48 ./cms.pl 88 FATAL main - Can't call method "verify" on an undefined value at pm/Emails/Core.pm line 438. 

代码:

#=================================================================================================== 
# Send an eamil 
# Args: enable_clients?, BCC Arrayref [[email protected], ...], Hashref { email_address, email_subject, email_body } 
#=================================================================================================== 
sub pm::Emails::Core::send_email { 
    my ($self, $enable_clients, $bcc, $email) = @_; 
    # die('Invalid BCC array') unless $bcc; 
    die('Invalid Email hashref') unless ($email && $email->{email_address} && $email->{email_subject} && $email->{email_body}); 

    $email->{email_address} = trim $email->{email_address}; # Trim the email address just to be sure no invalid emails sneak in 

    my $mime = undef; 
    my $smtp = undef; 

    ### 
    # Get a handle to the logger 
    my $logger = Log::Log4perl->get_logger(); 
    die('Failed to create logger') unless $logger; 
    ### 

    ### 
    # Send the email using the local SMTP server 
    # SPAM FILTER NOTES: 
    # We are sending the email as inlined HTML. 
    # Sending the email as a multipart with HTML & PlainText is getting flagged as SPAM. 
    { 
    my $msg = join(', ', 
      (
        'Time:' . localtime(), 
        'Sending Email TO: ' . $email->{email_address}, 
      #'BCC: ' . join(',', @$bcc), 
      'SUBJECT: ' . $email->{email_subject}, 
      'Clients Enabled: ' . ($enable_clients ? 'true' : 'false') 
      ) 
      ); 
    $logger->warn($msg); 
    open(FILE, '>>/var/log/mail.log') or die('Failed to open mail log: /var/log/mail.log'); 
    print FILE $msg . "\n"; 
    close FILE; 
    } 
    ### 

    if (!defined($self->{_phpversion_})) { 
     $self->{_phpversion_} = `php -r 'print phpversion();' 2>/dev/null`; 
    } 

    ### 
    # Generate the MIME email message 
    $mime = MIME::Lite->new( 
       Subject => $email->{email_subject}, 
       To => $email->{email_address}, 
       Type => 'text/html', 
       Data => $email->{email_body}, 
       'Reply-To' => '[email protected]', 
       'Return-Path' => '[email protected]', 
       From => '[email protected]', 
       Organization => 'Testing', 
       'X-Mailer' => 'PHP' . $self->{_phpversion_} 
      ); 


    ### 
    # Check to see if we are sending the email to clients, if not then redirect to another account & update the subject 
    if ($enable_clients) { 
    $logger->warn('Sending email to clients is enabled!'); 
    } else { 
     use Sys::Hostname; 
    $logger->warn('Sending email to clients is disabled!'); 
    $email->{email_address} = '[email protected]'; 
    $email->{email_subject} = '<' . hostname . ' - ADMIN ONLY EMAIL> ' . $email->{email_subject}; 
    $mime->replace(Subject => $email->{email_subject}); 
    } 

    $mime->preamble(''); 
    $mime->top_level(1); 
    $mime = $mime->as_string(); 
    ### 

    ### 
    # Connect to the SMTP server & send the message 
    $logger->debug('Connecting to SMPT server'); 
    $smtp = Net::SMTP->new('localhost', Timeout => 60, Debug => 0, Hello => 'test.com'); 
    $logger->debug('Connected to SMPT server'); 
    ### 

    ### 
    # Verify we can send the email to the included addresses 
    foreach my $email_address (($email->{email_address}), @$bcc) { 
    $logger->debug('Verifying Email address: ' . $email_address); 
    next if $smtp->verify($email_address); 

    $logger->warn('Failed to verify email address: ' . $email_address . ', re-connecting to SMPT'); 
    $smtp = Net::SMTP->new('localhost', Timeout => 60, Debug => 1, Hello => 'test.com'); 
    die('Failed to reconnect to SMPT server') unless $smtp; 
    last; 
    } 
    ### 

    ### 
    # Send the email message 
    $smtp->mail('[email protected]'); 
    $smtp->bcc(@$bcc, { Notify => ['FAILURE','DELAY', 'SUCCESS'] }); 
    $smtp->to($email->{email_address}, { Notify => ['FAILURE','DELAY', 'SUCCESS'] }); 
    $smtp->data; # This will start the data connection for the message body 
    $smtp->datasend($mime); # This will send the data for the message body 
    $smtp->dataend; # This will end the message body and send the message to the user 
    $smtp->quit; 
    ### 

    use List::Util qw[min]; 
    sleep(min(1, int(rand(2)))); 
} 

任何帮助是极大的赞赏。

+1

你似乎在第441行定义了'$ smtp',在你尝试从它运行'verify'方法后,有3行*。你是否有理由期望它在早期被定义? – tjd

+1

你发布的代码片段没有什么意义,因为它通常是用'$ smtp = Net :: SMTP-> new(...);'*之前*尝试调用其他'$ smtp'上的方法。我猜你留下了一些东西;在代码中搜索'$ smtp'并找到前几个事件。 – ThisSuitIsBlackNot

+0

我的歉意家伙只是发布完整的代码,感谢您的反馈。 – virtual020

回答

2

您不会创建$smtp对象(使用$smtp = Net::SMTP->new(...)),直到您尝试调用其上的verify()方法后的三行。所以当然在那个时候它将会是不确定的。

如果$smtp也是在您之前没有向我们展示的代码中创建的,那么唯一可行的方法是。但假设你已经向我们展示了所有提到的$smtp,那么这个代码不可能只在旧服务器上工作。这不是一个更新版本的Perl引起的问题,这是一个永远不会有效的逻辑错误。

解决此问题的显而易见的方法是重新排序代码,以便在尝试使用它之前创建该对象。但是由于我只能看到少量的代码,我无法知道这是否会在其他地方产生连锁效应。

您是否考虑过支付Perl程序员来帮助您执行这些迁移?从StackOverflow的期待免费咨询是不是一个真正的可持续的商业模式: -/

更新:好了,现在你已经添加了更多的代码,我们可以看到,$smtp是调用初始化前几行verify。那么为什么你会得到这个错误?

如果你读了documentation for Net::SMTP,在描述new()方法的部分,它说:

如果失败undef将被退回,并[email protected]将包含 失败的原因。

看起来这是发生了什么。但是你的代码没有检查new()的返回代码,并且假设它总能正常工作 - 这是一个非常奇怪的假设。为了弄清楚发生了什么问题,您需要在创建SMTP对象的两行中添加一些调试输出。当您有:

$smtp = Net::SMTP->new(...); 

将其更改为:

$smtp = Net::SMTP->new(...) 
    or die [email protected]; 

这样一来,如果您无法连接到SMTP服务器,你的程序将与(希望)有用的错误信息,这将使死你进一步调查。顺便说一句,我不知道你的代码来自哪里,但是现在没人真的推荐Net :: SMTP。这都是相当低级的。你会更好看Email::SenderEmail::Stuffer(这是一种有用的知识,一个Perl程序员能带给这个项目..

+1

但人们在互联网上发布的东西都是免费的!想想他们得到的曝光! – Sobrique

+0

你好戴夫再次感谢回应这个和我的第二篇文章。我很抱歉没有发布完整的代码,我可以看到有多么令人沮丧,当一个业余爱好者寻求帮助,甚至没有正确提出问题。 :)。你所说的话很有道理,但是由于我只剩下一些错误,我想我会放弃这一点。感谢您的反馈意见。非常感谢。 – virtual020

+0

我将运行调试并让您知道我的结果,这实际上是我遇到的最后一个问题。通过查看您的个人资料,我可以看到您是该领域的专家,感谢您花时间帮助解决此问题。 – virtual020

-1

嘿家伙只是想跟进这个问题。我尝试了所有的您的建议和。是无法得到解决

这台机器上运行的SMTP /邮件的但是更深入的研究表明,它正在运行的Postfix,事实证明这个剧本是为Sendmail编写简单的做了以下内容:

卸载Postfix-

sudo apt-get purge postfix 

安装Sendmail-

sudo apt-get install sendmail 

全部被解决了,谢谢你们的一切帮助球员。

相关问题