0

我有这个在网上找到的简单脚本,但它不起作用。我错过了什么?

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];


$email_from = $visitor_email;
$email_subject = "New Message";
$email_body = "You have received a new message from $name.\n".
    "\n\n $message".

$to = "myemail@domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');
} catch(Exception $e){
    //problem, redirect to sorry page
    header('Location: sorry.html');
}       
?> 

我总是被重定向到感谢页面,但我没有收到任何电子邮件。所有 HTML 内容都是正确的。

4

3 回答 3

1

假设您有一个类似于以下带有命名字段的表单:(输入字段必须命名)。

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<form method="post" action="handler.php">

Name: 
<input type="text" name="name" /> <br/>

Your Email: 
<input type="text" name="email" /> <br/>

Message: <br>
<textarea id="body" name="message" cols="100" rows="20"></textarea><br/>
<input type="submit" name="submit" value="Send Email" />

</body>
</html>

由于 . 末尾的点,这条线会给你带来问题$message

$email_body = "You have received a new message from $name.\n".
"\n\n $message".

点应该是分号;,例如:

$email_body = "You have received a new message from $name.\n".
"\n\n $message";

在测试中,我输入的消息直到更改为分号才通过。

该行echo "error; you need to submit the form!";应该是一个die()指令,以便停止执行。

如:die("Error. You need to submit the form.");or you can use exit;under yourecho还。

如:

echo "error; you need to submit the form!";
exit;

PHP (handler.php)

使用上面显示的表格进行测试和工作。

<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
// echo "Error. You need to submit the form.";
// exit;
    die("Error. You need to submit the form.");
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

$email_from = $visitor_email;
$email_subject = "New Message";
$email_body = "You have received a new message from $name.\n".
    "\n\n $message";

$to = "myemail@domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
// header('Location: thank-you.html');

echo "Success"; // My echo test

} catch(Exception $e){
    //problem, redirect to sorry page
// header('Location: sorry.html');

echo "Sorry"; // My echo test
}       
?>
于 2013-09-15T13:35:34.193 回答
0

如果您在本地主机上,则无法发送电子邮件。只有当您的代码在线时,才会发送信息。

您可以尝试一种更简单的方法,称为 PHPmailer。这是一个例子,

  require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {

echo "消息已发送!"; }

Php mailer 通过调用方法和属性使编码器变得更容易,而且将图像嵌入到邮件中也很容易。

不过,如果您在本地主机上,则无法发送电子邮件。

于 2013-09-15T09:08:56.693 回答
0

您是否检查过电子邮件中的垃圾邮件过滤器?

于 2013-09-15T09:09:27.653 回答