0

My PHP form isn't submitting successfully. I keep getting the custom error that I wrote ('Oops there was a problem. Please try again").

any help would be greatly appreciated. I'm totally new to PHP so I'm thinking maybe some of my php variables are linked wrong and arent connecting with my mailer-new.php file?

Thanks in advance,

   <section class="form-body">
    <form method="post" action="mailer-new.php" class="contact-form" >
        <div class="row">
            <?php
                if ($_GET['success']== 1){
                    echo " <div class=\"form-messages success\"> Thank you! 
                your message has been sent. </div>";
                }
                if ($_GET['success']== -1){
                    echo " <div class=\"form-messages error\"> Opps there was a 
                  problem. Please try again </div>";
                };
                ?>
            <div class="field name-box">
                <input type="text" name="name" id="name" placeholder="Who 
                    Are You?" required/>
                <label for="name">Name</label>
                <span class="ss-icon">check</span>
            </div>
            <div class="field email-box">
                <input type="text"  name="email" id="email" 
                    placeholder="name@email.com" required/>
                <label for="email">Email</label>
                <span class="ss-icon">check</span>
            </div>
            <div class="field msg-box">
                <textarea name="message" id="msg" rows="4" 
                    placeholder="Your message goes here..."/></textarea>
                <label for="message">Msg</label>
                <span class="ss-icon">check</span>
            </div>
            <input class="button" type="submit" value="Send"/>
        </div>
    </form>
</section>

MAILER.PHP

<?php

// Get the form fields, removes html tags and whitespace.
$name    = strip_tags(trim($_POST["name"]));
$name    = str_replace(array("\r","\n"), array(" ", " " ), $name);
$email   = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);

// Check the data.
if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: http://www.conallen.ie/index.php?
 success=-1#form");
    exit;
}

// Set the recipient email address. Update this to YOUR desired email address.
$recipient = "allenconallen46@gmail.com";

// Set the email subject.
$subject = "New contact from $name";

// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";

// Build the email headers.
$email_headers = "From: $name <$email>";

// Send the email.
mail($recipient, $subject, $email_content, $email_headers);

// Redirect to the index.html page with success code
header("Location: http://www.conallen.ie/index.php?success=1#form");

?>    
Masivuye Cokile
  • 4,723
  • 3
  • 16
  • 33
con
  • 25
  • 9
  • Your code looks reasonable, although you shouldn't need to call filter_var on the email address twice. I'd suggest you debug this to see what is in the values before your "check the data step". E.g. `var_dump($name, $email, $message);exit;` – Tim Fountain Feb 19 '18 at 16:02
  • @TimFountain Thanks for your reply, I have the response alert messages appearing. But now the email doesn't arrive in my inbox. I've no idea why they aren't sending though. Ill try your suggestion now - thanks – con Feb 19 '18 at 16:38

1 Answers1

0

This code works correctly in my local machine, even though email is not sent response messages are coming correctly. The changes I have done is changed the host name and made the two lined header redirect into one line in the failed response. Also you have mentioned in the HTML the file name as action="mailer-new.php" and the file name mentioned in the question as MAILER.PHP. Are they same in the code?

To check whether the mail is sent or not remove the header redirect and update like this. If you are getting a failed response means mail is not configured in your server.

if(mail($recipient, $subject, $email_content, $email_headers)) {
echo "mail sent";
} else {
 echo "mail sent failed";
}

Alternatively you can use the SMTP for sending email. You can use a library called PHP Mailer and can use any of the valid email address like a gmail account. Please have look at this question if that's the case

Sending email with PHP from an SMTP server

Krishnadas PC
  • 3,966
  • 34
  • 33
  • thanks for your reply. I have got the response success messages to appear now. But unfortunately, the emails are not landing in my email account. Do you have any idea as to why they arent sending? Also yes the file name is mailer-new.php sorry for the confusion. many thanks – con Feb 19 '18 at 16:36
  • Updated the answer. Please check it. – Krishnadas PC Feb 19 '18 at 16:56
  • I updated the header redirect and it notified me that the email was sent. but it still doesn't appear in my inbox. Thanks again. – con Feb 19 '18 at 17:45
  • Have you checked it in the spam folder? If it's still not working you must give it a try with the SMTP gmail. It should work. – Krishnadas PC Feb 19 '18 at 17:52
  • Yeah, I've checked my spam folder several times. I've also watched a few tutorials and still no emails appear. It seems like the code is working in ways but then the email just doesn't send correctly. Ill google SMTP Gmail now. thanks again – con Feb 19 '18 at 18:01
  • I finally fixed this issue. The problem wasn't my code, it was with the server i was hosting on. thanks for your help! – con Feb 21 '18 at 14:38