表格发送电子邮件PHP脚本

时间:2013-09-15 09:02:28

标签: php forms server-side

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

<?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内容都是正确的。

3 个答案:

答案 0 :(得分: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.");或您也可以在exit;下使用echo

如:

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
}       
?>

答案 1 :(得分: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邮件程序通过调用方法和属性使编码器更容易,并且将图像嵌入邮件也很容易。

如果您在本地主持人身上,则无法发送电子邮件。

答案 2 :(得分:0)

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