send email dosent work

Hi I have following php code I am trying to send email on my gmail account through contact form,When I submit the the form it shoe me the message “Thank you for contacting us. We will be in touch with you very soon” from my contactus.php but the email dosent appears in inbox or spam

contact.php

First Name *
Last Name *
Email Address *
Telephone Number
Comments *

###################################################################

contactus.php

<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "[email protected]"; $email_subject = "Your email subject line"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['first_name'])||!isset($_POST['last_name'])||!isset($_POST['email'])|| !isset($_POST['telephone'])||!isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email'];// //$required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?>
           <!-- include your own success html here -->   Thank you for contacting us. We will be in touch with you very soon.   <?php
		   echo $email_to; } ?>

I believe I tried to answer this for you on the phpfreaks forum but ran into issues with someone on there that did not let me give you the info you deserved. So I will do it here instead.

One likely issue is that you are setting the FROM to the email that was provided rather than your domain. The email is not technically coming from the person that posted the form, it’s coming from your domain, so the email programs will have issues with that cause other factors will not match up at all and trigger a spam filter. The REPLY is fine to leave for the email provided.

You’re also missing some important aspects of an email, like the other needed headers. Take a look at this article I wrote on sending emails in php and the steps needed to get them to an inbox. I know you won’t likely want to do most of the things in the article cause they aren’t free, so focus on the “Basics” section and that should help you out a lot. Then make sure to whitelist your domain in your gmail account too.

http://amecms.com/article/Sending-email-in-PHP-and-reaching-the-inbox

I did following changes but it still dosent go to inbox
contactus.php
################################################################

<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "[email protected]"; $email_subject = "Your email subject line"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['first_name'])||!isset($_POST['last_name'])||!isset($_POST['email'])|| !isset($_POST['telephone'])||!isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email'];// //$required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers //$to = '[email protected]'; //$subject = 'PHP mail is really cool!'; $body = 'This is the content for your email.'; $headers = 'MIME-Version: 1.0' . "rn"; $headers .= 'Content-Type: text/plain; charset=iso-8859-1' . "rn"; $headers .= 'From: "'.$from.'"
           <!-- include your own success html here -->   Thank you for contacting us. We will be in touch with you very soon.   <?php
		   echo $email_to; } ?>

Well now you are using $from in the FROM header but you haven’t defined a value for $from anywhere, so it has no value. There isn’t really a need to make a var for that, just put the email address in the FROM line. Also you are missing \ in your header lines.
Should be like this
[php]. “\r\n”;[/php]
You’re also not even using the $email_message in the $body, so that info will not be in the email at all.

I did following changes, it still dosent work no email in inbox

<?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "[email protected]"; $email_subject = "Your email subject line"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['first_name'])||!isset($_POST['last_name'])||!isset($_POST['email'])|| !isset($_POST['telephone'])||!isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email'];// //$required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "/^[A-Za-z .'-]+$/"; if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.
'; } if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers //$to = '[email protected]'; //$subject = 'PHP mail is really cool!'; $body = 'This is the content for your email.'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: text/plain; charset=iso-8859-1' . "\r\n"; $headers .= 'From: . "
           <!-- include your own success html here -->   Thank you for contacting us. We will be in touch with you very soon.   <?php
		   } ?>

First, please place code inside the PHP tags as it is much easier for us to move them to our applications for testing.

So, you need to debug what is being sent out. Most likely you have a small error in the formatting.

BUT, first, your mail command is:
[php]mail($email_to, $email_subject, $body, $headers)[/php]
There is no ending semicolon at the end of the line. Try:
[php]mail($email_to, $email_subject, $body, $headers);[/php]
Also, to debug this, you should use this type of code so you can see what is really being sent to the “mail” function:
[php]
die("email_to: " . $email_to . "
email_subject: " . $email_subject . "
headers: " . $headers . "
body: " . $body);
mail($email_to, $email_subject, $body, $headers)
[/php]
What this will do is show you each of the parts of your data sent to the function.
Then, you can see where the error is. But, again, it might just be your missing semicolon at the end of the line. Good luck, let us know…

This line is still wrong and has syntax error. Get rid of the < a the start of the email. Next, as I have said before, the email address needs to be from YOUR DOMAIN, I don’t think you own yahoo so having a yahoo address in there isn’t going to work.
[php]$headers .= ‘From: . " <[email protected]’ . “\r\n”;[/php]
[php]$headers .= ‘From: "[email protected]" [email protected]’ . “\r\n”;[/php]

Fatsol, you can have ANY address in your FROM line. But, yes, he is missing the “>”. I missed that.

This is how spammers work, they send out some fake FROM and try to get you to click on a link
that sends you to a site that infects your system.

Anyway, On one of my sites, I use a different email address because I do not want it to come back
to my server, but, to one of my email drops. Any return address will work. But, between the missing
semicolon and “>” I bet it will work after fixing those two. Nice catch… Didin’t even see it…

Well yes of course you can have any address you want in the FROM line, but the issue is the delivery of the email and the mismatch of the address to the domain sending it is usually a sure spam trigger. Yes the syntax error was likely helping the issue to persist, but what I was trying to get across to him was the need to have the FROM be an email that actually exists at his domain from which the form was submitted. Granted that is not the end all helping factor in his script but it should help in this instance.

He also would be wise to add his domains email address to his yahoo trusted contact list, so that it should certainly get to his inbox (once the FROM thing is fixed).

Sponsor our Newsletter | Privacy Policy | Terms of Service