好多人在用php的mail()函数发送邮件时不成功,折腾半天也没找到什么问题。
我昨天也遇到这情况了,现在记录一下,以便以后再遇到这问题。
1:要用mail首先你要把sendMail扩展打开,我用的phpstudy,打开很简单,把勾打上就可以了。
2:然后打开php.ini然后搜索“[mail function]”,找到下面文本块,把我加红加粗的前面的;去掉,然后修改成你的邮件服务器,我用的163的,qq的比较麻烦,所以用的163。
sendmail_path="D:\phpStudy\tools\sendmail\sendmail.exe -t" //这个后面的地址就是你的sendmail.exe的地址,这样如果你配置php环境时没有安装直接下载sendmail安装一下就可以了。
; For Win32 only. ; http://php.net/smtp SMTP = smtp.163.com ; http://php.net/smtp-port smtp_port = 25 ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = intrwins@163.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path ; 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(), even in safe mode. ;mail.force_extra_parameters = ; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename mail.add_x_header = On
sendmail_path="D:\phpStudy\tools\sendmail\sendmail.exe -t"
3:配置sendmail.ini
然后找到你的sendmail文件夹,找到sendmail.ini文件,搜索“[sendmail]”,配置相关信息。
4:同样的设置一下,看红色加粗部分POP3_这三个是设置接取邮件的配置,需要的也可以设置下,我们现在设置的是发送的配置。
smtp_server=smtp.163.com ; smtp port (normally 25) smtp_port=25 ; 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=auto
auth_username=****** //你的用户名
auth_password=********** //你的邮件密码
; 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=intrwins@163.com
5:重启你的php环境,然后再试试应该就可以了。
<?php $to = "zzndc@qq.com"; $subject = "我就是在测试php的mail函数,请163别给我退回"; $message = "我就是在测试php的mail函数,请163别给我退回"; $from = "intrwins@163.com"; $headers = "From: $from"; if(mail($to,$subject,$message,$headers)){ echo "发送成功"; }else{ echo "发送失败"; } ?>