这个脚本有什么不对?

时间:2015-03-21 17:18:24

标签: php html forms web contact

我有一个网站,在底部,我有一个“联系我们”部分。它发送电子邮件,但当我在我的电子邮件中检查它时,它会显示在垃圾邮件中。它认为它从空电子邮件中发出空消息。这是HTML:

Apply:
                        <form action ="contact2.php" method "post">
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Name</label>
                            <input type="text" class="form-control"name ="cf_name" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Email Address</label>
                            <input type="email" class="form-control"name = 'cf_email' placeholder="Email Addres" id="email" required data-validation-required-message="Please enter your email address.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Message</label>
                            <textarea rows="5" class="form-control" name = 'cf_message' placeholder="Why should we pick you?" id="message" required data-validation-required-message="Please enter a message."></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <br>
                    <div id="success"></div>
                    <div class="row">
                        <div class="form-group col-xs-12">
                            <button type="submit" class="btn btn-success btn-lg">Send</button>
                        </div>
                    </div>
                </form>

这是PHP:

 <?php
 $field_name = $_POST['cf_name'];
 $field_email = $_POST['cf_email'];
 $field_message = $_POST['cf_message'];

 $mail_to = 'staff@goldenmistgames.com';
 $subject = 'Message from a site visitor '.$field_name;

 $body_message = 'From: '.$field_name."\n";
 $body_message .= 'E-mail: '.$field_email."\n";
 $body_message .= 'Message: '.$field_message;

 $headers = 'From: '.$field_email."\r\n";
 $headers .= 'Reply-To: '.$field_email."\r\n";

 $mail_status = mail($mail_to, $subject, $body_message, $headers);

 if ($mail_status) { ?>
 <script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'contact_page.html';
 </script>
 <?php
 }
 else { ?>
 <script language="javascript" type="text/javascript">
    alert('Message failed. Please, send an email to gordon@template-   help.com');
    window.location = 'contact_page.html';
 </script>
 <?php
 }
 ?>

1 个答案:

答案 0 :(得分:0)

问题很简单,PHP-Mail功能没有使用配置良好的SMTP服务器。

现在,电子邮件客户端和服务器对发送服务器的电子邮件执行大量检查,例如反向DNS查找,灰名单和whatevs。所有这些测试都将失败,使用php mail()函数。如果您使用动态IP,则更糟糕。

使用PHPMailer-Class并将其配置为使用smtp-auth以及配置良好的专用SMTP服务器(本地SMTP服务器或远程服务器),您的问题就会消失。

https://github.com/PHPMailer/PHPMailer