Home > php教程 > php手册 > body text

XAMPP下配置SMTP mail服务,使PHP的mail() 函数可以发送邮件

WBOY
Release: 2016-06-13 10:50:08
Original
1766 people have browsed it

PHP的mail() 邮件函数很简单,但也导致了不能使用目前流行的 带验证功能的 SMTP 服务器(gmail, 163, 126等)
现在通过配置 XAMPP 提供的 sendmail 来使得 PHP 的 mail() 函数可以正常发送邮件,下面以:smtp.126.com 为例:
1.  找到 xampp/php/php.ini 文件,找到 [mail function] 语句块,修改如下:
1 [mail function]
2 SMTP = smtp.126.com
3 smtp_port = 25
4 sendmail_from = xxx@126.com
5 sendmail_path = "\"你的xampp安装目录\xampp\sendmail\sendmail.exe\" -t"
2. 找到 xampp/sendmail/sendmail.ini 文件,修改如下:
1 [sendmail]
2 smtp_server = localhost
3 smtp_port = 25
4 default_domain = 126.com
5 auth_username = 你的邮箱@126.com
6 auth_password = 你的密码

8 force_sender = xxx@126.com
3. 配置 SSL 服务(可选)
因为gmail, 163, 126 等需要使用SSL来连接SMTP邮件服务器,而xampp里的sendmail程序不支持ssl连接。
如果你使用的是其它邮箱,且不需要SSL来连接SMTP,那把smtp.126.com改成对应的SMTP服务器地址就好了。
我们可以下载安装一个SSL代理软件,我们这里使用http://www.stunnel.org/
安装成功后,打开stunnel里面的stunnel.conf文件,找到下面的代码,修改如下:
这里我们增加了一个 [126-smtp] 节点:
1 ;[gmail-smtp]
2 ;client = yes
3 ;accept = 127.0.0.1:25
4 ;connect = smtp.gmail.com:465

6 [126-smtp]
7 client = yes
8 accept = 127.0.0.1:25
9 connect = smtp.126.com:465
4. 测试你的 PHP mail() 函数吧,呵呵!
view source
print?  www.2cto.com
01  02     $from_name = 'xxx';
03     $from_email = 'xxx@126.com';
04     $headers = 'From: $from_name ';
05     $body = 'This is a test mail';
06     $subject = 'Test email from php mail()';
07     $to = 'xxx@xxx.com';
08     if (mail($to, $subject, $body, $headers)) {
09         echo "success!";
10     } else {
11         echo "fail…";
12     }
13 ?>
5. 你已经成功了!
 

 作者:json

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!