phpmailer,代码适用于不同的电子邮件,但使用应用程序密码切换到 gmail:“Mailer 错误:无法实例化邮件功能。”

问题描述 投票:0回答:1

我有一个工作站点联系表,其中包含 PHPmailer,通过托管电子邮件进行处理。更改为 Gmail,现在表单不再发送电子邮件,而是返回“Mailer Error: Could not instantiate mail function.”。我已经从 Gmail 中提取了应用程序密码并正在使用它。有人在此处发现任何阻止使用 Gmail 发送电子邮件的问题吗?应用程序密码不再起作用了吗?

<?php

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    $myEmail = "********@gmail.com";
    $myEmailPassword = "**** **** **** ****";
    $myPersonalEmail = "**********";

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


        $mail = new PHPMailer(true);

try {

        $mail->SMTPDebug = 0;

        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = $myEmail;
        $mail->Password = $myEmailPassword;
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        $mail->setFrom($myEmail, 'Mailer');
        $mail->addAddress($myPersonalEmail);
    $mail->addReplyTo($_POST['email'], $_POST['name']);

        $mail->isHTML(true);   
    $mail->Subject = $_POST['subject']; 
        $mail->Body = $_POST['message'];


$response = $_POST["g-recaptcha-response"];

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => '********************',
        'response' => $_POST["g-recaptcha-response"]
    );
    $options = array(
        'http' => array (
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $verify = file_get_contents($url, false, $context);
    $captcha_success=json_decode($verify);

    if ($captcha_success->success==false) {
        echo "<p>Please try the captcha again.</p>";
        echo "<br><a href=\"javascript:history.go(-1)\">Go Back To Form</a>";        
    } else if ($captcha_success->success==true) {
        
        $mail->send();
    echo 'Your message was sent successfully!';
    }
        } catch (Exception $e) { // handle error.
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>

这可以发送电子邮件,直到我更改了电子邮件、密码和主机输入。更改后,我收到“消息无法发送。邮件程序错误:无法实例化邮件功能。”

php gmail phpmailer
1个回答
0
投票

你在这里犯了一个简单的错误;您还没有调用

isSMTP()
,因此您的 SMTP 设置都没有被使用,并且它尝试使用 PHP mail() 函数通过本地邮件服务器发送,但您没有配置本地邮件服务器,这结果出现此错误消息。

将此行添加到您的代码中:

$mail->isSMTP();
© www.soinside.com 2019 - 2024. All rights reserved.