发送表单后邮箱中的空消息

时间:2017-12-05 18:29:55

标签: javascript php jquery html5

发送电子邮件时遇到问题。当我发送表单时,我的收件箱中会收到一条空白邮件。它看起来像这样:

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

表单已经过验证,它可以正常工作。问题是我不知道为什么邮件会在我的收件箱中空出来。

这是一个PHP代码:     

$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>";
}

?>

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>

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加几个标题?例如:

$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

然后,像这样叫mail命令:

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