Search code examples
phpsmtpgmailphpmailer

Unable to send emails to gmail addresses using phpmailer


Let me preface this by saying that I am not trying to use gmail as my smtp server. I am able to use PHPMailer to send emails to most all addresses from my host (hostgator) using my domains smtp server settings except for sending email to gmail addresses. Email sent to gmail addresses just never arrive. As far as I can tell, I am not getting any error messages.

What do I need to do? What is it that gmail is blocking so that people with gmail accounts cannot get email from some online email forms? What needs to be done to get around it?

Here is the code that I am using:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require "PHPMailer/src/PHPMailer.php";
require "PHPMailer/src/SMTP.php";
require "PHPMailer/src/Exception.php";

$mail = new PHPMailer(TRUE);
$mail->isSMTP();
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPAuth = true;
$mail->Host = "mail.mydomain.com";
$mail->Port = 465;
$mail->Username = "smtpusername";
$mail->Password = "smtppassword";
$mail->setFrom($FromEmail,$FromName);
$mail->addAddress("[email protected]","Fred");
$mail->addCC($FromEmail,$FromName);
$mail->addBCC("[email protected]","Webmaster");
$mail->Subject = "Email from contact form";
$mail->Body = $EmailMessage;
$mail->send();
$mail = null;

Solution

  • Got it figured out. It was actually a fairly simple answer to a frustrating issue. I hope that this will help others. The secret is that the "From address" must be from the same domain as the domain that is sending the email. It's that simple. I know that most of us would love to have emails sent from website contact forms be from the person sending the email so when someone reaches out to us we can just hit reply to respond to their email. This will work most of the time, with the exception of gmail addresses (and maybe some other online email services).

    So, the work around is this, make sure the from email address is the same as the domain that it is being sent from. If your form is on mywebsite.com, then the from email address must be from [email protected]. It doesn't even have to be a legitimate email address, but the domain needs to be matching.

    To get around the reply to issue (so that the person receiving the email can just simply hit reply to reply to the person that submitted the contact form), make sure that you add a reply to address (although, after testing I am finding out that with gmail, if the reply to is different from the From address, when you reply, the from address will be used).

    Here is sample code. It works:

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    
    require "PHPMailer/src/PHPMailer.php";
    require "PHPMailer/src/SMTP.php";
    require "PHPMailer/src/Exception.php";
    
    $EmailMessage = "The following message was generated from https://somefakedomain.org/contact : " . chr(10) . chr(13) . 
                            "From : " . $FromName . " (" . $FromEmail . ")" .  chr(10) . chr(13) . 
                            "To : " . $SendTo .  chr(10) . chr(13) . 
                            "At : " . date("F d, Y G:i") . chr(10) . chr(13) . 
                            $EmailMessage;
    
    $mail = new PHPMailer(TRUE);
    $mail->isSMTP();
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->SMTPAuth = true;
    $mail->Host = "mail.somefakedomain.org";
    $mail->Port = 465;
    $mail->Username = "smtpusername";
    $mail->Password = "smtppassword";
    $mail->isHTML(FALSE);
    $mail->setFrom("[email protected]","Webmaster"); /* The From email address MUST be the same as the domain the email is being sent from */
    $mail->AddReplyTo($FromEmail,$FromName); /* The reply to address works most all of the time, however gmail will use the From Email address if this is different */
    $mail->addAddress("[email protected]","Elvis");
    $mail->addCC($FromEmail,$FromName);
    $mail->addBCC("[email protected]","Webmaster");
    $mail->Subject = "Email from contact form";
    $mail->Body = $EmailMessage;
    $mail->send();
    $mail = null;
    

    I hope that this will help others. It's a fairly simple answer to a very frustrating issue.