繁体   English   中英

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

[英]Sent PHP Email has two blank fields

我是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;
}
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM