PHP sendmail,值不传递到电子邮件


PHP sendmail, values not passing through to email

我在 html 页面上有一个联系表单,该表单在单独的 php 页面上使用 php 脚本发送输入到电子邮件输入框中的文本值

除了在表单输入框中输入的文本没有通过,它们是空白的之外,一切都在工作

以下是我正在做的事情:

.HTML

    <form class="form-inline" action="mail_handler.php" method="post"   
     enctype="multipart/form-data">
                <div class="fullwidth">
                  <div class="left-feild col-md-6">
                    <div class="form-group">
                      <input type="text" class="form-control color01 background13 border-color08" name="name" placeholder="Name" required>
                    </div>
                    <div class="form-group">
                      <input type="email" class="form-control color01 background13 border-color08" name="email" placeholder="Email" required>
                    </div>
                    <div class="form-group">
                      <input type="text" class="form-control color01 background13 border-color08" name="number" placeholder="Phone/Cellphone Number">
                    </div>
                    <div class="form-group">
                      <input type="text" class="form-control color01 background13 border-color08" name="company" placeholder="Company name">
                    </div>
                  </div>
                  <div class="right-feild col-md-6">
                    <div class="form-group fullwidth">
                      <textarea class="form-control color01 background13 border-color08" rows="3" placeholder="type your message here" name="message" required></textarea>
                    </div>
                  </div>
                </div>
                <div class="submitbutton fullwidth">
                  <button type="submit" value="submit" name="submit" class="btn more background07 color01 color01-hover01" >book</button>
                </div>
              </form>

.PHP

<?php 
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
$from = 'From: info@recipient.com'; 
$to = 'myemail@gmail.com'; //email
$subject = 'website booking enquiry';
$message = "From: $name'n Number: $number'n E-Mail: $email'n Company:    
$company'n Message: $message'n ";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-type: text/html'r'n";
$headers = 'From: info@recipient.com' . "'r'n" .
'Reply-To: $email' . "'r'n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header( "Location: index.html" );//if u wish to get redirected
?>

这太长了,无法发表评论,因此我提交了以下内容。

代码中有很多错误。

您错过了最后一个标头的连接/点,并且正在"打破链条",并且变量不会在单引号中解析

此外,第一个标题不应有前导点。

$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
$from = 'From: info@recipient.com'; 
$to = 'myemail@gmail.com'; //email
$subject = 'website booking enquiry';
$message = "From: $name'n Number: $number'n E-Mail: $email'n Company:    
$company'n Message: $message'n ";
$headers  = "MIME-Version: 1.0'r'n";
$headers .= "Content-type: text/html'r'n";
$headers .= 'From: info@recipient.com' . "'r'n" .
"Reply-To: $email" . "'r'n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header( "Location: index.html" );//if u wish to get redirected
exit;

您还应该检查空字段,因为任何人都可以提交任何内容,您将获得空结果。

即:

if(!empty($_POST['name']) || !empty($_POST['email'])) {
// Assign variables to the POST arrays and process mail
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
// ... rest of your code
}

注意:||表示"或"。您可以使用它,或者&&表示"AND",或两者的混合。


脚注:

'n不会在单独的行上显示数据,如果这是目标,因为您使用的是 HTML 标头。

如果这是您要在此处执行的操作,则需要使用<br>

请参阅邮件手册:

  • http://php.net/manual/en/function.mail.php

PHP mail()的其他一些替代品是PHPMailer和Swiftmailer:

  • https://github.com/PHPMailer/PHPMailer
  • http://swiftmailer.org/

如评论中所述:

enctype="multipart/form-data" - 从表单元素中删除它。

可以安全地删除它,因为您不处理文件。

如果这是服务器问题,那么您将需要找出问题所在以及超出问题范围的问题。