联系PHP表单 - 不接收邮件(GMAIL)

时间:2016-04-28 09:44:49

标签: php forms twitter-bootstrap email contact

我一直试图让我的联系表格正常工作,但我没有收到我的Gmail邮箱中的任何电子邮件(包括垃圾邮件和垃圾邮件。也无法通过我的主机跟踪任何内容)。

我一直在关注本教程:http://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form

因为我已经尝试了所有东西,需要一些认真的帮助!

<?php


$from = $_POST['email'];
$sendTo = 'myemail@gmail.com';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'email' =>     'Email',  'phone' => 'Phone', 'message' => 'Message');

 // array variable name => Text to appear in email

$okMessage = 'Contact form succesfully submitted. Thank you, I will get back     to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

// let's do the sending

try
{
$emailText = "You have new message from contact     form\n=============================\n";

foreach ($_POST as $key => $value) {

    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $value\n";
    }
 }

  mail($sendTo, $subject, $emailText, "From: " . $from);

  $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&     strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
else {
echo $responseArray['message'];
}

HTML表格

<form id="contact-form" method="post" action="contact.php" role="form">

<div class="messages"></div>

  <div class="form-group">
      <div class="row">
          <div class="col-md-6 col-sm-12">
              <label>First name*</label>
              <input type="text" id="form-name" name="name" class="form-     control" placeholder="Please enter your firstname *" required="required">
          </div>
          <div class="col-md-6 col-sm-12">
              <label>Last name*</label>
              <input type="text" name="surname" id="form-surname"     class="form-control" placeholder="Please enter your firstname *"     required="required">
          </div>
      </div>
  </div>
    <div class="form-group">
        <div class="row">
            <div class="col-md-6 col-sm-12">
                <label>Email*</label>
                <input type="email" name="email" id="form-email"  class="form-control" placeholder="Please enter your firstname *"  required="required">
            </div>
            <div class="col-md-6 col-sm-12">
                <label>Phone</label>
                <input type="tel" name="phone" id="form-phone" class="form- control" placeholder="Please enter your phone">
            </div>
        </div>
    </div>


    <div class="form-group">
      <label for="comment">Message*</label>
      <textarea class="form-control" rows="7" name="message" id="form-message" default="Type us a message" required="required"></textarea>
    </div>    

    <div class="checkbox">
      <label><input type="checkbox" value="">Join mailing list</label>
    </div>  
    <input type="submit" class="btm btn-success btn-send" value="Send message">
    <p class="text-muted"><strong>*</strong> These fields are required.</p>
 </form>

2 个答案:

答案 0 :(得分:3)

由于许多服务器不允许您以未注册的人的名义发送电子邮件,因此您应该在服务器上创建一封电子邮件,仅用于向您发送用户编写的电子邮件他们填写联系表单,然后您应该使用PHPMailer之类的课程为您发送电子邮件。

一个显示如何使用PHPMailer的简单示例是:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.yourserver.here';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@userserver.here';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('user@userserver.here', 'Mailer');
$mail->addAddress('your@email.here', 'You');     // Add a recipient
$mail->addReplyTo('theuser@email.here', 'User that submitted form');

$mail->Subject = 'Subject here';
$mail->Body    = 'Write here your message';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

该示例取自我上面告诉您的链接。

答案 1 :(得分:1)

您没有邮件传递错误?

尝试发送来自知道主机的电子邮件:

$from = $_POST['email']; (like youremail@gmail.com)

Gmail会将DNS服务器中的DKIM(发布者)和SPF(签名者)记录检查到电子邮件主机。如果电子邮件主持人没有DKIM记录,则可能无法提供电子邮件。

更多(gmail):

https://support.google.com/mail/answer/180707?hl=en

更多(如何安装和配置DKIM):

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-dkim-with-postfix-on-debian-wheezy