首页 > 解决方案 > Php: Email received without any messages

问题描述

I am trying to create the "contact us" form in php and link it to my html. I do receive the email, but there is no message.

<?php
$visitor_email = $_POST['email'];
$message = $_POST['message'];


$email_from = 'web@based.com';

$email_subject = "New Form Submission";

$email_body = "User Email: $visitor_email.\n".
                "User Message: $message.\n";


$to = "ur.zic@based.com";

$headers = "From: $email_from \r\n";

$headers .= "Reply-To: $visitor_email \r\n";

mail($to, $email_subject, $email_body, $headers);

header('Location: '.$_SERVER['HTTP_REFERER']);

?>


<form id="ContactForm" method="post" action="message.php">
        <h5 class="contactUsHeading">CONTACT US</h5>
        <div class="form-group">
            <input
              type="email"
              name="email"
              placeholder="youremail@greatemail.com"
              class="form-control w-75 w-100-mob"
              required
            />
         </div>
         <div class="form-group">
            <textarea
              name="message"
              class="form-control"
              rows="7"
              placeholder="Tell us about your engineering challenges and we will find a way to solve them."
              required
            ></textarea>
          </div>
          <div class="form-group">
            <input type="submit" class="btn btn-brand btn-lg w-50 w-100-mob" value="Send us your challenge" >
          </div>
</form>

If there is a problem with the code I would be happy to take any suggestions.

Best regards,

Uroš

标签: phphtml

解决方案


根据您的 PHP 版本,正确连接可能很重要

$email_body = "User Email:". $visitor_email."\r\nUser Message:". $message."\r\n";
$headers = "From: ". $email_from. "\r\n";
$headers .= "Reply-To:". $visitor_email; 
// you might not need \r\n at the end if you do not add more header

如果邮件已发送,我还会添加一个条件

if(mail($to, $email_subject, $email_body, $headers)){
  header("Location: ".$_SERVER['HTTP_REFERER']);
// careful with the mix of single quote for different things (above)
  }
else {
    die("Error with your email");
  }

推荐阅读