SMTP Contact Form blank screen on Submit

2019-08-12 01:01发布

问题:

I've been having alot of issues with contact form, does anyone know why my code is just bringing up a blank screen with "(URL)/mail.php when i press Submit on my form.

Here is my code

 <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");
}
?>

Any insights would be greatly appreciated, I'm new to PHP.

回答1:

It's not able to find the php mailer class class.phpmailer.php. Is it in the same directory as as the rest of the files..?



回答2:

I would move the header() call outside of the if() statement to make sure a person is ALWAYS redirected: think about what would happen if a person that manually visits yoursite.com/mail.php without using your form to POST data:

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