You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP邮件脚本无法发送邮件求助(附代码示例)

Let’s Troubleshoot Your PHP Mail Script

First, let’s break down the obvious issues in your code that are likely stopping your emails from sending, plus some server-side checks to rule out deeper problems:

1. Invalid Sender Address

Your $from variable has two critical mistakes:

  • A leading space: " abc@gmail.com.com" → spaces are not allowed in email addresses
  • A double .com suffix: gmail.com.com isn’t a valid domain

Fix it to a clean, valid address:

$from = "abc@gmail.com";

2. Header Overwriting & Inconsistent Line Breaks

You’re setting headers twice, which overwrites the first set entirely. Also, mixing \r\n and \n for line breaks can cause issues with strict mail servers (RFC standards require \r\n for header separators).

Build your headers consistently instead:

$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "Reply-To: $from\r\n"; // Helps with deliverability and user replies

3. Escaped HTML in Your Message

You’re using &lt; and &gt; instead of actual < and > tags. Since you’ve set the content type to text/html, the mail client will render HTML directly—escaping these characters will make your email show raw code instead of formatted content.

Fix your message section to use regular HTML tags:

$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane !</h1>';
$message .= '<table>';
// Add your table rows and content here...
$message .= '</table></body></html>';

4. Check the mail() Function’s Return Value

Always verify if the server accepted your send request—this tells you if the issue is in your code or the server’s configuration. Add this to your code:

$mailSent = mail($to, $subject, $message, $headers);
if ($mailSent) {
    echo "Email sent successfully!";
} else {
    echo "Failed to send email. Check server logs for details.";
}

5. Server-Side Configuration Checks

If the code fixes don’t work, the problem might be with your server’s mail setup:

  • Ensure your server has a mail transfer agent (MTA) installed (like Sendmail, Postfix, or Exim). Without this, the mail() function can’t send anything.
  • Check your server’s mail logs (e.g., /var/log/mail.log on Linux) for errors—logs will tell you if emails are being rejected, flagged as spam, or failing to connect to the recipient’s server.
  • If using Gmail as the sender: Native mail() often fails with Gmail’s strict security rules. Use a library like PHPMailer to send via Gmail’s SMTP server (you’ll need an App Password if 2FA is enabled on your Gmail account).

Corrected Full Code Example

$to = "xyz@gmail.com";
$subject = "Order Confirmation - Your Order with Mysite.com has been successfully placed!";
$from = "abc@gmail.com";

// Properly formatted headers
$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "Reply-To: $from\r\n";

// Clean HTML message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane !</h1>';
$message .= '<table>';
// Insert your table content here...
$message .= '</table></body></html>';

// Send and verify result
if (mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully!";
} else {
    echo "Failed to send email. Check server mail logs for specific errors.";
}

内容的提问来源于stack exchange,提问作者shoeb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:32:25