2

I've spent the best part of an hour now looking at my code and others like it online to see why my PHP isn't working, but I cannot for the life of me figure it out.

I'm trying to create a basic contact form that takes an email address, a subject and then a message body and sends it to an account.

I'm using MAMP Pro to run my localhost server and have setup their 'Postfix' option so that the domain of outgoing emails is: 'gmail.com'. But whatever I seem to try just doesn't work!

I'll try and keep my code as simple as possible and select the parts that are only specific to the question.

My PHP is:

<?php

session_start();


if (isset($_POST["submit"])) {
    $to = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $from = 'joebloggs@gmail.com';

    if (!$_POST['email']) {
        $errEmail = 'Please enter a valid email address';
        $message = $errEmail;
    }

    if (!$errEmail)
    {
        if (mail ($to, $subject, $message))
        {    
            $message='Your message has been sucessfully sent! We aim to respond to all mail within 24-hours!';
        }
        else
        {
            $message='There has been an error sending your invite. Please try again!';
        }
    }
}

?>

My HTML is:

 <form class="form-horizontal" role="form" method="post" action="contact.php" >
    <div class="form-group">
        <label for="name" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="name" name="name">
        </div>
    </div>

    <div class="form-group">
        <label for="email" class="col-sm-2 control-label">Email</label>
        <div class="col-sm-10">
            <input type="email" class="form-control" id="email" name="email">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>
        </div>
    </div>

    <div class="form-group">
        <label for="subject" class="col-sm-2 control-label">Subject</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="subject" name="subject">
        </div>
    </div>

    <div class="form-group">
        <label for="company" class="col-sm-2 control-label">Company</label>
        <div class="col-sm-10">
            <input type="text class="form-control" id="subject" name="company" value="<?= $user['username']; ?>" readonly>
        </div>
    </div>

    <div class="form-group">
        <label for="message" class="col-sm-2 control-label">Message</label>
        <div class="col-sm-10">
            <textarea class="form-control" rows="4" name="message">
            </textarea>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">
        </div>
    </div>
</form>

When I execute the code (press submit) it runs absolutely fine and displays the 'Your message has been successfully sent!' message; however it just doesn't send the email.

All help very much appreciated. Thank you!

  • 3
    Although this is a duplicate question I upvoted you for spending the time to reduce this to a minimal verifiable example. We don't get enough of that here. – John Conde Jan 12 '17 at 21:49
  • 1
    check your spam box; many will send it to that because of a missing `From: email_address` and proper headers. Mail did its job, so you need to find out why it was never received. It might even have been rejected. Having an SPF record also helps. – Funk Forty Niner Jan 12 '17 at 21:53
  • Thanks for your suggestion Fred - I have to admit this is something that I did already attempt. I've also tried different email addresses (one gmail and one plus.net) and both are not showing any mail. – GuestUser140561 Jan 12 '17 at 22:00

0 Answers0