【问题标题】:PHP form not sending an email.Get thank you page but no emailPHP 表单未发送电子邮件。获取感谢页面但没有电子邮件
【发布时间】:2015-12-10 22:20:23
【问题描述】:

我的网站上有一个带有 php 脚本的 HTML Web 表单。点击提交时,会加载感谢页面,但提交的详细信息不会发送给收件人。

网站托管在 1and1

http://www.evolutionroofingltd.co.uk/contact.html

谁能看出我做错了什么?

contact.php

<?php
/* Set e-mail recipient */
$myemail  = "gregtwardochleb@hotmail.co.uk";

/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$email    = check_input($_POST['email']);
$phone  = check_input($_POST['phone'], "provide phone number");
$comments = check_input($_POST['comments'], "Write your comments");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

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

Comments:
$comments

End of message
";

/* Send the message using mail() function */
mail($myemail, $phone, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();

/* Functions we used */
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();
}
?>

【问题讨论】:

  • 这里最大的谜团是mail 函数的来源。
  • @joepd PHP 有一个内置的 mail() 函数

标签: php html webforms


【解决方案1】:

我很好奇在主题行中使用$phone - 但除非其中有有趣的角色,否则我认为这不是问题。

我建议您在脚本顶部打开错误报告(即:error_reporting( E_ALL ) 等),然后将 if 条件附加到邮件功能以查看您得到的结果,即:

$res=mail( $myemail, $phone, $message );
if( !$res ) show_error( print_r( error_get_last (),1 ) );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    相关资源
    最近更新 更多