【问题标题】:Html PHP web form not sending email?Html PHP Web表单不发送电子邮件?
【发布时间】:2016-03-11 23:38:45
【问题描述】:

我创建了一个带有 php 脚本的 html 表单,以便在用户点击“发送”时 ping 一封电子邮件。但是,当点击“发送”时,我收到一个错误“请更正以下错误:”谁能告诉我我是否正确连接表单或我的 php 脚本是否错误。谢谢!

contact.htm

<html>
<body>

<p>Required fields are <b>bold</b></p>

<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>

<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>

<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>

<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>

<p><input type="submit" value="Send it!"></p>

<p> </p>

</form>

</body>
</html>

谢谢.htm

<html>
<body>

<p><b>Your message was sent</b></p>

<p>Your message was successfully sent!
Thank you for contacting us, we will reply
to your inquiry as soon as possible!</p>

</body>
</html>

contact.php

<?php

$myemail  = "gregtwardochleb@hotmail.co.uk";

$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}

if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}

$message = "Hello!

Your contact form has been submitted by:

Name: $yourname
E-mail: $email
URL: $website

Like the website? $likeit
How did he/she find it? $how_find

Comments:
$comments

End of message
";

mail($myemail, $subject, $message);

header('Location: thanks.htm');
exit();

function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>
<?php
exit();
}
?>

【问题讨论】:

    标签: php html forms webforms


    【解决方案1】:

    您的 HTML 页面被重定向到联系页面。并且在联系页面中,你已经写了代码表示

    <b>Please correct the following error:</b><br />
    

    您的代码中有以下项目吗?

    $website  = check_input($_POST['website']);
    $likeit   = check_input($_POST['likeit']);
    $how_find = check_input($_POST['how']);
    $comments = check_input($_POST['comments'], "Write your comments");
    

    请告诉我你到底想做什么。

    【讨论】:

    • 嗨,首先我想在用户点击“发送”确认电子邮件已发送时出现一个感谢页面。其次,我希望提交的用户详细信息能够到达他们的目的地,即公司所有者的电子邮件地址。
    【解决方案2】:

    您缺少一些输入,因为您已包含在 contact.php 中,例如网站、如何、喜欢您未从表单输入中发布的内容

    <html>
    <body>
     <form action="contact.php" method="post">
      <p><b>Your Name:</b> <input type="text" name="yourname" /><br />
      <b>Subject:</b> <input type="text" name="subject" /><br />
      <b>E-mail:</b> <input type="text" name="email" /><br />
      <b>Website:</b> <input type="text" name="website" /><br />
      <b>Like the website:</b> <input type="text" name="likeit" /><br />
      <b>How did he/she find it:</b> <input type="text" name="how" /><br />
      <p><b>Your comments:</b><br />
      <textarea name="comments" rows="10" cols="40"></textarea></p>
      <p><input type="submit" value="Send it!"></p>
     </form>
    </body>
    </html>
    

    php 邮件功能在本地主机上不起作用,因为它需要域信息来发送邮件。所以你必须先把它放在服务器上。希望对你有用

    【讨论】:

    • 我添加了缺少的输入字段,但结果仍然相同
    • 你是在本地主机还是服务器上使用它?如果您在服务器上使用它,那么您能提供此代码的实时链接吗?
    • 目前是本地的。我想在它上线之前让它工作。这 3 个文件直接位于 web-forms 文件夹中。
    • php 邮件功能在本地主机上不起作用,因为它需要域信息来发送邮件。所以你必须先把它放在服务器上。希望它对你有用。
    • 哦,好吧,这一切都解释了。今晚我将在服务器上尝试它,希望它会发送一封电子邮件。我会与更新联系。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多