首页 > 解决方案 > 无法让 PHP 邮件程序与 Gmail 帐户一起使用

问题描述

我正在为我的网站使用网络托管,但我无法让联系表正常工作。出于某种原因,每当我在电子邮件标签中使用 gmail 地址时,都会显示以下错误:“您的邮件无法发送!PHPMailer 错误:无法实例化邮件功能。”

联系表格接受任何其他电子邮件域。

这是我正在使用的 php 代码:

<?php

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

$myEmail = 'company@email.com';
$myPassword = 'password';
$myPersonalEmail = 'myemail@gmail.com';

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

if(isset($_POST['submit'])) {

    $mail = new PHPMailer(true);

    $mail->SMTPDebug = 0;

    $mail->Host = 'mail.domain.com';
    $mail->SMTPAuth = true;
    $mail->Username = $myEmail;
    $mail->Password = $myEmailPassword;
    $mail->SMTPSecure = 'tls';
    $mail->Port = 465;

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

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

    try {
        $mail->send();
        header("Location:this_place.html"); 
        echo 'Your message was sent successfully!';
    } catch (Exception $e) {
        echo "Your message could not be sent! PHPMailer Error: {$mail->ErrorInfo}";
    }
    
} else {
    echo "There is a problem with the contact.html document!";
}    

?>

我已经看到了许多适用于 SMTP 的解决方案,但我不知道如何将其添加到 PHP 文件中。如果有人能以我的方式发送帮助,我将不胜感激!

标签: phpcontact-form

解决方案


推荐阅读