使用WAMP发送电子邮件

14
我使用最新版本的WAMP,当我尝试发送电子邮件时出现以下错误:
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() in C:\wamp\www\main\createaccount.php on line 8

发送失败...

该消息:

$to = "xxx@hotmail.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
 } else {
  echo("<p>Message delivery failed...</p>");
 }
你需要下载一个“邮件服务器”吗?
请帮忙。
6个回答

15
这对我有效,也适用于您:使用Fake Sendmail和网络主机邮件服务器(例如Godaddy、1and1等)。
1)下载sendmail zip并将其提取到C:\ Wamp \ bin \ sendmail(本示例仅供参考)。
2)编辑C:\ wamp \ bin \ sendmail \ sendmail.ini,并根据您的邮件服务器要求进行以下设置(我的设置如下):
smtp_server=mail.yourdomain.com
smtp_port=26
smtp_ssl=none
;default_domain=yourdomain.com
auth_username=smtpuser@yourdomain.com
auth_password=smtppassword
;pop3_server=
;pop3_username=
;pop3_password=
;force_sender=
;force_recipient=

3.) 在您的php.ini文件中设置sendmail.exe的路径。

[mail function]
; For Win32 only.
SMTP =

; For Win32 only.
sendmail_from =

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = "C:\wamp\bin\sendmail\sendmail.exe -t"

4.) 重新启动Wampserver。

你可能会在使用Gmail时获得成功,但是需要进行一些额外的调整才能使其正常工作。我更喜欢使用我上传代码的Web主机的邮件服务器。


正是我所需要的,以将我的开发环境与实际的 GoDaddy 账户链接起来。非常感谢! - Valentin Flachsel
这个有效!谢谢。这个挽救了我。突然间Wamp64服务器在开发本地主机上停止工作,这个挽救了我。 - GunWanderer
谢谢,对于我的情况,我还必须在php.ini中添加SMTP和smtp_port。使用Apache 2.4.9(Win32)PHP 7.2.3。 - toesslab

1

你的机器上没有运行SMTP服务器,但是你不必担心。只需将SMTP设置为一个开放的SMTP服务器,例如:

ini_set('SMTP', 'smtp.yourisp.com');

查看您的ISP主页或http://www.e-eeasy.com/SMTPServerList.aspx获取SMTP服务器列表。

如果您有桌面邮件程序,可以使用与发件人相同的地址。


谢谢,我尝试了很多服务器,但是我收到相同的错误消息,可能是我运行的其他程序使用了端口25 ;/ - Jorm
你确定在 mail 函数之前放置了 ini_set 并尝试过你自己 ISP 的 SMTP 服务器了吗? - Tatu Ulmanen

0

这里有另一种解决方案 - 使用SMTP本地主机发送邮件的WAMP


请记住,每次更改php.ini后,

您必须重新启动wamp(! ! !)

p.s.在php.ini中,我已经使用了:

SMTP = localhost 
smtp_port = 25 
sendmail_from = your_user@gmail.com

如果您无法编辑php.ini文件,请尝试在您的php脚本中插入这些行。

ini_set("SMTP", "localhost");
ini_set("smtp_port", "25");
ini_set("sendmail_from", "your_user@gmail.com");

0

1
至少 Gmail 要求 SSL 保护连接,因此使用普通的 mail() 不是一个选项。 - Tatu Ulmanen
@Tatu Ulmanen:谢谢 :) 我记得雅虎好像没有。 - Tony The Lion

0
你确定这些服务器上安装了邮件程序吗?如果没有,那就是你的问题。例如,XAMPP带有一个名为Mercury的邮件程序,你必须在通过服务器发送邮件之前启动它。

0

按照本文所述,如果您有Gmail帐户,它将起作用。 或者至少任何您知道服务器、端口和这些内容的电子邮件帐户。

从本地主机发送电子邮件


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接