j-man86 j-man86 - 1 month ago 9
PHP Question

PHPMailer $mail->From headers not working with gmail

I'm using the following code to send a mail after a form submission with the PHP mailer class https://github.com/Synchro/PHPMailer. The mail sends and is received successfully. The only thing that isn't wokring is the following:

$mail->From = $email;

is the email that a user will enter on the form (it is set with a
variable). I would like the email to appear that it's from the user who filled out the form, so I can hit reply and have it go to their email address.

However, the "from" email address is being set as
, i.e. the username from the gmail account that the PHPMailer script is sending from.

enter image description here

What am I doing wrong here, and how do I get the From email header to work?

Also, I am using Gmail to receive the mail-- maybe there's a gmail security setting that won't allow the "From" email to be "faked"???


$email = $_POST['email'];
$name = $_POST['moveName'];

$mail = new PHPMailer();

$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'example@gmail.com';
$mail->Password = 'password';

$mail->From = $email;
$mail->FromName = $name;
$mail->AddReplyTo($email, $name);


$mail->Subject = 'Quote Request';
$mail->Body = 'hey';


Answer Source

With gmail you have to configure an email address as allowed "$mail->from" first. This FAQ entry explains how to do it: https://support.google.com/mail/answer/22370?hl=en