PHP sendmail,值不通过电子邮件

时间:2016-07-06 11:32:53

标签: php 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
?>

1 个答案:

答案 0 :(得分:1)

评论时间过长,因此我提交了以下内容。

您的代码中有很多错误。

你错过了最后一个标题的连接/点,并且“打破链条”,并且变量不会用单引号解析

第一个标题也不应该有一个前导点。

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

查阅邮件手册:

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

如评论中所述:

  

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

由于您没有处理文件,因此可以安全删除。

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