Problem receiving mail from Contact Form

the mail() function

Moderators: macek, egami, gesf

stevewhit40
New php-forum User
New php-forum User
Posts: 1
Joined: Sun Nov 06, 2011 5:31 am

Problem receiving mail from Contact Form

Postby stevewhit40 » Sun Nov 06, 2011 5:48 am

Hi,

I'm new to using PHP and am stuck trying to solve an annoying email problem.

I am a developing a site with a contact form which calls a PHP file to validate the information input, before sending an email and redirecting the user to an html thank you page.

In testing, the call to the PHP code works, the validation works and the redirection works. But I don't receive the email. I tested the email from my Hotmail and Gmail accounts - it works ok. I have tried amending the PHP code - with the same results. My site is hosted on a Windows server.

My contact form code and PHP code are shown below. Any help will be greatly appreciated.

Contact Form Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>STEVE WHITTAKER - Contact</title>
<link href="sjw.css" rel="stylesheet" type="text/css" /></style>
</head>

<body>
<div id=wrapper>
<P><img border=0 alt=Logo src="img/logo-header.gif" width=756 height=76></p>
<div id=nav-wrapper>
<UL id=dmenu>
<LI><A href="http://www.stephenjameswhittaker.com/1introduction.htm">Introduction</A></LI>
<LI><A href="http://www.stephenjameswhittaker.com/2keyskills.htm">Key
Skills</A></LI>
<LI><A href="http://www.stephenjameswhittaker.com/3career.htm">Career</A></LI>
<LI><A href="http://www.stephenjameswhittaker.com/4experience.htm">Experience</A></LI>
<LI><A href="http://www.stephenjameswhittaker.com/5portfolio.htm">Portfolio</A></LI>
<LI><A href="http://www.stephenjameswhittaker.com/6testimonials.htm">Testimonials</A></LI>
<LI><A href="http://www.stephenjameswhittaker.com/7qualifications.htm">Qualifications</A></LI>
<LI><A href="http://www.stephenjameswhittaker.com/8contact.php">Contact
Steve</A></LI></UL>
<P>&nbsp;</p></div>
<div id="header">
<p></p>
<img src="img/contact-header.gif" width=281 height=170>
<img src="img/contact-header-rhs.jpg" width=421 height=170></div>
<div id="content">
<table width=100% border=0 cellpadding=0 cellspacing=0>

<tr>
<td width=231 height=120 valign="top">&nbsp;</td>
<td width=471 valign="top" class="LHS-links">
<p>&nbsp;</p>
<p><span class="RHS-link"><strong><font color="#333333">Steve Whittaker</font></strong></span></p>
<br />
<p><span class="RHS-link"><font color="#333333">W</font>&nbsp;
<a href="http://www.stephenjameswhittaker.com">www.stephenjameswhittaker.com</a></span></p>
<br />
<p><span class="RHS-link"><font color="#333333">T</font></span>
<span class="RHS-link">&nbsp; +(34) 63 43 10 600</span></td>
</tr>
<tr>
<td height="20" colspan=2>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<form method="POST" action="send_form_email.php">
<table width=471 cellpadding=0 cellspacing=0 border=0>
<tr>
<td width=2%>&nbsp; </td>
<td width=25% valign=top>
<label for="name"><span class="RHS-link"><font color="#333333">Name:</font></span></label></td>
<td width=73% valign=top>
<input type="text" name="name" maxlength="50" size="30"></td>
</tr><tr>
<td>&nbsp; </td>
<td><label for="email_from"><span class="RHS-link"><font color="#333333">Email Address: *</font></span></label></td>
<td><input type="text" name="email_from" maxlength="50" size="30"></td>
</tr><tr>
<td>&nbsp; </td>
<td valign=top><label for="comments"><span class="RHS-link"><font color="#333333">Your Message:</font></span></label></td>
<td><textarea rows="9" name="comments" cols="40" maxlength="750"></textarea></td>
</tr><tr>
<td>&nbsp; </td>
<td><input type="submit" value="Send Message" name="submit"></td>
<td><input type="reset" value="Clear" name="reset"></td>
</tr></table>
</form></td></tr>
<tr>
<td height="20" colspan=2>&nbsp;</td>
</tr></table>

<div id="clear"><img src="img/dotted-line.gif" width=702 height=2></div>

</div>
</div>

</body>
</html>


PHP Code
<?php

if(isset($_POST['submit'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "hi@stephenjameswhittaker.com";
$email_subject = "A Nice Message to stephenjameswhittakerdotcom";

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.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email_from']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$name = $_POST['name']; // required
$email_from = $_POST['email_from']; // 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.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\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);
header("Location: thanks.htm");
}
?>

Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest

cron