0

Hi guys I am developing a site and am producing an email form using php, I had various issues before such as " failed to connect to mailserver at "local host" port 25. Verify SMTP etc." so as a result a potential solution was to make the site online, therefore I have and the php code is inserted in the live site and the error did disappear however no mail was sent to email address listed. I have been working on this form for about a week now and tried out various different forms which are available on the internet but still no email received. My php code is below:

UPDATED PHP CODE

      <?php 

   //Checks for header injection 
   function has_header_injection($str){
       return preg_match("/[\r\n]/",$str);
   }


   if(isset($_POST['contact_submit'])){
       $name = trim($_POST['name']);
       $email = trim($_POST['email']);
       $msg = $_POST['message'];

       //check to see if name or email have header injecion 

       if(has_header_injection($name) || has_header_injection ($email)){
           die(); //if true kill script
       }

       if(!$name || !$email || !$msg){
           echo '<h4 class = "error"> All fields are required </h4> <a href = "contact.php" class = "button block"> Go Bacck and Try again </a>';
           exit;
       }

       //add recipient email to variable 
       $to = "mahdi.mashrafi@yahoo.com";
       $subject = "$name sent you a message via your contact form";

       $message = "Name: $name\r\n"; 
       $message .= "Email: $email\r\n";
       $message .= "Message:\r\n$msg";

       $message = wordwrap($message, 72);
        //set mail headers into a variable

        $headers = "MIME-Version: 1.0\r\n";
        $headers .="Content-type: text/plain; charset=iso-8859-1\r\n";
        $headers .= "From: $name <$email> \r\n";
        $headers .= "X-Priority: 1\r\n";
        $headers .= "X-MSMail-Priority:High\r\n\r\n";

     $status = mail ($to, $subject , $message , $headers);
     var_dump($status);

?>

**HTML FORM**

         <h5>Thanks for contacting </h5>
     <p>Please allow 24 housr to respond</p>
     <p> <a href = "/final" class = "bitton block">&laquo; Go to homepage </a> </p> 

    <?php } else {?>
    <form method = "post" action = "" id = "contact-form">
    <label for = "name">Your name </label>
    <input type = "text" id = "name" name = "name">

    <label for = "email">Your email</label>
    <input type = "email" id = "email" name = "email">

    <label for = "message">and your message </label>
    <textarea type = "message" id = "message" name = "message"></textarea>

     <input type = "submit" class = "button next" name = "contact_submit" value ="Send Message">
    </form>
    <?php } ?>
    </div>

Any good tutorials on email form will be great and potential solutions will also be hugely appreciated. I have literally no idea why the mail isnt being sent/recieved.

--------UPDATED THE PHP CODE HAS BEEN UPDATED -------

RESULT https://gyazo.com/e9abf452e3cf370297cf3a3a2f51d5f6

el mashrafi
  • 182
  • 1
  • 3
  • 14
  • Check the return value of `mail()` method like this: `$ret = mail(....)` and user `var_dump($ret);` to display what is in the variable. If it's true the email should be sent. Also check if you have a mail server on the server. Are you trying on a shared server? – Daniel Dudas Jul 21 '16 at 16:20
  • Swift mailer is by far the easiest head ache free way that I've come across for sending emails in PHP. It does all the heavy lifting for you. If you're okay with libraries you should definitely consider using this one. http://swiftmailer.org/docs/messages.html – Isabel Inc Jul 21 '16 at 16:21
  • @DanielDudas thanks for the suggestion yes I have tried it , check the question it has been updated. – el mashrafi Jul 21 '16 at 16:42
  • @Isabel Inc thanks for the answer ill give that a go if this goes south. – el mashrafi Jul 21 '16 at 16:43

1 Answers1

0

Try to do the following to debug the error message. Change

mail ($to, $subject , $message , $headers);

to

$status = mail ($to, $subject , $message , $headers);
var_dump($status);

if your $status false then it means you have some error sending mail. Let us know how this goes.

Update: https://stackoverflow.com/a/24644450/688924 has very good description of what could possibly go wrong in mail() function. Follow the instructions provided there. Even after following all the instruction you find mail() is not working then I would suggest you to try the alternatives like phpMailer, swiftmailer external libraries.

Community
  • 1
  • 1
Samundra
  • 1,869
  • 17
  • 28