在Windows下利用php自带的mail函数发邮件

这几天看《Head First PHP & MySQL》,里面有发邮件的例子是用系统自带的mail函数发送的,自己照书上写的试了一直不成功,后来终于在网上找到解决方案,现在总结下。

我的开发环境是WAMP Server 2.4

关于php mail函数介绍 请看这里

Windows下用mail函数需要下载一个sendmail 点此下载

  • 解压到C:wampsendmail
  • 配置sendmail.ini(我这里以126邮箱为例)
    • smtp_server=smtp.126.com
    • smtp_port=587
    • smtp_ssl=ssl
    • error_logfile=error.log
    • debug_logfile=debug.log
    • auth_username=你的用户名
    • auth_password=你的密码
    • force_sender=你的邮箱

     配置error.log和debug.log可以在sendmail目录下的error.log和debug.log查看相应的发送错误和情况。

  • 配置php.ini(文件位置类似于:C:wampinapacheApache2.4.4in),找到[mail function]
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    ;SMTP = 
    ; http://php.net/smtp-port
    ;smtp_port = 25
    
    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = you@domain.com
    
    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    sendmail_path = "C:wampsendmailsendmail.exe -t -i"

    上面的主要就是最后一句 sendmail_path = "C:wampsendmailsendmail.exe -t -i"

  • 在Apache模块上要允许 “ssl_module”(网上找的好多文章都没有这一步,我自己试的时候一直因为这个不成功)

    

我写了这么一个测试页面

<?php

mail('mib23@126.com', 'test', 'test');

?>

好了,这样应该已经能发送了。

原文地址:https://www.cnblogs.com/mib23/p/phpsendmail.html