0

I have created a simple html form to send an email using php. Currently whenever I try to send the info I just get redirected to my process.php page with and browser error (myserver.com unable to handle this request).

I have already tried sending a test email by making my php page just the mail() function and it does indeed work so it has to do with my code somewhere. I'm sure it's something simple so here is my code.

HTML (contact.html):

<!-- Contact form -->
<form id="form" action="process.php" method="post">
    <div>
        <label for='name'><span class='required'></span></label>
        <input id="Field1" type='text' name='name' placeholder='Type your Email Here' required/>
    </div>
    <div>
        <label for='message'><span class='required'></span></label>
        <textarea id="Field2" name='message' placeholder="Type a Message for us Here" required></textarea>
    </div>
    <div>
        <button type='submit'>SEND MESSAGE</button>
    </div>  
</form>

PHP (process.php):

<?php
//if "email" variable is filled out, send email
if(isset($_POST['name']) && isset($_POST['message'])) {

    //Email information
    $admin_email = "test@mydomain.com";
    $email = $_POST['name'];
    $subject = "Email from contact form";
    $comment = $_POST['message'];

    //send email
    if(mail($admin_email, $subject, $comment, "From:" . $email)) {
        echo '<p>Success</p>';
        header('Location: contact.html');
    } else { 
        echo '<p>Error sending message</p>'; 
    }
} else {
    echo '<p>Please fully fill out the form</p>'; 
}
?>
Ryan - Llaver
  • 528
  • 4
  • 19

2 Answers2

0

Check your php.ini file for mail configurations.

  • open php.ini file
  • search "mail function" there
  • set SMTP to your server
  • set sendmail_from (sending mail_ID)
  • Ambika
    • 594
    • 2
    • 11
    0

    You have syntax error in the PHP code you have shared - You are missing ; in the $subject = "Email from contact form" line.

    Please see bellow -

    <?php
    //if "email" variable is filled out, send email
    if (isset($_POST['name'], $_POST['message'])) {
    
        //Email information
        $admin_email = "test@mydomain.com";
        $email = $_POST['name'];
        $subject = "Email from contact form";
        $comment = $_POST['message'];
    
        // //send email
        if(mail($admin_email, $subject, $comment, "From:" . $email)) {
            echo '<p>Success</p>';
            header('Location: contact.html');
        } else { 
            echo '<p>Error sending message</p>'; 
        }
    } else {
        echo '<p>Please fully fill out the form</p>'; 
    }
    ?>
    
    Tosho Trajanov
    • 790
    • 8
    • 19