Php mail sent but not received in email

Hi guys,

I’m a bit of a novice when it comes to php. I have recently built a “Contact Us” form and I am having difficulties getting the results sent to the desired email address.

According to the Mail Log, the message is being sent, but never received. Below is the code for the php script for the webform:

<?php

/* Subject and Email Variables */

	$emailSubject = 'Little Treasures Contact Form';
	$webMaster = 'info@littletreasures-shop.co.uk';
	
/* Message Info */

	$NameField = $_POST['Name'];
	$PhoneField = $_POST['Phone'];
	$EmailField = $_POST['Email'];
	$SubjectField = $_POST['Subject'];
	$MessageField = $_POST['Message'];
	
	$body = <<<EOD
<br><hr><br>
Name: $Name <br>
Phone: $Phone <br>
Email: $Email <br>
Subject: $Subject <br>
Message: $Message <br>
EOD;

	$headers = "From: $Email\
";
	$headers .= "Content-type: text/html\
";

if(mail($webMaster, $emailSubject, $body, $headers))
{
echo header("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
}
else
{
echo header("Location: http://littletreasures-shop.co.uk/Contact/messagesent/false.html");
}

?>

Any input would be highly appreciated!

Thank you in advance,

Ryan.

Hi Ryski. Welcome to the forums. :slight_smile:

Do you mean that you receive an email, but it’s blank? If so, the issue is simple. Instead of this:

Name: $Name <br>
Phone: $Phone <br>
Email: $Email <br>
Subject: $Subject <br>
Message: $Message <br>

you need this:

Name: $Name[COLOR="#FF0000"]Field[/COLOR] <br>
Phone: $Phone[COLOR="#FF0000"]Field[/COLOR] <br>
Email: $Email[COLOR="#FF0000"]Field[/COLOR] <br>
Subject: $Subject[COLOR="#FF0000"]Field[/COLOR] <br>
Message: $Message[COLOR="#FF0000"]Field[/COLOR] <br>

You don’t actually have any variables called “Name”, “Phone” etc. :slight_smile:

Also change this:

$headers = "From: $Email\
";

to

$headers = "From: $Email[COLOR="#FF0000"]Field[/COLOR]\
";

See if that helps. :slight_smile:

Hi ralph,

Thank you for the response. It’s not that the email is blank, the email just doesn’t land in the inbox/spam folder at all, but the server log shows that it has been sent. I’ve tried using different target emails (gmail, yahoo, hotmail, etc.) it appears that the email has been sent. However, it never reaches its destination.

Tried the suggested adjustments with the same results. it’s amazing how something so simple can be so frustrating!

You might need to give us some more information. For example, show us the HTML code for your form, and explain where this PHP is in relation to that form.

the code for the form is:

<form action="contact.php" method="post" id="contact">
            <label for="Name2"><strong> Name </strong><br/>
            </label>
            <input name="Name" type="text" id="Name2" size="45">
            <label for="Phone"><strong><br/>
              Phone Number<br/>
            </strong></label>
            <input name="Phone" type="text" id="Phone" size="45">
            <strong>
              <label for="Email"><br/>
                Email<br/>
              </label>
            </strong>
            <input name="Email" type="text" id="Email" size="45">
            <label for="Subject"><strong><br/>
              Subject<br/>
            </strong></label>
            <input name="Subject" type="text" id="Subject" size="45">
            <label for="Message"><strong><br/>
              Message<br/>
            </strong></label>
            <textarea name="Message" id="Message" cols="35" rows="3"></textarea>
            <br/>
            <input type="submit" name="Send" id="Send" value="Submit">
            <input type="reset" name="Reset" id="Reset" value="Reset">
          </form>

and the php script is in the same directory as the index for the contact page.

As an experiment, try replacing the contents of your contact.php file with this code:


<?php

$your_email = "info@littletreasures-shop.co.uk"; // email address to which the form data will be sent
$subject = "Little Treasures Contact Form"; // subject of the email that is sent

if (!isset($_POST['Send'])) {
    header( "Location: http://littletreasures-shop.co.uk/Contact/index.html" );
  }

if (isset($_POST["Send"])) {
	$nam = $_POST["Name"];
	$pho = $_POST["Phone"];
	$ema = trim($_POST["Email"]);
	$sub = $_POST["Subject"];
	$com = $_POST["Message"];

	if (get_magic_quotes_gpc()) { 
	$nam = stripslashes($nam);
	$ema = stripslashes($ema);
	$pho = stripslashes($pho);
	$sub = stripslashes($sub);
	$com = stripslashes($com);
	}

$error_msg=array(); 

if (empty($nam) || !preg_match("~^[a-z\\-'\\s]{1,60}$~i", $nam)) { 
$error_msg[] = "The name field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
}

if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
	$error_msg[] = "Your email must have a valid format, such as name@mailhost.com";
}

if (empty($pho) && !preg_match("/^[A-z\\/0-9\\s\\(\\)]{1,60}$/", $pho)) { 
$error_msg[]="The phone field can contain only digits, spaces and parentheses";
}

if (empty($sub) || !preg_match("~^[a-z\\-'\\s]{1,60}$~i", $sub)) { 
$error_msg[] = "The subject field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
}

$limit = 1000;

if (empty($com) || !preg_match("/^[0-9A-Za-z\\/-\\s'\\(\\)!\\?\\.,]+$/", $com) || (strlen($com) > $limit)) { 
$error_msg[] = "The Comments field must contain only letters, digits, spaces and basic punctuation (&nbsp;'&nbsp;-&nbsp;,&nbsp;.&nbsp;), and has a limit of 1000 characters";
}

// Assuming there's an error, refresh the page with error list and repeat the form

if ($error_msg) {
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
<style>
	body {background: #f7f7f7; font: 100%/1.375 georgia, serif;padding: 20px 40px;}
	div {margin-bottom: 10px;}
	.content {width: 40%; margin: 0 auto;}
	h1 {margin: 0 0 20px 0; font-size: 175%; font-family: calibri, arial, sans-serif;}
	fieldset {border: 0; padding: 0; margin: 0;}
	legend {position: absolute; left:-9999px}
	label {margin-bottom: 2px;}
	input[type="text"], input[type="email"], textarea {font-size: 0.75em; width: 98%; font-family: arial; border: 1px solid #ebebeb; padding: 4px; display: block;}
	input[type="radio"] {margin: 0 5px 0 0;}
	textarea {overflow: auto;}
	.hide {display: none;}
	.err {color: red; font-size: 0.875em; margin: 1em 0; padding: 0 2em;}
</style>
</head>
<body>
	<div class="content">
		<h1>O dear!</h1>
		<p>Unfortunately, your message could not be sent. The form as you filled it out is displayed below. Please make sure each field is completed, and please also address any issues listed below:</p>
		<ul class="err">';
foreach ($error_msg as $err) {
echo '<li>'.$err.'</li>';
}
echo '</ul>
	<form method="post" action="', $_SERVER['PHP_SELF'], '">
		<fieldset> 
			<legend>Contact Us</legend>
				<div>
					<label for="name">Name</label>
					<input name="Name" type="text" size="40" maxlength="60" id="name" value="'; if (isset($_POST["Name"])) {echo $nam;}; echo '">
				</div>
				<div>
					<label for="email">Email Address</label>
					<input name="Email" type="email" size="40" maxlength="60" id="email" value="'; if (isset($_POST["Email"])) {echo $ema;}; echo '">
				</div>
				<div>
					<label for="phone">Telephone</label>
					<input name="Phone" type="text" size="40" maxlength="60" id="phone" value="'; if (isset($_POST["Phone"])) {echo $pho;}; echo '">
				</div>
				<div>
					<label for="subject">Subject</label>
					<input name="Subject" type="text" size="40" maxlength="60" id="subject" value="'; if (isset($_POST["Subject"])) {echo $sub;}; echo '">
				</div>
				<div>
					<label for="comm">Comments</label>
					<textarea name="Message" rows="10" cols="50" id="comm">'; if (isset($_POST["Message"])) {echo $com;}; echo '</textarea>
				</div>
				<div>
					<input type="submit" name="Send" value="Send">
				</div>
		</fieldset>
	</form>
</body>
</html>';
exit();
} 

$email_body = 
	"Name: $nam\
\
" .
	"Email: $ema\
\
" .
	"Telephone: $pho\
\
" .
	"Subject: $sub\
\
" .
    "MESSAGE:\
\
" .
	"$com" ; 

// Assuming there's no error, send the email and redirect to Thank You page

if (isset($_REQUEST['MESSAGE']) && !$error_msg) {
mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\\r\
" . "Reply-To: $nam <$ema>");
header ("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
exit();
}  
}

OK, tried that. Now the email doesn’t appear in the Sendmail log and it displays a blank page when you click submit. Still no email in the inbox/junk folder either. Could it be a server side issue?

EDIT: the site is hosted on serversfree.com

Hm, sounds like a dud server to me. Does it support php?

Ah man, I’m such an eejit: I made one mistake at the end of that code in post #6. If you correct the error in red below, I reckon things should work fine. In the final lines of the code, change this:

if (isset($_REQUEST['[COLOR="#FF0000"]MESSAGE[/COLOR]']) && !$error_msg) {
mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\\r\
" . "Reply-To: $nam <$ema>");
header ("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
exit();
}  
}

to this:

if (isset($_REQUEST['[COLOR="#FF0000"]Message[/COLOR]']) && !$error_msg) {
mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\\r\
" . "Reply-To: $nam <$ema>");
header ("Location: http://littletreasures-shop.co.uk/Contact/messagesent/true.html");
exit();
}  
}

And forget what I said about the hosting. The serversfree.com website clearly states that PHP is supported.

Works perfectly! You sir, are an absolute legend!

Thank you very much for your help.

Ah, great. Glad to hear that. :slight_smile:

It also has security protection now. One thing you might want to change is the error page. If you just hit submit without filling in the fields, you get a pretty plain Error page. This could be styled to look like the rest of your site. (Just thought I’d better point that out.) If you need help with that part of it, just shout.