与本地WAMP服务器测试联系表单


Testing Contact Form with local WAMP server

我正在制作一个联系人表单,以便某人可以向指定的电子邮件发送消息。但是,我得到一个非常持久的错误消息,并且不会消失:

Warning: 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'mail.php on line 10

我试着在第10行按指定的ini_set(),但是它没有改变任何东西。我试着研究可能是什么情况,但到目前为止还没有发现任何东西。我在想也许WAMP不支持邮件。


HTML代码

<form action = "mail.php" method=  "POST">
    <p>Name</p> <input name = "name" type = "text">
    <p>Email</p> <input name = "email" type = "text">
    <p>Message</p><textarea name = "message" rows = "6" cols = "25"></textarea><br />
    <input value = "Send" type = "submit" >
    <input value = "Reset Form" type = "reset">
</form>

成功创建表单,并通过提交按钮向mail.php发送数据。


PHP代码

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "To: myawesome.email@gmail.com";
$mailheader = "From: $name 'r'n";
$formcontent= "From: $email 'r'n Message: $message";
mail($recipient, $mailheader, $formcontent) or die("Error!");
echo "Your message has been delivered." . " -" . "<a href='form.html' style='text-decoration: none; color: #ff0099;'> Return Home </a>";
?>

其中mail.php应该接收$ receiver, $mailheader和$formcontent,并将它们发送到指定的地址。

您可能没有在端口25上运行邮件服务器(很可能是这种情况),或者您的软件防火墙阻塞了该端口。


请注意,即使使用本地运行的邮件服务器,您的ISP也很可能会阻止端口25 -这是防止垃圾邮件的正常做法。

要从本地服务器进行测试,并实际发送电子邮件,您很可能需要使用外部电子邮件提供商-并在端口上连接到它而不是25。Gmail的SMTP服务器(TLS,端口465)将在这里发挥奇迹,但您需要通过Gmail帐户进行身份验证并发送。

您是否尝试检查您的邮件服务器是否正确配置或像以下示例一样配置

// Please specify your Mail Server - Example: mail.example.com.
ini_set("SMTP","mail.example.com");
// Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.
ini_set("smtp_port","25");
// Please specify the return address to use
ini_set('sendmail_from', 'example@YourDomain.com')
;

或检查php.ini文件。下面是一个配置示例:

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = me@example.com
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").