0

I am having a strange problem. The following code segment prints Send and the email is successfully delivered to my inbox

$from_name ="John Doe";
$from_mail = "someemail@gmail.com";
$to = "someemail@gmail.com";
$subject = "Test Mail";
$mail_body = "This is email test";
$message = $mail_body ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8\r\n";
$headers .= "From: ".$from_name." <".$from_mail.">\r\n";

$sendmail=mail($to,$subject,$message,$headers);

if($sendmail)
{
   echo json_encode("Send");
}
else
{
   echo json_encode("Not Send");
}

Now the next segment I am about to show is nearly the same, but I get the inputs from POST.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $to_email  = "someemail@gmail.com";
    $name      = $_POST["name"];
    $email     = $_POST["email"];
    $message   = $_POST["message"];

    $subject = "Contact from website";


    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8\r\n";
    $headers .= "From: ".$name." <".$email.">\r\n";

    $sendmail = mail($to_email, $subject, $message, $headers);

    if($sendmail)
    {
        echo json_encode("Send");
    }
    else
    {
        echo json_encode("Not Send");
    }
}

Now the above also prints Send, but no email is actually delivered, not even to my Spam folder. I have dumped all the POST variables and they all contain the data I expect them to. If I check it out in the Net tab of firebug, I can also see that the data is posted

email   someemail@gmail.com 
message dsfsdfsdfsdf 
name    John Doe

I have checked the server logs and there are no errors. $to_email is set to my email address.

Is there anything that stands out as to why the first one sends and the second one does not, even though they both produce the output Send?

Thanks

katie hudson
  • 2,765
  • 13
  • 50
  • 93
  • Avoid using the built in mail wrapper and switch to SMTP. –  Aug 03 '15 at 18:31
  • something like php mailer? – katie hudson Aug 03 '15 at 18:34
  • In your second test, how long is `$message`? Does it contain line breaks? – Mr. Llama Aug 03 '15 at 18:37
  • It would contain line breaks. Its a text area. – katie hudson Aug 03 '15 at 18:45
  • Yes, PHPMailer has support for SMTP. Most mail servers have filters which automatically reject anything that was not received through SMTP because sendmail and similar direct delivery options are extremely prone to abuse. GMail servers will for instance allow you to send it one or two mails with the `mail` function and after that they will block your IP for an unspecified amount of time or until you use SMTP. Just to prove if this is the case try running your first code right after the second one. If your emails do not reach their destination you know you have been blocked. –  Aug 03 '15 at 18:46

0 Answers0