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

PHP表单发送邮件失败求助:XAMPP环境下报错

Troubleshooting PHP Mail() Failure on XAMPP/Windows 10

Hey there! Let's break down why your email isn't sending and fix this step by step. I've noticed a mix of code issues and environment-specific config problems that are likely causing the $sent = false result.

1. Fix Critical Code Issues First

Your code has a few syntax and formatting mistakes that are probably breaking the mail function before it even tries to send:

  • Reserved variable name: $for is a reserved keyword in PHP. Swap it for $to to avoid unexpected behavior.
  • Invalid concatenation: You have a space in $headers. = — it should be $headers .= (no space between . and =). This is a syntax error that will throw a fatal error if you enable error reporting.
  • Incorrect line breaks: Email headers require CRLF (\r\n) line breaks, not just a newline. Using loose line breaks won't work consistently across systems.
  • Malformed MIME header: Spaces in text / html; charset = utf-8 will mess up the content-type parsing. Remove those spaces to make it text/html; charset=utf-8.
  • Quoted sender name: Wrap the sender's name in quotes to handle spaces properly.

Here's the corrected version of your code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$to = 'email@example.com';
$title = 'Sending email from PHP';
$message = '<html>
<head>
    <title> Email with HTML </title>
</head>
<body>
    <h1> Email with HTML </h1>
    This is an email that is sent in HTML format
    <hr>
    Sent by my program in PHP
</body>
</html>';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: "Name1 Name2" <info@example.com>';

$sent = mail($to, $title, $message, $headers);

if ($sent) {
    echo 'Email sent correctly';
} else {
    echo 'Error in sending the email';
}
?>

2. Configure XAMPP for Email Sending (Windows-Specific)

Windows doesn't include a built-in mail transfer agent (MTA), so PHP's mail() function can't send emails out of the box. You need to set up XAMPP to use an external SMTP server (like your personal email provider's SMTP):

Step 1: Edit php.ini

  1. Open C:\xampp\php\php.ini in a text editor.
  2. Find the [mail function] section and update these settings:
    SMTP=smtp.example.com ; Replace with your email provider's SMTP server (e.g., smtp.gmail.com for Gmail)
    smtp_port=587 ; Use 587 for TLS, 465 for SSL
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
    

Step 2: Edit sendmail.ini

  1. Open C:\xampp\sendmail\sendmail.ini.
  2. Update these core settings:
    smtp_server=smtp.example.com ; Same as above
    smtp_port=587
    auth_username=your-actual-email@example.com ; Your full email address
    auth_password=your-app-specific-password ; Use an app password if using Gmail/Outlook (not your regular password)
    force_sender=your-actual-email@example.com
    

Note for Gmail users: You'll need to enable 2-step verification on your Google account, then create an app password to use here (regular passwords won't work with third-party SMTP clients anymore).

3. Debug Further If Issues Persist

  • Enable error reporting: The error_reporting(E_ALL); line in the corrected code will show you specific PHP errors that might be hidden otherwise.
  • Check logs: Look at XAMPP's Apache error log (C:\xampp\apache\logs\error.log) and PHP error log (C:\xampp\php\logs\php_error.log) for clues about why the mail is failing.
  • Test with plain text: Try sending a simple plain-text email first to rule out HTML formatting issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:18:15