配置PHP中的mail方法发送邮件

来源:互联网 发布:周杰伦红模仿知乎 编辑:程序博客网 时间:2024/06/05 19:39

我的环境:XAMPP v3.2.1

准备工作

1.检查有没有sendmail程序,在xampp安装目录下的sendmail文件夹下。没有则下载sendmail.zip(http://glob.com.au/sendmail/),然后解压并放在在xampp目录下。

2.去mail.google.com注册一个google的邮箱,比如账号:user@gmail.com,密码: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_username=user@gail.com

auth_password=passwd

测试代码:

[php] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. $now = date("Y-m-d h:i:s");  
  3. $headers = 'From: name<sender@qq.com>';  
  4. $body = "hi, this is a test mail.\nMy email: sender@qq.com";  
  5. $subject = "test mail";  
  6. $to = "recver@qq.com";  
  7. if (mail($to$subject$body$headers))  
  8. {  
  9. echo 'success!';  
  10. }   
  11. else   
  12. {  
  13. echo 'fail';  
  14. }  
  15. ?>  
以上代码执行后recver@qq.com 会收到发过来的邮件,但是邮件头中的sender@qq.com会被替换成user@gmail.com。

0 0
原创粉丝点击