0

I have an html form that sends user entered data to PHP.

I am unsure why my code is not working. I am using POST to retrieve the data. I am able to use a javascript alert window to show the form data. But something is wrong in my code that is preventing my email from being received, if even sent.

Here is my html form:

 <form action="contact.php" method="POST" id="messageForm" name="messageForm">
   <label>Name</label>
   <input class="form-control" id="name" name="name" type="text" placeholder="Enter Full Name" />
   <label>Phone</label>
   <input class="form-control" id="phone" name="phone" type="text" placeholder="Enter Phone Number" />
   <label>Email</label>
   <input class="form-control" id="email" name="email" type="text" placeholder="Enter Valid Email" />
   <label>Interest</label>
   <select class="form-control" id="interest" name="interest" value="interest">
     <option value="" disabled selected>Make selection</option>
     <option value="one">one</option>
     <option value="two">two</option>
     <option value="three">three</option>
   </select>
   <label>Comments</label>
   <textarea class="form-control" id="comment" name="comment" style="resize: none;" rows="10"></textarea>
   <input class="btn btn-danger" type="submit" id="submitMessage" name="submitMessage" value="Submit" />
 </form> 

Inside the form, I have my PHP looking like this:

 <?php
   if(isset($_POST['submitMessage']))  
   {
     $name = htmlspecialchars($_POST['name']);
     $phone = htmlspecialchars($_POST['phone']);
     $email = htmlspecialchars($_POST['email']);
     $interest = htmlspecialchars($_POST['interest']);
     $comment = htmlspecialchars($_POST['comment']);
     $cc = 'myEmail@yahoo.com';

     $to = 'myOtherEmail@gmail.com';  
     $subject = 'Web Message';
     $headers = "From: ".$email."" . "\r\n";
     $headers .= "CC: " . $cc . "\r\n";
     $headers .= "MIME-Version: 1.0\r\n";
     $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
     $message = "You have received a message<br /><br />";      
     $message .= "Greetings<br /><br />";
     $message .= '<html><body>';
     $message .= '<table>';  
     $message .= '<tr><th>Name</th><th>Phone</th><th>Email</th><th>Interest</th><th>Comment</th></tr>';
     $message .= '<tr><td>'.$name.'</td><td>'.$phone.'</td><td>'.$email.'</td><td>'.$interest.'</td><td>'.$comment.'</td></tr>';
     $message .= '</table></body></html>';
     $message .= 'Thank you';

     @mail($to, $subject, $message, $headers);
     echo ("<script language='javascript'>
            window.alert('Thank you, ".$name.".  Your message has been sent.')
            window.location.href=''
            </script>"); 
    }
  ?>

I am not receiving an on-page error. It appears the email is being sent, but no email is being received.

Please help.

Thank you.

HoodCoderMan
  • 103
  • 7
  • 26
  • 2
    you should take out the @ on the mail and capture the error to see if there is any – Saikios Aug 05 '14 at 02:47
  • 1
    *"But something is wrong in my code that is preventing my email from being received, if even sent."* - There's nothing wrong with your code. Check your logs (and Spam) and make sure that `mail()` is available for you to use. Are you running this from a local machine or hosted service? – Funk Forty Niner Aug 05 '14 at 02:47
  • 4
    __Don't__ use `@` to suppress errors - that's the very thing you need when you're debugging. __Do__ check the return value of `mail()` - if it's false your mail hasn't been accepted. Be aware that `mail()` requires very special handling with Windows, and lastly, look at using a decent mailer like PHPMailer rather than `mail()`. It'll make your life a whole lot easier. –  Aug 05 '14 at 02:48
  • If your SMTP is running latest version of Postfix, chances are that SMTP Authentication will be required for sending mail. – James Wong Aug 05 '14 at 02:49
  • @Fred I am using both a local machine and a hosted service. No email has been received. Not sure if the email was even sent. – HoodCoderMan Aug 05 '14 at 02:55
  • You're not getting the mail on both? – Funk Forty Niner Aug 05 '14 at 02:56
  • Note that your HTML looks quite broken in `$message`. You may want to review that. Also at first you may just want to send a text email until the email goes through. Then change the message to HTML if you prefer. In most cases the HTML in emails does not include the `` and `` tags, but for sure they should be at the start and end of your message. – Alexis Wilke Aug 05 '14 at 02:58
  • 1
    Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Aug 05 '14 at 02:58
  • For further testing run the following code (**with your valid email-Address!**). I don't find the correct way to format it but just copy and paste it. ` $to = "yourmail@example.com"; $subj = "Test Mail"; $text = "My test mail text. Great if I could see this in my inbox"; $header = "From: My Name \r\n"; if (mail($to, $subj, $text, $header)) echo 'Mail sent'; else echo "error while sending mail.";` – wedi Aug 05 '14 at 03:07
  • @Fred - No, I am not receiving from the local host or the web server. I did just learn that I need to retrieve the SMTP information to be able to send from my web server. – HoodCoderMan Aug 05 '14 at 03:08
  • I think that you has not complete the config file. If you only use the `mail()`function, you need to open the mail expansion, and set the SMTP smtp_portand so on, and most important -your username and your password.Without that, mail can not be sent.Also, you can use PHPMail class to send. if you know chinese, [click here](http://iat.net.cn/several-ways-to-send-email.html), i have a blog about that – iatboy Aug 05 '14 at 03:54

0 Answers0