I’m having trouble with my email setup using PHPMailer and Gmail’s SMTP server. My situation is a bit confusing so let me explain.
I have an email address that uses a custom domain but it’s actually hosted on Gmail (like a G Suite or Google Workspace account). When I try to use this account to send emails through my PHP script, nothing gets delivered.
However, if I switch to a regular @gmail.com address, the emails do send but only to other Gmail users. Non-Gmail recipients never receive anything.
Here’s my current implementation:
function sendEmailViaSMTP($recipient, $sender, $senderName, $emailTitle, $content) {
global $errorMsg;
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPDebug = 0;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'ssl';
$mailer->Host = 'smtp.gmail.com';
$mailer->IsHTML(true);
$mailer->Port = 465;
$mailer->Username = EMAIL_USER;
$mailer->Password = EMAIL_PASS;
$mailer->SetFrom($sender, $senderName);
$mailer->Subject = $emailTitle;
$mailer->Body = $content;
$mailer->AddAddress($recipient);
if(!$mailer->Send()) {
$errorMsg = 'Email failed: '.$mailer->ErrorInfo;
return false;
} else {
$errorMsg = 'Email delivered successfully!';
return true;
}
}
What could be causing this issue? Any suggestions would be helpful.