我想我已经试着在3天前解决了这个问题,但似乎找不到问题。
我使用XAMPP并使用以下代码:
<?php
$to = "carl.j.97@live.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "carl.j.97@live.com";
$headers = "From: $from";
$res= mail($to,$subject,$message,$headers);
echo " $res Mail Sent.";
?>
当我进入那个页面时,我得到一个错误,上面写着:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set(
我在examplep中的php.init文件如下:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smpt.gmail.com
; http://php.net/smtp-port
smtp_port = 25
这就是我的全部密码。
在生产服务器中运行此脚本。除非在本地安装SMTP服务器,否则无法从localhost发送电子邮件。
-
你确定你编辑了正确的php.ini文件吗?
-
编辑php.ini后,您是否重新启动了Apache服务器?
如果您在XAMPP或其他在localhost上运行的程序上运行此程序,请在谷歌上搜索使电子邮件服务正常工作所需的设置。您需要编辑一些文件并更改一些端口/客户端名称。
编辑:您只需要修改两个ini文件:php.ini和sendmail.ini
1) 在php.ini(c:''examplep/php/php.ini)中查找邮件函数>>[mail function]
更改以下内容::
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = from@gmail.com
sendmail_path = "'"C:'xampp'sendmail'sendmail.exe'" -t"
注意:请确保为sendmail_oath指定的路径有效。就我而言,它在C。保存您的更改。
2) 接下来修改sendmail.ini(c:''examplep/sendmail/sendmail.ini)如所示,对汞进行注释
# Mercury
#account Mercury
#host localhost
#from postmaster@localhost
#auth off
# A freemail service example
#account Hotmail
#tls on
#tls_certcheck off
#host smtp.live.com
#from [exampleuser]@hotmail.com
#auth on
#user [exampleuser]@hotmail.com
#password [examplepassword]
然后粘贴以下行:
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from x@gmail.com
auth on
user x@gmail.com
password x
port 587
# Set a default account
account default : Gmail
再次保存更改。
使用下面的代码来测试它是否工作!
<?php
$subject="Test mail";
$to="someone@whatever.com";
$body="This is a test mail";
if (mail($to,$subject,$body))
echo "Mail sent successfully!";
else
echo"Mail not sent!";
?>
注意:在上面的配置中,发件人应该使用gmail电子邮件服务,对于收件人,任何电子邮件服务都可以。