0
votes

I'm trying to send a mail through PHP mailer, but I'm getting this error. anyone know how to fix it, it tried multiple times but failed

Something went wrong :( PHPMailer\PHPMailer\Exception: SMTP Error: The following recipients failed: [email protected]: "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}

this is my PHP code. thank you in advance

    <?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 = "[email protected]";
$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 = '[email protected]';
    $mail->Password = 'xxxxxxxxxx';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    //Recipients
    $mail->setFrom('[email protected]','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;
    }
}


?>
1
Are you trying this locally or on a hosting?Javier Olayo

1 Answers

0
votes

This is because the mail server at sevnstaging.website is not permitted to send email from gmail.com addresses, as it says:

Your domain gmail.com is not allowed in header From

Gmail.com has this SPF record:

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

The netblocks this refers to do not include the server at sevnstaging.website, so this would result in delivery problems. The rejection itself may be happening for three reasons:

  • sevnstaging.website may only allow you to use a from address from its own domain, or using your username ([email protected])
  • sevnstaging.website may check the SPF record at gmail.com to see whether it is permitted to send from a gmail.com address, see a failure, and reject your sending attempt
  • It may simply deny sending using gmail.com from addresses via its own heuristics.

Generally speaking, if you want to send form a gmail.com address, you need to send through a gmail.com mail server.

Also bear in mind that gmail is relatively relaxed about this - other domains (such as all Yahoo domains) are far stricter.