【发布时间】:2017-03-01 08:21:31
【问题描述】:
我想在提交联系表单时使用 PHP 发送电子邮件..
这是我的 index.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple PHP Contact Form</title>
<meta name="HandheldFriendly" content="true">
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div class="wrapper">
<div id="contact_form">
<form name="form1" id="ff" method="post" action="insert.php">
<h1>Responsive HTML5 Contact Form Demo</h1>
<p>This is a working demo of HTML5 and PHP Responsive Contact Form. The form is so simple, that you can implement it in a few minutes.</p>
<label>
<span>Name*:</span>
<input type="text" placeholder="Please enter your name" name="name" id="name" required autofocus>
</label>
<label>
<span>City*:</span>
<input type="text" placeholder="Please enter your city" name="city" id="city" required>
</label>
<label>
<span>Phone:</span>
<input type="tel" placeholder="Please enter your phone" name="phone" id="phone">
</label>
<label>
<span>Email*:</span>
<input type="email" placeholder="youremail@gmail.com" name="email" id="email" required>
</label>
<input class="sendButton" type="submit" name="Submit" value="Send">
</form>
</div>
</div>
</body>
</html>
这是我的 insert.php:
<?php
// Get values from form
$name=$_POST['name'];
$city=$_POST['city'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$to = "mymail@gmail.com";
$subject = "Future Tutorials Contact Form Test";
$message = " Name: " . $name . "\r\n City: " . $city . "\r\n Phone: " . $phone . "\r\n Email: " . $email;
$from = "FutureTutorials";
$headers = "From:" . $from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
if(mail($to,$subject,$message,$headers))
{
print "Hello";
// Created by Future Tutorials
}else{
echo "Error! Please try again.";
}
?>
当我按下提交按钮时,它会显示以下错误消息
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()
。但是我在这里找不到错误的原因。
我在这里省略了原始电子邮件。 谢谢
【问题讨论】:
-
看看这个stack here。希望有帮助
-
@User57,开始删除@符号。我只是运行你的代码没有任何问题,所以我认为问题在于 sendmail 的配置,而不是你的脚本。检查此讨论是否可以帮助您stackoverflow.com/questions/786221/mail-returns-false
-
好的,现在我又遇到了一个错误,
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() -
stackoverflow.com/questions/19132171/… 在你的本地主机上试试这个