我的环境:XAMPP v3.2.1
准备工作
1.检查有没有sendmail程序,在xampp安装目录下的sendmail文件夹下。没有则下载sendmail.zip(http://glob.com.au/sendmail/),然后解压并放在在xampp目录下。
2.去mail.google.com注册一个google的邮箱,比如账号:[email protected],密码:passwd
配置步骤:
1.php.ini中如下设置:
; SMTP = localhost;
smtp_port = 25;
sendmail_from = sendmail_path = "C:/xampp/sendmail/sendmail.exe -t"
也就是注释掉"SMTP"、"smtp_port"、"sendmail_from",设置sendmail_path变量的值为以上所示(根据自己电脑上sendmail.exe的目录自行设置)。
2.sendmail.ini中如下设置
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=TLS
error_logfile=error.log
debug_logfile=debug.log
auth_password=passwd
测试代码:
<?php
$now = date("Y-m-d h:i:s");
$headers = 'From: name<[email protected]>';
$body = "hi, this is a test mail.\nMy email: [email protected]";
$subject = "test mail";
$to = "[email protected]";
if (mail($to, $subject, $body, $headers))
{
echo 'success!';
}
else
{
echo 'fail';
}
?>
以上代码执行后[email protected] 会收到发过来的邮件,但是邮件头中的[email protected]会被替换成[email protected]。