PHPMailer无报错却不发送邮件(谷歌云生产服务器)求助
Hey there, let's figure out why your PHPMailer code reports "Message has been sent" but you're not actually receiving the email—especially since you're running this on a Google Cloud production server. Here are the key steps to diagnose and fix the issue:
1. Check Google Cloud's Outbound SMTP Restrictions
This is the most common gotcha with Google Cloud. By default, GCP blocks outbound traffic on ports 25, 465, and 587 (standard SMTP ports) for most instances to combat spam. Even if your Gmail credentials are correct, your server might not be able to reach Gmail's SMTP server at all.
- If you're using a personal Gmail account, you can request a restriction removal from Google (though approval isn't guaranteed for non-Workspace accounts).
- Alternatively, switch to a GCP-approved email delivery service like SendGrid, Mailgun, or use Google Workspace SMTP (if you have a paid Workspace account)—these services are whitelisted for outbound traffic.
2. Enable PHPMailer's Debug Mode to See What's Really Happening
Your current code has $mail->isSMTP(); commented out, which means PHPMailer might be using the server's local mail server instead of Gmail's SMTP. Let's enable SMTP mode and debug logging to get detailed insights into the send process:
<?php require ('PHPMailer/PHPMailerAutoload.php'); $mail = new PHPMailer; // Enable SMTP mode and detailed debug logging $mail->isSMTP(); $mail->SMTPDebug = 2; // Level 2 shows full SMTP conversation logs $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'allthingsremainhere@gmail.com'; $mail->Password = 'password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->SetFrom('youcanreply@lishup.com', 'Reply It! - LishUp'); $mail->AddAddress('techgoeshere@gmail.com', 'Tech'); $mail->Subject = 'First PHPMailer Message'; $mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.'; if(!$mail->send()) { echo 'Message was not sent.'; echo 'Mailer error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent.'; } ?>
Run this modified code and check the output. You'll see exactly if the server can connect to Gmail's SMTP, authenticate successfully, and if the message was accepted by Gmail. Common issues here might be connection timeouts (confirming the GCP port block) or authentication warnings.
3. Verify Spam/Junk Folders and Sent Items
Sometimes the email does get sent, but it's routed directly to the recipient's spam or junk folder. Also, check the Sent Items folder of your allthingsremainhere@gmail.com account—if the message shows up there, it means Gmail accepted it, and the issue is on the recipient's end or in transit.
4. Switch to OAuth2 Authentication (More Reliable Than "Less Secure Apps")
Google is phasing out support for "Less secure app access" even if you've enabled it. Using OAuth2 is a more secure and reliable way to authenticate PHPMailer with Gmail. PHPMailer has built-in support for OAuth2—you'll need to create a Google Cloud Project, set up OAuth credentials, and configure PHPMailer to use them. This avoids relying on the deprecated "less secure apps" setting and might bypass some of GCP's restrictions.
内容的提问来源于stack exchange,提问作者Brain Gamer

