我用的是win10 和 WampServer
php mail()函数在windows中是不能直接使用的,需要安装sendmail,假如是用的XAMPP,则已经下载好,不需要重新下载
1.下载sendmail.zip 地址:http://glob.com.au/sendmail/
2.下载成功后解压,并把下载好的文件剪切至wamp目录(其实存到哪里都一样这里为了方便存就放在wamp下好了,和www在同一目录)
3.配置php.ini 和刚刚下载的 sendmail/sendmail.ini
①php.ini
搜索[mail function]
关键字找到如下代码
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.qq.com
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from =798932948@qq.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path ="E:\wamp\wamp64\sendmail\sendmail.exe -t"
SMTP = 邮箱smtp地址 (例:QQ邮箱是smtp.qq.com l63邮箱是smtp.163.com )
sendmail_from = 你对应邮箱的邮件地址(例:QQ邮箱)
sendmail_path = 下载的sendmail中sendmail.exe的地址(例:E:\wamp\wamp64\sendmail\sendmail.exe -t)
注意:去掉sendmail_path 前的分号,这里分号表示注释的意思
改完之后重启服务器配置才会生效
②sendmail.ini
因为我是用QQ邮箱,QQ邮箱不能直接使用密码,要使用QQ授权码
(什么是QQ邮箱授权码?)
[sendmail]
; you must change mail.mydomain.com to your smtp server
smtp_server=smtp.qq.com
smtp_port=25
auth_username=798932948@qq.com
auth_password=QQ邮箱授权码
force_sender=798932948@qq.com
4.然后就可以用php中的mail()发送邮件了
例子localhost/mail.php
<?php
$to = "收件人邮箱账号";
$subject = "THEME";//主题
$message = "Hello Email";//邮件内容
$from = "798932948@qq.com";
$headers = "From: $from";
$result = mail($to,$subject,$message,$headers);
var_dump($result);
?>