2

Okay, so I'm working on an online portfolio and I'm working on a mail form. It asks a name, email and an message. Then when pressing send it will send an email to me with all of that and redirect the user to another page. However somewhere it seems to die. While still redirecting the user to the form complete page.

<form  method="post" name="contactform" action="<?php $_SERVER['PHP_SELF'] ?>">
    <input type="text" class="name" required maxlength="50" placeholder="Your name" name="name">
    <input type="text" class="email" required maxlength="50" placeholder="Your email" name="email">
    <textarea required maxlength="500" placeholder="Your message" name="message"></textarea><br><br>
    <input name="submit" type="submit" name="submit" value="submit">
</form>

and this is the PHP code, i's on the top of the document with no spaces anywhere after the ?> closes

<?php
if(isset($_POST['submit']))
{
    $errors = '';
    $myemail = 'lucasvandongen10@gmail.com';//<-----Put Your email address here.
    if(empty($_POST['name'])  || 
       empty($_POST['email']) || 
       empty($_POST['message']))
    {
        $errors .= "\n Error: all fields are required";
    }
    else
    {
    }

    $name = $_POST['name']; 
    $email_address = $_POST['email']; 
    $message = $_POST['message']; 

    if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address))
    {
        $errors .= "\n Error: Invalid email address";
    }
    else
    {
    }
    if( empty($errors))

    {

        $to = $myemail;
        $email_subject = "Contact form submission: $name";
        $email_body = "You have received a new message. "." Here are the details:\n Name: $name \n "."Email: $email_address\n Message: \n $message";
        $headers = "From: <noreply@lucasvandongen.com>\n";
        $headers .= "Reply-To: $email_address";
        mail($to,$email_subject,$email_body,$headers);
        //redirect to the 'thank you' page
        header('Location: contact-form-thank-you.html');
    }
    else
    {
    }
}
else
{
}
?>
fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • And just as a sidenote, It has send me an email once, I didn't notice, thinking it died and changed the code but I'm now unable to find what did make it work – Lucas van Dongen Feb 25 '16 at 22:55
  • Does email sending work when you call the `mail()` function directly? It may be due to a mail blocked because it looks like spam, etc. – A.L Feb 25 '16 at 22:56
  • If you else-blocks are empty, you can remove them altogether. And indenting the code makes it easier to read. You also just assume that the mail was sent. – Qirel Feb 25 '16 at 22:59
  • Oh wow, that actually seems to be the case yes! However I'd think it would end up in my spam folder but it hadn't so I assumed it died. – Lucas van Dongen Feb 25 '16 at 23:02
  • Gmail filters those as spam if not configured correctly. It hates the `mail()` function – Phiter Feb 25 '16 at 23:02

0 Answers0