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

我的环境: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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值