Declan Watt Declan Watt - 1 year ago 71
PHP Question

Contact PHP form - not receiving mail (GMAIL)

Ive been trying to get my contact form working, but i'm not receiving any emails in my gmail folders (including spam and trash. and also can't track anything through my host).

I've been following this tutorial:

Need some serious help as i've tried everything!


$from = $_POST['email'];
$sendTo = '';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'email' => 'Email', 'phone' => 'Phone', 'message' => 'Message');

// array variable name => Text to appear in email

$okMessage = 'Contact form succesfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

// let's do the sending

$emailText = "You have new message from contact form\n=============================\n";

foreach ($_POST as $key => $value) {

if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";

mail($sendTo, $subject, $emailText, "From: " . $from);

$responseArray = array('type' => 'success', 'message' => $okMessage);
catch (\Exception $e)
$responseArray = array('type' => 'danger', 'message' => $errorMessage);

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
else {
echo $responseArray['message'];


<form id="contact-form" method="post" action="contact.php" role="form">

<div class="messages"></div>

<div class="form-group">
<div class="row">
<div class="col-md-6 col-sm-12">
<label>First name*</label>
<input type="text" id="form-name" name="name" class="form- control" placeholder="Please enter your firstname *" required="required">
<div class="col-md-6 col-sm-12">
<label>Last name*</label>
<input type="text" name="surname" id="form-surname" class="form-control" placeholder="Please enter your firstname *" required="required">
<div class="form-group">
<div class="row">
<div class="col-md-6 col-sm-12">
<input type="email" name="email" id="form-email" class="form-control" placeholder="Please enter your firstname *" required="required">
<div class="col-md-6 col-sm-12">
<input type="tel" name="phone" id="form-phone" class="form- control" placeholder="Please enter your phone">

<div class="form-group">
<label for="comment">Message*</label>
<textarea class="form-control" rows="7" name="message" id="form-message" default="Type us a message" required="required"></textarea>

<div class="checkbox">
<label><input type="checkbox" value="">Join mailing list</label>
<input type="submit" class="btm btn-success btn-send" value="Send message">
<p class="text-muted"><strong>*</strong> These fields are required.</p>


As many servers don't allow you to send emails in the name of someone whom is not registered, you should create an email on your server that's only for sending you the emails that your users write once they fill the contact form, then you should use a class like PHPMailer to send the email for you.

A simple example showing how to use PHPMailer would be:

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('', 'Mailer');
$mail->addAddress('', 'You');     // Add a recipient
$mail->addReplyTo('', 'User that submitted form');

$mail->Subject = 'Subject here';
$mail->Body    = 'Write here your message';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';

The example is taken from the link I've told you above.