0


I am doing a mail sent function from a contact form inside a Slim 3 project.
I get the name, email and message field data from $request->getParams() and then place it to an $email_message to send the information directly to my email. And the email was not delivered. Funny thing is, without the getParams() data (I just replace it with any string), the email is delivered and i receive it. So probably the mail syntax is right and email is enabled on my hosting.
And if i echoed the message right before the mail() code, it print out the message correctly, so data from the form is received. So I could not figure out what is going wrong here.
I try to use print_r(error_get_last()) but it only returns a blank page.
Please somebody can help? Thanks a lot!
My code:

$data = $request->getParams();
$name = $data['name'];
$email_from = $data['email'];
$message = $data['message'];

$email_message = "Name: ".$name."Message: ".$message;

$email_to = "myemail@email.com"; 
$email_subject = "Message from website"; 

$headers = 'From: '.$email_from."\r\n".
           'Reply-To: '.$email_from."\r\n" .
           'X-Mailer: PHP/' . phpversion();
$send = mail($email_to, $email_subject, $email_message, $headers);
if($send)
    { 
       echo 'Sent!';
    } else { 
       print_r(error_get_last());
    }
die();

UPDATED: now it prints out 'Sent!', and still i don't get any email to my email address.
For someone that mark this as duplicate, i also read that answer but not successful. This issue is different because it DID send email successully without data passed from the contact form, it only gets problem when there is data from getParams() added. So i hope somebody who is experience in Slim or PHP can help. Please don't just mark as duplicate if you don't really understand the question.

Anh K
  • 96
  • 5
  • 2
    What's in the PHP error log? Likely your print_r never executes because the script has already crashed. – Matt Mar 31 '17 at 14:10
  • Hi, error logs did not report anything when i submit the form. The only errors display is several hours ago on another issue. – Anh K Mar 31 '17 at 14:17

1 Answers1

1

99.9% of people trying to mail with PHP giving up.

PHP only does half of the work for you, it formats the outgoing email, however it does not do the actual sending. What does? Well it depends on the OS you're using, and even then, it depends on what mail server you want to use.

I've only set this environment up for Debian/Linux. I'd install postfix (apt install postfix), configure it, and start the process. Only then does the mail function work. HOWEVER it still maybe rejected from some email servers (ie, gmail) because it's unencrypted.

Best of luck.

Dellowar
  • 3,160
  • 1
  • 18
  • 37