0

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,

Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?

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.

  • You fix the problem, by _not_ trying to fake the sender address. See https://stackoverflow.com/a/24644450/1427878, "Don't use a faux From: sender" – CBroe Aug 12 '22 at 07:28
  • Sorry if I misunderstood your answer here. you mean I am not supposed to use $_POST value in From? How do I get sender email address then? – user18707487 Aug 13 '22 at 20:32
  • No, you are not supposed to set FROM to completely arbitrary addresses, that your server is not registered as an "allowed" sender for anywhere. – CBroe Aug 15 '22 at 05:57
  • So then, I can't use a contact form at all with Gmail smtp since I can't get sender's email addresses. – user18707487 Aug 16 '22 at 18:39
  • You use _your_ address as the sender, and you set the website visitor's address as `Reply-To`, so that the receiver of the mail can reply to them right away. – CBroe Aug 17 '22 at 05:52
  • Thanks for advise. Your logic is working perfectly. Should I get rid of setFrom completely or just leave with empty value? or they don't matter? I tried with empty value on setFrom, and tried with getting rid of setFrom completely. Either way, my test email went to the recipient anyway. – user18707487 Aug 17 '22 at 23:57
  • If you don't set it, then I think it should be automatically added along the way, based on the login credentials. – CBroe Aug 18 '22 at 06:03
  • I see. By the way, I want to accept one of your answers, but I can't find the button. I went to see the instruction, they said I am supposed to have "toggle button" besides all answers but I don't see anywhere and really can't figure it out. – user18707487 Aug 19 '22 at 03:49
  • That's because so far, these are all just comments. And writing new answers to topic that have already been discussed, is rather discouraged, usually the new question gets closed as a duplicate instead. – CBroe Aug 19 '22 at 05:49

0 Answers0