PHP:使用PHPMailer跨服务器发送邮件遇报错求助
Hey there! Let's walk through troubleshooting this PHPMailer issue where you're trying to send emails from server2.com using a mailbox created on Server1 via DirectAdmin. I’ve dealt with tons of these cross-server email setups, so here’s what to check step by step:
1. Nail Down Your SMTP Configuration
First off, let’s make sure your PHPMailer settings are pointing to Server1’s correct SMTP details—this is where most people trip up. DirectAdmin uses standard defaults, but confirm with your host if you’re unsure:
- SMTP Host: Use Server1’s actual hostname (like
mail.server1.com) or its public IP. Don’t uselocalhost—you’re on Server2, so you need to connect remotely. - Port: Stick to
587(TLS) or465(SSL). Port25is often blocked by hosts to prevent spam, so avoid it if possible. - Authentication: Set
SMTPAuthtotrue—you need to use the full email address (e.g.,your@server1.com) and the exact password you set for that mailbox in DirectAdmin. - Encryption: Match the port with the right encryption:
'tls'for 587,'ssl'for 465.
Here’s a working config snippet to reference:
$mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->isSMTP(); $mail->Host = 'mail.server1.com'; $mail->SMTPAuth = true; $mail->Username = 'your@server1.com'; $mail->Password = 'your_directadmin_email_password'; $mail->SMTPSecure = 'tls'; $mail->Port = 587;
2. Check Server1 (DirectAdmin) Firewall & Permissions
Your Server1 might be blocking Server2’s connection or restricting the email account:
- Firewall Whitelist: Make sure Server1’s firewall allows incoming SMTP connections from Server2’s public IP. In DirectAdmin, you can add Server2’s IP to the firewall rules for ports 587 and 465.
- SMTP Access Restriction: Head to the email account settings in DirectAdmin—some hosts lock SMTP access to local IPs by default. Look for an option like "Allow external SMTP connections" and enable it for your mailbox.
- SPF/DKIM Records: Ensure Server1’s DNS has a valid SPF record that includes Server2’s IP (e.g.,
v=spf1 a mx ip4:123.45.67.89 -all). Also, enable DKIM for the domain in DirectAdmin—this not only helps deliverability but can also prevent connection errors from strict mail servers.
3. Rule Out Server2’s Network & PHP Limits
Server2 might be blocking outgoing connections or missing required PHP extensions:
- Outgoing Port Blocking: Many web hosts block ports 25, 465, or 587 to prevent spam. Contact Server2’s support to confirm these ports are open, or ask if they provide a relay server you can use instead.
- OpenSSL Extension: PHPMailer needs OpenSSL to handle TLS/SSL connections. Check if it’s enabled by running
php -m | grep opensslvia SSH on Server2, or create a quick phpinfo() page to verify. - SELinux Restrictions: If Server2 uses SELinux (common on CentOS/RHEL), it might block PHP from sending emails. Test temporarily by running
sudo setenforce 0, or add a permanent rule:sudo setsebool -P httpd_can_sendmail on.
4. Debug the Exact Error with PHPMailer
PHPMailer has built-in debugging that will tell you exactly what’s failing. Enable debug mode in your code:
$mail->SMTPDebug = 2; // Shows full SMTP conversation details $mail->Debugoutput = 'html'; // Prints debug info as readable HTML
Run the script again and look for common error clues:
- "Connection refused": Server1’s firewall is blocking Server2, or the port is closed.
- "Authentication failed": Wrong email/password, or SMTP auth is disabled for the mailbox.
- "SSL certificate problem": For testing, you can temporarily disable certificate verification (not recommended for production):
$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) );
5. Test with a Command-Line Tool First
Before assuming it’s PHPMailer, test if you can connect to Server1’s SMTP from Server2 using a simple tool like telnet or swaks:
- Telnet test:
telnet mail.server1.com 587(if it connects, you’ll see a 220 response) - Swaks test (more detailed):
swaks --to recipient@example.com --from your@server1.com --server mail.server1.com --port 587 --auth LOGIN --user your@server1.com --password your_password
If this test fails, the issue is with server-to-server connectivity, not your PHPMailer code. If it works, then you’ll need to tweak your PHPMailer setup.
内容的提问来源于stack exchange,提问作者kfir

