首页 > 解决方案 > 使用我的 Phpmailer 连接到 Webmail 服务器时遇到问题

问题描述

一直在尝试使用 phpmailer 使用以下代码将邮件发送到我从 monovm.com 托管服务提供商购买的商业邮件

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
require 'src/POP3.php';

//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = "smtp.haygoldinternational.com";    
    $mail->Port       =  587;                    //Set the SMTP server to send through
    $mail->SMTPAuth   =  true;                                   //Enable SMTP authentication
    $mail->Username   = "office@haygoldinternational.com";                   //SMTP username
    $mail->Password   = "mypassword";                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
                                     //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    if(isset($_POST['btnSubmit']))
    {

            $mail->setFrom('office@haygoldinternational.com', 'HAYGOLD');
            $mail->addAddress('office@haygoldinternational.com', 'HAYGOLD');     //Add a recipient
            $mail->addReplyTo("toheebabiodun03@gmail.com", "Abiodun");
    
            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = "TESTING";
            $mail->Body    = "Just testing";
            $mail->send();
            echo 'Message Sent Successfully';
        
    }
    
} 
catch (Exception $e) 
{
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

我不断得到的错误是这个

2021-04-03 15:37:28 SERVER -> CLIENT: 220 us2.outbound.mailhostbox.com ESMTP Postfix
2021-04-03 15:37:28 CLIENT -> SERVER: EHLO haygoldinternational.com
2021-04-03 15:37:28 SERVER -> CLIENT: 250-us2.outbound.mailhostbox.com250-PIPELINING250-SIZE 41648128250-VRFY250-ETRN250-STARTTLS250-AUTH PLAIN LOGIN250-AUTH=PLAIN LOGIN250-ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2021-04-03 15:37:28 CLIENT -> SERVER: STARTTLS
2021-04-03 15:37:28 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2021-04-03 15:37:28 CLIENT -> SERVER: QUIT
2021-04-03 15:37:28 SERVER -> CLIENT:
2021-04-03 15:37:28 SMTP ERROR: QUIT command failed:
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

可能是什么原因,我能够使用相同的代码来连接我的 gmail,它工作得很好。请帮助兄弟

标签: phpphpmailer

解决方案


比我在评论中建议的要简单——您要连接的服务器不是您要求连接的服务器。您要求smtp.haygoldinternational.com但已连接到us2.outbound.mailhostbox.com. 这将使证书验证失败,就像它检测到名称不匹配时一样。

更改您的Host值以匹配它(如果它是预期的),或者找出您被重定向到那里的原因。


推荐阅读