I've recently been "prettifying" some tried and true email forms to make them more mobile friendly, and thought I was going out of my mind seeing the PHP mail() function now randomly failing (and returning FALSE). Well it wasn't random. After nearly pulling out all my hair, I finally realized that anytime I entered an invalid domain for the form field that becomes my "reply-to" address, mail() would fail, and return FALSE! Note, these aren't "malformed" email addresses (which i already check for), just invalid ones (like "name@goooogle.com").
I've included a test form code below if you think it matters, but do you think this is a new "feature" of PHP, or something that just my hosting company's server is doing? If it is PHP, is there maybe some new "testDomain()" function I could also add to my forms? It would be nice to alert the user who made a legitimate mistake, but all I can tell them for sure is that their mail attempt failed. After all, mail() doesn't return some friendly little error code that indicates what happened, it just returns true or false. In fact when it does return false, it doesn't even report an error in the log file.
Truth be told, this is the ONLY time I've ever gotten mail() to fail in my forms. But I think assuming the failure always meant the domain was bad would be unwise.
<!DOCTYPE HTML>
<html>
<head> <title> Simple Test Email Form;</title> </head>
<!--
<?php
// define variables and set to empty values
$name = $email = $comments = ""; // $address = $citystate = $zip = $phone not used
$nameErr = $emailErr = $commentsErr = "";
$headers = $email_message = $sendersIP = "";
$email_to = "myaddress@mydomain.org"; // this is bogus!!!
$email_subject = "Private Mailform";
$status = "Form Not Yet Processed";
// some basic security functions
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// mail processing
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"])) { $nameErr = " Name is required"; }
else { $name = test_input($_POST["name"]); }
if (empty($_POST["email"])) { $emailErr = "Email is required"; }
else { $email = test_input($_POST["email"]); }
// at least email should be validated
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "Invalid email format"; }
// keep senders IP, so we can watch for idiots.
$sendersIP = $_SERVER['REMOTE_ADDR'];
$comments = $_POST["comments"];
// So is all well?
if (empty($nameErr) && empty($emailErr) && empty($commentsErr) )
{
$email_message = $headers = "";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "IP: ".$sendersIP."\n";
$email_message .= "Comments: "."\n\n".clean_string($comments)."\n";
// create regular email headers
$headers .= 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail_sent = mail($email_to, $email_subject, $email_message, $headers);
// modify status string to show result
$status = ($mail_sent==TRUE) ? "mail() function returned TRUE" :" mail() function returned FALSE";
}
}
?>
-->
<body >
<table><tr><td style ="text-align:right;" width=100%>
<b><?php echo $status; ?></b><br>
<p><span >* = required fields.<br>Please double check your email address.</span></p>
<form name="contactform" method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <span class="error">*<?php echo $nameErr;?></span>
<input type="text" name="name" value="<?php echo $name;?>"><br>
Email: <span class="error">*<?php echo $emailErr;?></span>
<input type="text" name="email" value="<?php echo $email;?>"><br>
<br>
<div align="center"> ---- <span class="error">*</span> Message ---- <span class="error"><?php echo $commentsErr;?></span><br>
<textarea name="comments" wrap="physical" cols="40" rows="10" ><?php echo $comments;?></textarea>
<br><br>
</div>
<input name="submit" id="submit" type="submit" value="Submit" >
</form>
</td></tr></table>
</body>
</html>