I'm using the PHPMailer to my website contact page , but I faced a problem , when the message is received both sender and receiver gmail are the same?
use PHPMailer\PHPMailer\PHPMailer;
if (isset($_POST['name']) && isset($_POST['email'])) {
$name = filter_var( $_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['mail'],FILTER_SANITIZE_STRING);
$subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
$body = filter_var($_POST['body'],FILTER_SANITIZE_STRING);
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
//SMTP Settings
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "@gmail.com"; //enter you email address
$mail->Password = ''; //enter you email password
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
//Email Settings
$mail->isHTML(true);
$mail->setFrom($email,$name);
$mail->addAddress("[email protected]"); //enter you email address
$mail->Subject = ($subject);
$mail->From= $email;
$mail->Body = $body;
CodePudding user response:
When sending via Gmail's SMTP servers, they ignore any From
headers to prevent abuse. Your From
will always be that of the Gmail account you're sending via.
You'll need a different email service provider if you want to send custom From
headers, but note that this is likely to wind your emails up in spam unless you've got the rights (via SPF etc.) to send email on behalf of that custom address. You might consider a Reply-To
header instead.