WAMP环境下PHP mail()函数无法发送邮件的问题求助
PHP mail()函数无法正常工作的排查与解决
问题概述
花费1天多时间配置php.ini和sendmail.ini后,PHP的mail()函数仍无法正常运行,报错信息如下:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp64\www\test-mail.php
以下是相关代码及配置文件:
test.php代码
<?php $to = "mymail@gmail.com"; $subject = "Utilisation de mail()"; $message = "Salut, comment ça va"; $headers = 'From: mymail@gmail.com'. "\r\n" . 'MIME-Version: 1.0' . "\r\n"; 'Content-type: text/html; charset=utf-8'; if(mail($to,$subject,$message,$headers)){ echo 'Envoye !'; }else{ echo 'Erreur envoi';}
php.ini (v8.2) 邮件配置段
[mail function] ; For Win32 only. ; https://php.net/smtp SMTP = ; https://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; https://php.net/sendmail-from ;sendmail_from ="admin@wampserver.invalid" ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; https://php.net/sendmail-path sendmail_path = "C:/wamp64/bin/sendmail/sendmail.exe -t" ; Force the addition of the specified parameters to be passed as extra parameters ; to the sendmail binary. These parameters will always replace the value of ; the 5th parameter to mail(). ;mail.force_extra_parameters = ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename mail.add_x_header = Off ; Use mixed LF and CRLF line separators to keep compatibility with some ; RFC 2822 non conformant MTA. mail.mixed_lf_and_crlf = Off ; The path to a log file that will log all mail() calls. Log entries include ; the full path of the script, line number, To address and headers. ;mail.log = ; Log mail to syslog (Event Log on Windows). ;mail.log = syslog
sendmail.ini配置
[sendmail] ; you must change mail.mydomain.com to your smtp server, ; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup) ; emails delivered via IIS's pickup directory cause sendmail to ; run quicker, but you won't get error messages back to the calling ; application. smtp_server=smtp.gmail.com ; smtp port (normally 25) smtp_port=465 ; SMTPS (SSL) support ; auto = use SSL for port 465, otherwise try to use TLS ; ssl = alway use SSL ; tls = always use TLS ; none = never try to use SSL smtp_ssl=ssl ; the default domain for this server will be read from the registry ; this will be appended to email addresses when one isn't provided ; if you want to override the value in the registry, uncomment and modify default_domain=localhost ; log smtp errors to error.log (defaults to same directory as sendmail.exe) ; uncomment to enable logging error_logfile=error.log ; create debug log as debug.log (defaults to same directory as sendmail.exe) ; uncomment to enable debugging debug_logfile=debug.log ; if your smtp server requires authentication, modify the following two lines auth_username=mymail@gmail.com auth_password=(Google PIN) ; if your smtp server uses pop3 before smtp authentication, modify the ; following three lines. do not enable unless it is required. pop3_server= pop3_username= pop3_password= ; force the sender to always be the following email address ; this will only affect the "MAIL FROM" command, it won't modify ; the "From: " header of the message content force_sender=mymail@gmail.com
解决方案
1. 修正php.ini的Windows SMTP配置
错误提示显示PHP尝试连接localhost:25,是因为Windows下PHP默认优先使用内置SMTP客户端,而非sendmail.exe。修改php.ini的Win32相关配置:
[mail function] ; For Win32 only. SMTP = smtp.gmail.com smtp_port = 465 sendmail_from = "mymail@gmail.com" ; 保留sendmail_path,若要强制使用sendmail.exe,需确保路径正确(用正斜杠或双反斜杠) sendmail_path = "C:/wamp64/bin/sendmail/sendmail.exe -t -i"
2. 修复PHP代码的headers语法错误
你的$headers赋值存在语法问题,第三行的Content-type未与前面内容用.连接,导致该header未被正确添加:
$headers = 'From: mymail@gmail.com'. "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=utf-8';
3. 验证Gmail SMTP权限
使用Gmail SMTP需确认:
- 应用专用密码(Google PIN)有效,且对应你的邮箱账号
- 账号开启两步验证后生成的专用密码,而非普通登录密码
- 网络可访问smtp.gmail.com:465,可通过命令提示符执行
telnet smtp.gmail.com 465测试连接
4. 查看sendmail日志定位细节
sendmail.ini已开启日志,前往C:\wamp64\bin\sendmail\查看error.log和debug.log,日志会记录具体失败原因(如认证错误、SSL问题等),便于精准排查。
5. 替代方案:使用PHPMailer
若原生配置仍无法解决,推荐使用PHPMailer替代mail()函数,稳定性更强且配置直观:
<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; 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 { // 服务器配置 $mail->SMTPDebug = SMTP::DEBUG_OFF; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'mymail@gmail.com'; $mail->Password = '你的Google应用专用密码'; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = 465; // 收件人 $mail->setFrom('mymail@gmail.com', '发送者名称'); $mail->addAddress('mymail@gmail.com'); // 邮件内容 $mail->isHTML(true); $mail->Subject = 'Utilisation de mail()'; $mail->Body = 'Salut, comment ça va'; $mail->send(); echo 'Envoye !'; } catch (Exception $e) { echo "Erreur envoi: {$mail->ErrorInfo}"; }
内容的提问来源于stack exchange,提问作者JoKa
相关产品推荐
相关产品推荐

