1

This is my first time working in php and I'm running into issues getting mail() to work. I am using MAMP Pro to test (and normally there is a real email address to send to). When I click the send button it clears the form but does not show the thank you message (it also isn't showing the two dividers on the bottom that are just stripes) and I have yet to receive a successful email. Any help is appreciated!

<!doctype html>
<html lang="en-us">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!--[if IE]><meta http-equiv="cleartype" content="on" /><![endif]-->
    <title>Twin City Grill</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="styles/normalize.css">
    <link rel='stylesheet' type="text/css" href="styles/main.css">
</head>
<body>
    <nav class="navbar">
        <a href="index.php">
        <img class="logo" src="assets/Twin-City-Grill-white.png" alt="Twin City Grill, white blocky serif font">
        <h1 class="screen-reader-only">Twin City Grill</h1>
        </a>
        <div class="links-container">
            <a href="index.php" class="navbar-link">Contact</a>
        </div>
    </nav>

    <div class="wrapper">
        <div class="divider"></div>
        <div class="divider"></div>
        <div class="contact-form">

    <?php
    $action=$_REQUEST['action'];
    if ($action=="")
        {
        ?>
            <form  action="" method="POST" enctype="multipart/form-data" role="form" id="contact-form">
                <input type="hidden" name="action" value="submit">
                <h2>Contact Us</h2>
                <p class="form-instructions"><em>All fields are required.</em></p>
                <div class="form-group">
                    <label for="name" class="screen-reader-only">Name:</label>
                    <input type="text" name="name" id="name" placeholder="Name" required="required">
                </div>
                <div class="form-group">
                    <label for="phone" class="screen-reader-only">Phone Number:</label>
                    <input type="tel" name="phone" id="phone" placeholder="Phone Number" required="required">
                </div>
                <div class="form-group">
                    <label for="email" class="screen-reader-only">Email Address:</label>
                    <input type="email" name="email" id="email" placeholder="Email Address" required="required">
                </div>
                <div class="form-group">
                    <label for="message" class="screen-reader-only">Message:</label>
                    <textarea name="message" id="message" cols="30" rows="10" placeholder="Your Message" required="required"></textarea>
                </div>
                <div class="form-group">
                    <input id="submit" name="submit" type="submit" value="Send" >
                </div>
            </form>
        <?php
        }
    else
        {
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $phone=$_REQUEST['phone'];
        $message=$_REQUEST['message'];

        if (($name=="")||($email=="")||($message=="")||($phone==""))
            {
            echo "All fields are required, please fill <a href=\"\">the form</a> again.";
            }
        else{
            $from="From: $name<$email>\r\nReturn-path: $email";
            $subject="Message sent using your contact form";
            $email_message .= "Name: ".clean_string($name)."\n";
            $email_message .= "Email: ".clean_string($email)."\n";
            $email_message .= "Telephone: ".clean_string($phone)."\n";
            $email_message .= "Message: ".clean_string($message)."\n";
            mail("testemail@gmail.com", $subject, $email_message, $from);
            ?>
            <h2>Thanks!</h2>
            <p>We will get back to you as soon as possible. Have a great day!</p>
            <?php
            }
        }
    ?>
        </div>

        <div class="divider"></div>
        <div class="divider"></div>
    </div>

<footer>

</footer>

</body>
</html>

2 Answers2

0

It looks like the mail() function is failing silently for whatever reason. try something like this to get the actual error:

$success = mail("testemail@gmail.com", $subject, $email_message, $from);
if (!$success) {
    echo error_get_last()['message'];
}
circusdei
  • 1,907
  • 12
  • 27
0

Instead of $from variable, try this

$from = 'From: from@example.com' . "\r\n" .
    'Reply-To: from@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

Also check your spam

Punith R Kashi
  • 129
  • 1
  • 10