将电子邮件脚本重定向到网页

时间:2013-07-12 13:33:27

标签: php email send

我目前有以下的电子邮件/数据库脚本工作正常,但重定向似乎不起作用。而不是重定向到它只是显示空白的PHP页面的URL。我该如何解决这个问题?

<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("register", $con);
$sql="INSERT INTO register_interest (Name, Email, Message, Website)

VALUES ('$_POST[Name]', '$_POST[Email]', '$_POST[Message]', '$_POST[Website]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
 mysql_close($con);
$to = "email";
$subject = "Interest";
$email = $_POST['Email'] ;
$message = $_POST['Message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;

if ($sent) {
  header("Location: http://www.url.co.uk"); 
  exit();
} else {
  print "We encountered an error sending your email"; 
}
?>

3 个答案:

答案 0 :(得分:0)

试试这段代码:

if(!$sent){
    print "We encountered an error sending your email";
    exit;
}

header("Location: http://www.url.co.uk");

答案 1 :(得分:0)

<?php
$con = mysqli_connect("localhost","user","pass","register");
if (mysqli_connect_errno())
{
    die('Could not connect: ' . mysqli_connect_error());
}
$name = mysqli_real_escape_string($con, $_POST['Name']);
$email = mysqli_real_escape_string($con, $_POST['Email']);
$message = mysqli_real_escape_string($con, $_POST['Message']);
$website = mysqli_real_escape_string($con, $_POST['Website']);

$sql = "INSERT INTO register_interest (Name, Email, Message, Website)
VALUES ('$name', '$email', '$message', '$website')";

if (!mysqli_query($con, $sql))
{
    die('Error: ' . mysqli_error($con));
}
mysqli_close($con);

$to = "email@address.com";
$subject = "Interest";
$email = $_POST['Email'] ;
$message = $_POST['Message'] ;
$headers = "From: $email\n";
$sent = mail($to, $subject, $message, $headers) ;

if ($sent) {
  header("Location: http://www.url.co.uk"); 
  exit();
} else {
  print "We encountered an error sending your email"; 
}
?>

答案 2 :(得分:0)

检查在打开PHP标记之前是否有空行),然后header指令将不起作用。