0

在我的 Perl 代码中,我使用 Net::SMTP 和 MIME:Entity 通过我的 Sendgrid 帐户发送电子邮件(来自他们的 v2 api,我可以使用我的帐户登录作为 api)...

它工作完美。

但是我们将服务器移到了更新的版本。仍然是 Unix (Linux),但具有更新的硬件,包括更新版本的 Plesk。IP 地址都是一样的,我们把它们移到了新的硬件上。我们确实更改了作为主要 IP 地址的 IP 地址,但我们确保将其添加到 sendgrid whitelabel...

所以我不知道为什么它会停止工作。我在代码中没有收到任何错误,api 正在连接。但无论我做什么,我都没有看到任何电子邮件。

由于没有错误,不知道为什么它不起作用。

这是我的代码,你看到会导致它失败的缺陷吗?

sub Send_Multi_Part_Email_MimeLite {
    my ($__to,$__cc,$__bcc,$__from,$__subject,$__html_message,$__text_message,$__importance,$__xpriority,$__x_ms_priority) = @_;
    if($__x_ms_priority && ($__x_ms_priority =~ /\|/)) {
        ($__x_ms_priority,$_attachFile,$_attachFilePath) = split /\|/, $__x_ms_priority, 3;
    }
    if($__xpriority && ($__xpriority =~ /\|/)) {
        ($__xpriority,$_unSubKey,$_mbrUn) = split /\|/, $__xpriority, 3;
    }
    if($__importance && ($__importance =~ /\|/)) {
        ($__importance,$_emailType) = split /\|/, $__importance, 2;
    }

    my $_useNewSystem = 1; # Declare new system... set to 1 to use it and not use MimeLite any longer...
    if($_useNewSystem) {
        use MIME::Entity;
        use Net::SMTP;

        my $_sendIp = $ENV{REMOTE_ADDR} || "127.0.0.1";
        if($__to && ($__to =~ /\;/)) {
            $__to =~ s/\;/\,/g;
        }
        if($__cc && ($__cc=~ /\;/)) {
            $__cc =~ s/\;/\,/g;
        }
        if(!$__bcc) {
            $__bcc = 'testmailacct@gmail.com'; # not real, but when in production is a real email, so we can get emails to confirm they are being delivered... only when debugging... Commented out if not in test mode...
        }

        if($__bcc && ($__bcc =~ /\;/)) {
            $_bcc =~ s/\;/\,/g;
        }
        if(!$__html_message) {
            $__html_message = $__text_message;
            $__html_message =~ s/\n/br()."\n"/eg;
            $__html_message =~ s/\cM\cJ/br()."\n"/eg;
        }

        $_emailSentType = "";


        $mime = MIME::Entity->build(Type  => 'multipart/alternative',
                                Encoding => '-SUGGEST',
                                From => $__from,
                                To => $__to,
                                Subject => $__subject,
                                'Importance'    => $__importance,
                                "X-Mailer"      => "$_co_domain Sendgrid Mailer - Version 2.0",
                                'X-Organization' => "$_co_name",
                                "X-Mail-Sent-For" => "The Club or www.$_co_domain/$_un; From IP: $_sendIp",
                                'X-Priority' => $__xpriority,
                                'X-MSMail-Priority' => $__x_ms_priority
        );

        $_sendKey = ""; # removed for security...

        $_removeLink = qq~https://www.testing.com/backoffice.cgi?page=unsubscribe$_sendKey~;
        $__text_message .= qq~

    You are receiving this email as a club member of The $_co_name. You can turn off your
    email subscription by logging into your back office with your username and password
    that you registered with. Or you may click:
    $_removeLink to remove yourself.
    ~;


        if($__html_message) {
            $__html_message .= qq~<br>
            <br>
    <span style="font-size: 12px; color: #808040;">
    You are receiving this email as a club member of The $_co_name.<br>
    You can turn off your email subscription by logging into your back office<br>
    with your username and password that you registered with.<br>
    Or you may click: <a href="$_removeLink">Here</a> to remove yourself,<br>
    or call our Customer Care Center at (888)123-4567 for assistance<br>
    <br>
    <br></span>
    <br>
    ~;

        }

    $mime->attach(Type => 'text/plain',
                 Encoding =>'-SUGGEST',
                 Data => $__text_message);

    $mime->attach(Type => 'text/html',
                 Encoding =>'-SUGGEST',
                 Data => $__html_message);

        if($_attachFile) {
        ### Attach stuff to it:
        if($_attachFilePath && ($_attachFilePath =~ /csv$/i)) {
            $_mimType = "text/csv";
            $_mimEncoding = "US-ASCII";
        } elsif($_attachFilePath && ($_attachFilePath =~ /gif$/i)) {
            $_mimType = "image/gif";
            $_mimEncoding = "base64";
        } elsif($_attachFilePath && ($_attachFilePath =~ /jpg$/i)) {
            $_mimType = "images/jpeg";
            $_mimEncoding = "base64";
        } elsif($_attachFilePath && ($_attachFilePath =~ /png$/i)) {
            $_mimType = "images/png";
            $_mimEncoding = "base64";
        } else {
            $_mimType = "text/plain";
            $_mimEncoding = "UTF-8";
        }
        # Attach the file that it sent...
        $mime->attach(Path     => $_attachFilePath,
                     Type     => "$_mimType",
                 Encoding => "$_mimEncoding");
      }


        # Sendgrid Login credentials
        $username = 'myloginemail';
        $password = "myloginpass";

        # Open a connection to the SendGrid mail server
        $smtp = Net::SMTP->new('smtp.sendgrid.net',
                                    Port=> 587,
                                    Timeout => 60,
                                    Hello => "testing.com", Debug => 1) or return("0","could not establish connection!: $!");

        # Authenticate
        if($smtp) {
            $smtp->auth($username, $password);
            # Send the rest of the SMTP stuff to the server
            $smtp->mail($__from);
            $smtp->to($__to);
            $smtp->data($mime->stringify);
            $smtp->quit();

            open(DEB,">>/home/path/files/aa_mime_email_debug_tracking.txt");
            seek(DEB,0,2);
            $_resultMsg = $smtp->message();
            $_resultMsg =~ s/\n//eg;
            $_resultMsg =~ s/\cM\cJ//eg;


            # just added this, not sure if it will work... want to create a loop of the $smtp to put it in the debug file, so I can see what is happening and why email does not go anywhere, but connection works...
            $_resultMsg2 = "";
              foreach (keys %{$smtp}) {
                $_resultMsg2 .= "," if $_resultMsg2;
                # Test this one:
              $_resultMsg2 .= " $_ => ${$hash_ref}{$_}";
              # Then test this one:
              $_resultMsg2 .= ", $_ = " . ${$hash_ref}->{$_};
            }

            print DEB qq~Result: '~ . $_resultMsg . qq~'; smtpTest: '~ . $_resultMsg2 . qq~'; From => "$__from", To => "$__to", Subject => "$__subject", If Member Username passed: "$_mbrUn", on: ~ . Format_Date_For_Viewing(time(),"") . "\n";
            close(DEB);
            return (1,"");
        } else {

            open(DEB,">>/home/path/files/aa_mime_email_debug_tracking.txt");
            seek(DEB,0,2);
            print DEB qq~ERROR: '~ . $smtp->message() . qq~' (Sendgrid error!!!) - From => "$__from", To => "$__to", Subject => "$__subject", If Member Username passed: "$_mbrUn", on: ~ . Format_Date_For_Viewing(time(),"") . "\n";
            close(DEB);
            return (0,$smtp->message());
        }
    } else {
        # Code for old email system... not using sendgrid, using sendmail... no longer in use, but code left there for times when sendgrid not working or something... used MimeLite
    }
}

