提交时,SMTP联系表单空白屏幕

时间:2014-01-06 13:36:56

标签: php html forms smtp

我在联系表单上遇到了很多问题,有没有人知道为什么我的代码只是在我的表单上按“提交”时用“(URL)/mail.php”显示空白屏幕。

这是我的代码

 <h2 class="formhead">Contact Form</h2>
<br>
  <form class="form" action="mail.php" method="post">

    <p class="name">
        <input type="text" name="name" id="name" placeholder="John Doe" />
        <label for="name">Name</label>
    </p>
<br>
    <p class="email">
        <input type="text" name="email" id="email" placeholder="mail@example.com" />
        <label for="email">Email</label>
    </p>
<br>
    <p class="number">
        <input type="text" name="number" id="number" placeholder="0774XXXXXXX" />
        <label for="name">Contact Number</label>
    </p>
<br>
    <p class="web">
        <input type="text" name="web" id="web" placeholder="www.example.co.uk" />
        <label for="name">Website</label>
    </p>
<br>
    <p class="query">
        <textarea name="query" id="query" placeholder="Write something to us" /> </textarea>
    </p>
<br>
    <p class="submit">
        <input name="btnSubmit" type="submit" onclick="javascript: return validate();"  value="Send" />
    </p>
  </form>

PHP

<?
ob_start();
if(isset($_POST['btnSubmit']))
{
require("class.phpmailer.php");

$mail = new PHPMailer();

//Your SMTP servers details

$mail->IsSMTP();               // set mailer to use SMTP
$mail->Host = "localhost";  // specify main and backup server or localhost
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "supp0rt@c(hidden)y.co.uk";  // SMTP username
$mail->Password = "DQ(hidden)"; // SMTP password
//It should be same as that of the SMTP user

$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form

$mail->From = $mail->Username;  //Default From email same as smtp user
$mail->FromName = "Display Name";

$mail->AddAddress("enquiries@c(hidden)y.co.uk", "chapnolo"); //Email address where you wish to receive/collect those emails.

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML


$mail->Subject = $_POST['email'];
$message = "Name of the requestor :".$_POST['name']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Phone number :".$_POST['number']."\r\n <br> Message: ".$_POST['query']."\r\n <br> Website: ".$_POST['web'];
$mail->Body    = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
header("Location: $redirect_url");
}
?>

非常感谢任何见解,我是PHP的新手。

2 个答案:

答案 0 :(得分:0)

无法找到php邮件程序类class.phpmailer.php。它与其他文件位于同一目录中吗??

答案 1 :(得分:0)

我会将header()语句移到if()语句之外,以确保一个人始终被重定向:想想如果一个人在不使用你的情况下手动访问yoursite.com/mail.php会发生什么情况表单到POST数据:

<?PHP
ob_start();
if(isset($_POST['btnSubmit']))
{
    // Mail stuff
}
header("Location: $redirect_url");
?>