0

I have problem with send email. When I send a form, I get a blank message in my inbox. It looks like this:

Mail:
New message from:
E-mail:
Topic:
Sent message:

The form has validation, it works correctly. The problem is that I don't know why messages come empty to my inbox.

Here is a php code:

$name = $_POST['name'];
$mail = $_POST['mail'];
$topic = $_POST['topic'];
$message = $_POST['message'];

$email_to = "example@qmail.com";

$email_subject = "You received a new message";

$email_body = "Mail: \r\n";
$email_body .= "New message from: " .  $name . "\r\n";
$email_body .= "E-mail:"  .  $mail . "\r\n";  
$email_body .= "Topic: " . $topic . "\r\n";
$email_body .= "Sent message: " . $message . "\r\n";


$success = mail($email_to, $email_subject, $email_body);

if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=success-form\index.html\">";
}
else{
  print "<script>console.log('error'); </script>";
}

?>

Form in HTML:

 <form method="post" action="../form.php" >
        <input name="name" type="text" placeholder="Name and Surname">
        <label></label>
        <input name="mail" type="email" placeholder="Email address">
        <label></label>
        <input name="topic" type="text" placeholder="Topic">
        <label></label>
        <textarea name="message" placeholder="Message..."></textarea>
        <label></label>
       <input type="submit" class="send-btn" value="Send">
 </form>
  • You get `$success == true` as well? – Colin Cline Dec 05 '17 at 18:33
  • Try echo-ing the $name, $mail, etc. fields. I think the issue is with the form and not the php code. – lakshman.pasala Dec 05 '17 at 18:48
  • is there no validation code at php side ? after you receive the inputs and send them in email? try adding `echo $email_body` and `return` before the `mail` function and check if you are getting the desired values. – Muhammad Omer Aslam Dec 05 '17 at 18:49
  • Try `var_dump()` or as ppl in above said `echo()` all variables and see what is going on and form will sent data or not. – Colin Cline Dec 05 '17 at 18:51
  • Did you try to print out what is included in $_POST array after the submit? I.e. `var_dump($_POST); exit()` . You can also check if character encoding is same in both form and script. – Grzegorz Krauze Dec 05 '17 at 18:51

1 Answers1

0

Have you tried adding a few headers? For example:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "FROM:".$email_from."\r\n"; // Variable to define

Then, call you mail command like this:

$success = mail($email_to, $email_subject, $email_body, $headers);
Bernz
  • 171
  • 3