PHP mailer returning an error PHPMailer\PHPMailer\Exception: SMTP Error: The following recipients failed:

PHP mailer returning an error PHPMailer\PHPMailer\Exception: SMTP Error: The following recipients failed:

我正在尝试通过 PHP 邮寄程序发送邮件,但出现此错误。任何人都知道如何修复它,它尝试了多次但失败了

Something went wrong :( PHPMailer\PHPMailer\Exception: SMTP Error: The following recipients failed: mohamedanushkar@gmail.com: "Your IP: 68.65.121.178 : Your domain gmail.com is not allowed in header From" in /home/sevnowsc/howmuch.sevnstaging.website/wp/PHPMailer-master/src/PHPMailer.php:1820 Stack trace: #0 /home/sevnowsc/howmuch.sevnstaging.website/wp/PHPMailer-master/src/PHPMailer.php(1513): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Thu, 8 Oc...', '\r\n\r...') #1 /home/sevnowsc/howmuch.sevnstaging.website/wp/PHPMailer-master/src/PHPMailer.php(1352): PHPMailer\PHPMailer\PHPMailer->postSend() #2 /home/sevnowsc/howmuch.sevnstaging.website/wp/php/form.php(75): PHPMailer\PHPMailer\PHPMailer->send() #3 {main}

这是我的 PHP 代码。提前谢谢你

    <?php

$errorMSG = "";

// FIRST NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}


// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

$message = $_POST["message"];


$EmailTo = "mohamedanushkar@gmail.com";
$reference_id = uniqid();
$Subject = "Howmuch" . $reference_id;

$Body = "<html>
<head>
  <title></title>
</head>
<body>
<div  style='border: 5px solid #141348; padding:20px;'>
<h2>Person's details</h2>
<p> Full Name: $name </p>
<p>Email: $email </p>
<p>Address: $message</p>


</body>
</html>";


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

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


$mail = new PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = -1;
    $mail->isSMTP();
    $mail->Host = 'sevnstaging.website';
    $mail->SMTPAuth = true;
    $mail->Username = 'info@sevnstaging.website';
    $mail->Password = 'xxxxxxxxxx';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    //Recipients
    $mail->setFrom('mohamedanushkar@gmail.com','How much');
    $mail->addAddress($EmailTo);
    $mail->addReplyTo($email);
   // $mail->addCC($email);

    //Content
    $mail->isHTML(true);
    $mail->Subject = $Subject;
    $mail->Body = $Body;
    $mail->AltBody = '';

    $mail->send();
    echo  "<script type='text/javascript'>window.location.href = '../thank-you.php';</script>"; 
} catch (Exception $e) {
    if ($errorMSG == "") {
        echo "Something went wrong :( <br>";
        echo $e;
    } else {
        echo $errorMSG;
    }
}


?>

这是因为 sevnstaging.website 的邮件服务器不允许从 gmail.com 个地址发送电子邮件,正如它所说:

Your domain gmail.com is not allowed in header From

Gmail.com 有此 SPF 记录:

v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all

这里引用的网络块不包括 sevnstaging.website 的服务器,因此这会导致传送问题。拒绝本身可能由于三个原因而发生:

  • sevnstaging.website 可能只允许您使用其自己域中的发件人地址,或使用您的用户名 (info@sevnstaging.website)
  • sevnstaging.website 可能会检查 gmail.com 的 SPF 记录以查看是否允许从 gmail.com 地址发送,发现失败并拒绝您的发送尝试
  • 它可能会通过自己的启发式方法简单地拒绝使用 gmail.com 从地址发送。

一般来说,如果您想从 gmail.com 地址发送,您需要通过 gmail.com 邮件服务器发送。

另请记住,gmail 对此相对宽松 - 其他域(例如所有 Yahoo 域)要严格得多。