Perl邮件程序错误

时间:2017-11-12 15:28:13

标签: perl

sub send_mail_via_smtp
{
    my( %p ) = @_;

    eval 'use Net::SMTP::TLS';

    my $repository = $p{session}->get_repository;

    my $smtphost = $repository->get_conf( 'smtp_server' );
    ##my $smtphost = 'smtp.gmail.com';

    if( !defined $smtphost )
    {
        $repository->log( "No STMP host has been defined. To fix this, find the full\naddress of your SMTP server (eg. smtp.example.com) and add it\nas the value of smtp_server in\nperl_lib/EPrints/SystemSettings.pm" );
        return( 0 );
    }


    my $smtp = new Net::SMTP::TLS(
           $smtphost,
        Hello   =>      'server.example.org',
        Port    =>      587,
        User    =>      'Example',
        Password=>      'Example',
        );

    # my $smtp = Net::SMTP->new( $smtphost);

    if( !defined $smtp )
    {
        $repository->log( "Failed to create smtp connection to $smtphost" );
        return( 0 );
    }

    $smtp->mail( $p{from_email} );
    $smtp->to( $p{to_email} );

    # if( !$smtp->recipient( $p{to_email} ) )   
    # {
    #    $repository->log( "smtp server refused <$p{to_email}>" );
    #    $smtp->quit;
    #    return 0;
    # }

    my $message = build_email( %p );
    my $data = $message->as_string;
    # Send the message as bytes, to avoid Net::Cmd wide-character warnings
    utf8::encode($data);
    $smtp->data;
    $smtp->datasend( $data );
    $smtp->dataend;
    $smtp->quit;

    return 1;
}

这是我的邮件代码,但是当我运行此错误时

找不到对象方法&#34; new&#34;通过包&#34; Net :: SMTP :: TLS&#34; (也许你忘了加载&#34; Net :: SMTP :: TLS&#34;?)

我尝试使用没有tls的邮件但是没有工作

1 个答案:

答案 0 :(得分:2)

之后

eval 'use Net::SMTP::TLS';

添加此行

die $@ if $@; 

这应该给出一条消息,说明为什么Net :: SMTP :: TLS没有加载