无论如何,我不知道为什么它不起作用。服务器管理员检查并没有错误或队列中没有电子邮件......所以没有任何东西卡在某个地方,我们可以找到......

除了我糟糕的编程之外,有人能看到什么吗???哈哈

谢谢,-理查德

4

1 回答 1

0

如果程序以前工作过,没有改变,并且没有给出任何错误,那么它可能没有任何问题。测试手动发送电子邮件。

首先对您的用户名和密码进行编码:

perl -MMIME::Base64 -e 'print encode_base64 $_ for qw/myloginemail myloginpass/'

然后在第一个 334 之后使用带有编码用户名的 telnet,在第二个之后使用密码:

telnet smtp.sendgrid.net 587
Trying 167.89.118.51...
Connected to smtp.sendgrid.net.
Escape character is '^]'.
220 SG ESMTP service ready at ismtpd0011p1las1.sendgrid.net
helo testing.com
250 Hello, nice to meet you
auth login
334 VXNlcm5hbWU6
bXlsb2dpbmVtYWls
334 UGFzc3dvcmQ6
bXlsb2dpbnBhc3M=
235 Authentication succeeded
mail from:<testmailacct@gmail.com>
250 2.1.5 Ok
rcpt to:<testmailacct@gmail.com>
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
subject: test

This is test 0.
.
250 2.0.0 Ok: queued as BBAEA3483C73
quit
221 2.0.0 Bye
Connection closed by foreign host.
于 2017-09-14T12:05:02.997 回答