Php:收到的电子邮件没有任何消息

时间:2019-12-02 08:43:50

标签: php html

我正在尝试在php中创建“与我们联系”表单并将其链接到我的html。我确实收到了电子邮件,但没有消息。

<?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>

如果代码有问题,我很乐意提出任何建议。

最诚挚的问候,

Uroš

2 个答案:

答案 0 :(得分:0)

取决于您的PHP版本,正确连接以及以\ r \ n进行常规连接而不是一次\ n和另一次\ r \ n

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

答案 1 :(得分:0)

从带有标题的电子邮件中进行设置。如果邮件为HTML格式,您还可以使用内容类型 text / html

$headers = "From:" . $email_from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $email_subject, $email_body, $headers);

希望这项工作