PHP邮件发送异常求助:仅部分邮箱可接收,主流邮箱无法送达
Hey Dylane, let's break down why your emails aren't reaching major providers and how to fix it—these services have strict anti-spam and authentication rules that your current code isn't accounting for. Here's what you need to do:
1. Fix Line Endings Immediately
Your current code switches between \r\n and \n based on the recipient's email, but MIME standards require consistent \r\n line endings for all email headers and content. Using just \n can cause header parsing errors, which triggers spam filters or outright rejection.
Remove the preg_match check and hardcode this instead:
$passage_ligne = "\r\n";
2. Add Critical DNS Authentication Records (SPF, DKIM, DMARC)
This is the #1 reason mainstream providers reject emails—they can't verify your domain has authorized the sender. You need to add these three records to your domain's DNS settings:
- SPF: A TXT record that lists which servers are allowed to send emails from your domain. Example value:
v=spf1 a mx include:your-hosting-provider.com ~all
(Replaceyour-hosting-provider.comwith your actual hosting or SMTP provider's SPF include.) - DKIM: Generates a cryptographic signature for your emails, proving they weren't tampered with. You'll need to generate a public/private key pair (most hosting providers have tools for this), add the public key as a TXT record to your domain, and configure your email code to sign messages with the private key.
- DMARC: A TXT record that tells providers how to handle unauthenticated emails from your domain (e.g., reject or quarantine). Example value:
v=DMARC1; p=quarantine; sp=quarantine; rua=mailto:dmarc@yourdomain.com
3. Ditch the Native mail() Function for a Proper Library
The built-in mail() function is low-level and prone to human error when handling MIME, authentication, and modern email standards. Libraries like PHPMailer or SwiftMailer handle all these details automatically.
Here's a simplified PHPMailer replacement for your code:
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; // Load PHPMailer (install via Composer or download the files) require 'path/to/PHPMailer/src/Exception.php'; require 'path/to/PHPMailer/src/PHPMailer.php'; require 'path/to/PHPMailer/src/SMTP.php'; $mail = new PHPMailer(true); try { // SMTP Configuration (use your hosting provider's SMTP details) $mail->isSMTP(); $mail->Host = 'smtp.yourdomain.com'; $mail->SMTPAuth = true; $mail->Username = 'my@domain.com'; $mail->Password = 'your-email-password-or-app-specific-password'; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587; // Sender & Recipient $mail->setFrom('my@domain.com', 'Something'); $mail->addAddress('recipient@example.com'); // Replace with your target email $mail->addReplyTo('my@domain.com', 'Something'); // Email Content $mail->isHTML(true); $mail->Subject = 'Some subject'; $mail->Body = '<html><head></head><body> HTML TEXT</body></html>'; $mail->AltBody = 'TEXT goes here!'; // Send & Redirect $mail->send(); header('location:../index.php'); } catch (Exception $e) { echo "Message failed to send: {$mail->ErrorInfo}"; } ?>
PHPMailer handles MIME boundaries, correct line endings, and makes it easy to add DKIM signatures if needed.
4. Avoid Spam Triggers
- Ensure your
FromandReply-Toaddresses match your domain (no random free email addresses). - Avoid spammy keywords in your subject line or content (e.g., "FREE", "WIN", excessive capitalization).
- Make sure your HTML content is well-formed (no broken tags).
5. Test and Debug
- Check your server's mail logs (usually in
/var/log/mail.logor your hosting control panel) for bounce messages—they'll tell you exactly why an email was rejected (e.g., "SPF check failed"). - Use free deliverability tools to scan your emails for spam score issues and validate your DNS records.
These steps should resolve most delivery issues with Gmail, Outlook, and Yahoo. The DNS records are especially critical—don't skip those!
内容的提问来源于stack exchange,提问作者Dylane EBANG

