PHP使用SMTP发送Gmail邮件失败,Swift_TransportException报错
Hey there, let's work through this Gmail SMTP connection issue you're hitting with SwiftMailer! Since your code works perfectly with Mailtrap, we can rule out any issues with the code itself—this is definitely a problem with Gmail's configuration rules or your server's network restrictions. Here are the key fixes to try:
1. Fix the Port-Encryption Mismatch
You mentioned using port 587 with SSL encryption, which is an incorrect pairing:
- Port 465 goes with SSL encryption
- Port 587 requires TLS encryption
Even though you tested all combinations, double-check that you're using the correct matching pair for your next test—this is a super common slip-up.
2. "Less Secure Apps" is No Longer Supported
Google removed the "Allow less secure apps" option back in 2022, so that setting won't help even if you think you have it enabled. You have two solid alternatives here:
Option A: Use an App Password (Requires 2-Step Verification)
Even though you turned off 2-step verification, re-enabling it lets you generate a dedicated App Password for SMTP access:
- Log into your Google account, go to the Security settings
- Turn on 2-Step Verification
- Find the App Passwords section, generate a password specifically for your email setup
- Replace your regular account password with this App Password in your PHP code
Option B: SMTP Relay for Google Workspace Accounts
If you're using a Google Workspace (formerly G Suite) account, you can set up SMTP relay in the Workspace admin console. This lets you use your regular account password without an App Password, but you'll need to whitelist your server's IP address in the admin settings.
3. Check Server Network Restrictions
This is one of the most overlooked causes:
- Your server's firewall or security group might be blocking outbound access to ports 465 or 587. Reach out to your hosting provider to confirm these ports are open.
- Some shared hosts block SMTP ports by default to prevent spam—ask them to lift this restriction for your server.
- Test port connectivity directly on your server with commands like:
telnet smtp.gmail.com 587 # 或者用nc命令 nc -zv smtp.gmail.com 587
If these commands fail, the problem is definitely network-related.
4. Verify Your SwiftMailer Configuration
Just to be 100% sure, here's a working example of the correct Gmail SMTP setup for SwiftMailer:
require_once '/path/to/vendor/autoload.php'; // 587端口搭配TLS的正确配置 $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls')) ->setUsername('example@example.com') ->setPassword('your-app-password-here'); // 这里用App Password或者Workspace授权密码 $mailer = new Swift_Mailer($transport); // 测试发送邮件 $message = (new Swift_Message('Test Email from Gmail SMTP')) ->setFrom(['example@example.com' => 'Your Name']) ->setTo(['recipient@test.com']) ->setBody('This is a test to confirm SMTP connectivity'); $result = $mailer->send($message);
5. Check for Google Account Security Blocks
Google might flag your server's IP as an unusual login location:
- Log into your Gmail account, check the Recent activity section for any blocked login attempts
- If you see a notice about a "suspicious login", click "Allow access" and try your SMTP connection again
If all else fails, consider switching to Google's OAuth2 authentication for SMTP—it's the most secure and recommended method now, and SwiftMailer has extensions to support it.
内容的提问来源于stack exchange,提问作者Irfan Y

