无法使用表单发送电子邮件

时间:2014-02-10 08:46:10

标签: php html

我的表格有问题。它通过但我从未收到过电子邮件。我检查了垃圾邮件,但没有出现。有人能给我一些暗示吗?无法识别错误。

<div class="contact-form">
<form class="email" action="mailer.php" method="post">
<h3>Kontaktirajte nas!</h3>
<div>
<p>Ime:</p>
input type="text" name="name" />
<p>E-mail:</p>
<input type="text" name="email" />
<p>Naslov   :</p>
<input type="text" name="subject" />
<p>Poruka:</p>
<textarea name="message"></textarea></p>
<input class="send" type="submit" value="Send">
</form>
</div>
</div>

下面是php代码(mailer.php)。

$myemail = "example@gmail.com";

$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");


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

$message = "

Name: $name
E-mail: $email
Subject: $subject

Message:
$message

";

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


header('Location: thanks.html');
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>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

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

2 个答案:

答案 0 :(得分:0)

代码看起来很好,你可以在这里引用PHP邮件@ http://uk1.php.net/manual/en/function.mail.php

您需要检查代码中定义的FROM地址,或者在php.ini中设置(这可能会导致它被退回而不会点击您的电子邮件)

如果您通过PHP邮件发送,则不需要运行smtp服务器,但是值得在SMTP服务器上进行测试以确定这是否是您的问题。

尝试使用W3schools代码,看看是否会发送给您并让我们知道结果......

链接可在此处找到@ http://www.w3schools.com/php/php_mail.asp

答案 1 :(得分:0)

Sendin电子邮件可能是一件非常厚重的事情。

要检查的第一件事是您定义了有效的发件人地址。有些主持人只是在没有有效电子邮件的情况下拒绝发送电子邮件。

$additional_headers = "From: from@example.com\r\n";   
mail($myemail, $subject, $message, $additional_headers);

检查邮件错误日志(如果您访问它们),它可以让您更深入地了解问题所在。