PHPMailer Gmail SMTP not working with custom domain email addresses

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.

Had the same problem moving from regular Gmail to Google Workspace. It’s usually authentication or server config differences causing this. You might need to enable “Less secure app access” in Google Admin console, though Google’s killing that feature. Check your domain’s SPF and DKIM records first - they’re critical for delivery to non-Gmail addresses. Try switching to port 587 with TLS instead of SSL on port 465. Some hosting providers block 465. Since it partially works with regular Gmail, your code’s probably fine. The workspace account likely has extra restrictions your domain admin set up.

yeah that could be it! def need an app password if 2FA is on. just create one for smtp, your usual password won’t work. give it a shot and see if it helps!