I am using gmail SMTP to send the mail with the help of phpmailer library. It is sending mails fine but it is not sending from the mail address that I am setting in SetFrom address. Here is my code:
<?php
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "[email protected]";
$mail->Password = "gmail_password";
$mail->From = '[email protected]';
$mail->FromName = 'Admin';
$mail->AddAddress('[email protected]', 'Receiver'); // Add a recipient
$mail->IsHTML(true);
$mail->Subject = 'Here is the Subject';
$mail->WordWrap = 50;
$mail->Body = "This is in <b>Blod Text</b>";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>
It is sending mail from [email protected] but I want it to send with '[email protected]' as set in $mail->From. Any help will be highly appreciated.
Yes, this is a Google Mail restriction. The "From" email address must match or is automatically set that way by Google SMTP.
My solution was to add
$mail->AddReplyTo($FromEmail, $FromName);
This way at least if you reply to the message it will be delivered as described