0
votes

So I'm trying to send an email using current version of PHPMailer (https://github.com/PHPMailer/PHPMailer). The SMTP connection and the mail sending is working fine, except the script is not using the "SetFrom" $email variable for the senders mail, but instead the "Username" email.

I've tried using multiple forms of setting the "From" mail and nothing seems to work, although it's important to note that the "name" in the "SetForm" is set just fine, but "email" is not.

Here's the code:

    $mail = new PHPMailer;

    $mail->isSMTP();                                   
    $mail->Host = 'smtp.gmail.com';                     
    $mail->SMTPAuth = true;                            
    $mail->Username = 'USERNAME(MY EMAIL)';      
    $mail->Password = 'PASSWORD';                    
    $mail->SMTPSecure = 'tls';                        
    $mail->Port = 587;

    $mail->isHTML(true);

    $mail->SetFrom($email,$name);
    $mail->addAddress($email_received);                 

    $mail->Subject = $subject;
    $mail->Body = $user_msg;
2
I would check your $email variable from the time it is created to when it is called in $mail->SetFrom($email,$name). Basically, is the value of $email changing anywhere in the script?Denis Priebe
Echoed it just before calling SetFrom, unchanged.WarSensation
Does $email follow the [email protected] format?Denis Priebe
Yes it is in the [email protected] format.WarSensation

2 Answers

0
votes

This is a common question, asked many times on here. gmail does not allow you to set arbitrary senders - if you're sending through gmail, you have to use either your gmail address, or a pre-configured alternative. See here for google's docs on the matter. Generally it's a bad idea to try to do this anyway as it's likely to cause your message to fail SPF tests that the address owner may have configured.

0
votes

If you check header of the received mail, you will find your SetFrom email id defined as X-Google-Original-From.

It's because Gmail automatically rewrites the From line of any email you send via its SMTP server to the default 'Send mail as` email address in your Gmail or Google Apps email account Settings.

If you really want to use Gmail as the email provider, then you need to open a Google Apps account and verify that you own the domain which you are using for your email id.

There is also a workaround for this problem (but not recommended):

In your Gmail Settings, go to the 'Accounts' tab and add another email address you own in Send mail as section. This will cause Google's SMTP server to re-write the From field with whatever address you enabled as the default Send mail as address. Here, you need to enter smtp settings of your domain. So, when you send email using this setting, PHPMailer will connect to Gmail and Gmail will connect to your SMTP server before sending any email.