As the title says, I am using PHPMailer to send messages on the contact form. I read quite a lot of questions here related to Gmail SMTP and can send messages. However, when I receive the messages from the sender, it never shows the sender's email address but only MY EMAIL address as the sender's email address but the name is sender's name.
So basically, it failed to receive the sender's email address. Everything else looks fine.
Here is my code below,
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = '465';
$mail->SMTPAuth = true;
$mail->Username = 'myemail@gmail.com';
$mail->Password = 'mypassword';
$mail->CharSet = 'UTF-8';
$mail->setFrom($your_email, $your_name);
$mail->addAddress('myemail@gmail.com');
$mail->Subject = $subject;
$mail->Body = $content;
if($mail->send()) {
$emailSent = true;
} else {
echo "Your message couldn't be sent. Please try again later.";
}
$your_email is from input inside the form and I already echoed it and it shows the sender's email address. I use the pretty much same code for receiving the email using my server, not Gmail SMTP and received the right information about the sender, so I highly doubt something is wrong with this code.
Also, I read this below,
and one of the guys suggested to get rid of
$mail->isSMTP();
And then, it actually works fine which mean I receive the email with sender's name as well as email address.
Is it bad not to use $mail->isSMTP();?
If somebody knows how to fix this problem, I would appreciated it.