0

I have a PHP mail form that is not sending mail. The script runs as it should for the user; takes them to the "Thank you!" page, but mail never comes to the email address it was supposed to go to. How can I go about fixing this issue? I have tried to solve it via other threads here on stackoverflow, but my issue is unique.

PHP

<?php
/* Set e-mail recipient */
$myemail = "the_email@gmail.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "

Name: $name
E-mail: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: http://www.thewebsite.com/thankyou.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

1 Answers1

1

The function mail returns a boolean. As documentation says, ...

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

So, ... store mail returned value to check if something went wrong.

$mailSent = mail(...);
if (!$mailSent) {
    echo "Something went wrong";
}

And if you are using windows, ... you have to know that

when PHP is talking to a SMTP server directly, if a full stop is found on the start of a line, it is removed. To counter-act this, replace these occurrences with a double dot.

<?php
$text = str_replace("\n.", "\n..", $text);
?>

Another final check to do is, ... have you smtp server installed in the machine that runs the script? If not install one. Depending on which OS, or *AMP like system you have, may be possible that something is missing: smtp could be abstent. It is possibile that this code works in production but not in your personal computer.

sensorario
  • 20,262
  • 30
  • 97
  • 159
  • Thank you for the information, my SMTP server is working on my machine from how I was instructed to check anyway. I'm using OS X 10.6.8 for the server machine; old, I know... but it usually gets the job done. @sensorario – James Hammond Sep 05 '17 at 03:49
  • I would strongly urge you not too use PHP's mail() method - it is not secure or safe to use. See here for more: https://blog.ripstech.com/2017/why-mail-is-dangerous-in-php/ – slothluvchunk Sep 05 '17 at 04:11