PHP中mail()函数添加邮件文本版本及SpamAssassin优化咨询
Hey there! Let's break down your two PHP email questions step by step:
mail() Function Including both plain-text and HTML versions of your email is a best practice—it ensures compatibility with email clients that don’t render HTML, and it can even help reduce your spam score. Here’s a straightforward way to implement this with PHP’s built-in mail() function by creating a multi-part MIME message:
// Recipient details $to = 'user@example.com'; $subject = 'Your Email Subject'; // Generate a unique boundary to separate content sections $boundary = uniqid('email-boundary-'); // Build the email headers $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n"; $headers .= "From: Your Brand <noreply@yourdomain.com>\r\n"; // Plain-text content (keep it concise, mirror HTML key info) $plainContent = "Hi,\n\nThis is the plain-text version of your email. All important details from the HTML version should be here.\n\nThanks,\nYour Brand Team"; // HTML content $htmlContent = "<html><body> <p>Hi,</p> <p>This is the <strong>HTML version</strong> of your email—feel free to add styling here.</p> <p>Thanks,<br>Your Brand Team</p> </body></html>"; // Assemble the multi-part message $message = "--$boundary\r\n"; $message .= "Content-Type: text/plain; charset=utf-8\r\n"; $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $message .= $plainContent . "\r\n\r\n"; $message .= "--$boundary\r\n"; $message .= "Content-Type: text/html; charset=utf-8\r\n"; $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $message .= $htmlContent . "\r\n\r\n"; // Close the boundary $message .= "--$boundary--"; // Send the email mail($to, $subject, $message, $headers);
The critical part here is setting the multipart/alternative content type and using a unique boundary to split the plain-text and HTML sections. Make sure both versions contain the same core information—don’t skip details in the plain-text version, as some spam filters penalize mismatched content.
Since you haven’t shared your specific script, I’ll cover the most common SpamAssassin triggers and fixes that apply to nearly all PHP email setups:
- Clean up your email headers:
- Use a
Fromaddress tied to your own domain (avoid free services like Gmail unless you’ve configured proper authentication). - Add a valid
Reply-Toaddress that matches your domain—this helps with sender verification. - Set a
Return-Pathto handle bounce notifications, which SpamAssassin uses to confirm legitimate sender practices.
- Use a
- Authenticate your domain:
- Set up SPF, DKIM, and DMARC records in your domain’s DNS. These records prove you’re authorized to send emails from your domain, and SpamAssassin awards positive points for valid authentication.
- Avoid spam red flags:
- Steer clear of overused spam terms (e.g., "free", "win", "act now") and excessive uppercase text.
- Limit the number of hyperlinks, especially links to untrusted or unfamiliar domains.
- Include a clear unsubscribe option:
- Not only is this required by anti-spam laws (like CAN-SPAM), but SpamAssassin also views this as a sign of legitimate email.
- Test directly with SpamAssassin:
- Save a copy of your sent email as an
.emlfile, then runspamassassin -t < your-email.emlon a server with SpamAssassin installed. This will give you a detailed breakdown of which rules are triggering points and exactly how to fix them.
- Save a copy of your sent email as an
- Avoid shared hosting IP pitfalls:
- If you’re using shared hosting, your server’s IP might have a spam history. Consider using a dedicated email service or a dedicated IP for sending emails to improve deliverability.
内容的提问来源于stack exchange,提问作者user5328507

