发送的PHP电子邮件有两个空白字段

时间:2018-06-09 10:14:28

标签: php html email

我是PHP的新手。我有一个简单的联系表格。我在线获取代码,复制/粘贴/调整。它会发送电子邮件,但电子邮件中有两个字段(电子邮件和州)是空白的。所有其他领域都已填写。我看了,我没有看到任何理由。我错过了什么?在此先感谢您的帮助。这是我的代码:

HTML

<form id="contactusForm" method="post" action="connect.php">
      <div><blockquote>Please fill out this short form and we will respond as soon as possible. Nothing is required except your reason for contact and your message. Thank you for your time.</blockquote>
      <br><br><br>
</div> 
<div class="row">
      <div class="col-25" id="help-with">

        <label for="reason">Reason</label>
    </div><div class="col-75">
     <select name="reason" id="reason" type="select" tabindex="1">
        <option value="Prayer">Request For Prayer</option>
          <option value="Information">More Information</option>
          </select></div> </div>  <br>
    <br>
      <div class="row">
      <div class="col-25">
        <label for="name">Name</label>
      </div>
      <div class="col-75">
        <input name="name" type="text" id="name" placeholder="Your name.." tabindex="2" title="Name" size="50" maxlength="50">
      </div>
    </div>

    <div class="row">
    <div class="col-25">
      <label for="phone">Phone</label>
      </div>
      <div class="col-75">
        <input name="phone" type="tel" id="phone" placeholder="Your phone number.." tabindex="3" title="Phone" size="30" maxlength="30">
      </div>
      </div>    
       <div class="row">
        <div class="col-25">
        <label for="email">Email</label>
      </div>
      <div class="col-75">
        <input name="email" type="email" id="email" placeholder="Your email..." tabindex="4" title="Email" size="50" maxlength="50" >
      </div>
      </div>

     <div class="row">
     <div class="col-25">
        <label for="state">State</label>
      </div>
         <div class="col-75">
        <select name="state" id="state" type="select" tabindex="5">
            <option value="pick-a-state">Select your state...</option>
       <option value="NA">N/A</option>
            <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
            </select>
      </div>
    </div>

      <div class="row">
     <div class="col-25">
        <label for="country">Country</label>
      </div>
      <div class="col-75">
         <select name="country" id="country" type="select" tabindex="6">
            <option value="">Select your country...</option>
         <option value="United States">United States</option> 
    <option value="United Kingdom">United Kingdom</option> 
    <option value="Afghanistan">Afghanistan</option> 
        </select>
      </div>
    </div>
    <div class="row">
      <div class="col-25">
        <label for="message">Your Message:</label>
      </div>
      <div class="col-75">
        <textarea name="message" type="text" maxlength="800" required="required" id="message" style="height:180px" placeholder="Type your message here..." tabindex="7"></textarea>
      </div>
    </div><br>
    <br>
      <input type="checkbox" name="contact_me_by_fax_only" value="1" style="display:none !important" tabindex="-1" autocomplete="off">
    <div class="row">
      <input type="submit" tabindex="10" value="Submit">
    </div>
  </form>

PHP

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;


require 'PHP/PHPMailer/vendor/autoload.php';

    $reason = $_POST['reason'];
    $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    $phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
    $mailFrom = $_POST['email'];
    $state = $_POST['state'];
    $country = $_POST['country'];
    $message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

    //validation
    if(empty($message)){
        header("location: connect.html?nomessage");
    }

$honeypot = FALSE;
if (!empty($_REQUEST['contact_me_by_fax_only']) && (bool) $_REQUEST['contact_me_by_fax_only'] == TRUE) {
    $honeypot = TRUE;
    log_spambot($_REQUEST);
    # treat as spambot
} else {
    # process as normal

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.dotster.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'test@missionlighthouse.org';                 // SMTP username
    $mail->Password = '1234567';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('test@missionlighthouse.org', 'MLC Website');
    $mail->addAddress('myemail80@myhost.net', 'John Doe');     // Add a recipient

    //Body Content
    $body = "<p><strong>Hello</strong>, you have received a message submitted from the MLC Website.<br><br>

    <br><strong>Message Topic:</strong><br>" . $reason.
    "<br><br><strong>Name:</strong><br>" . $name.
    "<br><br><strong>Phone:</strong><br>" . $phone.
    "<br><br><strong>Email:</strong><br>" . $email. 
    "<br><br><strong>State:</strong><br>" . $state. 
    "<br><br><strong>Country:</strong><br>" . $country.
    "<br><br><strong>Message:</strong><br>". $message."</p>";



    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Email from MLC Website';
    $mail->Body    = $body;
    $mail->AltBody = strip_tags ($body);

    $mail->send();

     header("Location: thankyou.html?sent");

} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}

1 个答案:

答案 0 :(得分:0)

如果您将变量$ mailFrom更改为$ email,我相信您的电子邮件字段会按预期显示。至于状态字段,我不确定...尝试将变量$ state的位置更改为其他名称,例如$ stateField