联系表格以将电子邮件发送给特定的人

时间:2019-02-24 12:39:59

标签: php forms email

我在教堂网站上有一个简单的联系表。它将通过电子邮件发送到我在代码中输入的地址。但是,这就是我要添加的内容。我希望访客能够输入联系原因(信息或祷告请求)和消息。但是,如果他们想联系特定的工作人员(从下拉列表中选择),他们可以并且不发送到信息/祷告电子邮件,而是发送给该工作人员的地址。现在,我有了工作人员下拉列表,但是一般情况下,电子邮件仅到达表单的指定地址,而不是选择时到达工作人员的地址。这有道理吗?

换句话说,有人会收到要求信息的电子邮件。或祈祷,工作人员将收到直接发送给他们的电子邮件。那可能吗?预先感谢您的输入。

这是我的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.</blockquote>

    </div> 


    <label for="reason">Reason</label>
    </div><div class="col-75">
 <select name="reason" id="reason" type="select" tabindex="1">
      <option value="select">Select One</option>
     <option value="prayer@mlc.org">Request For Prayer</option>
     <option value="info@mlc.org">Request For Information</option>
     <option value="john@mlc.org">Contact Pastor John</option>
          <option value="dennis@mlc.org">Contact Assistant Pastor Dennis </option>
          <option value="bryan@mlc.org">Contact Assistant Pastor Bryan</option>
          <option value="kellie@mlc.org">Contact Kid's Pastor Kellie</option>
          </select></div> </div><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="3" 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="4" 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="5" 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="6">
                <option value="">Select your state...</option>
                <option value="NA">N/A</option>
                <option value="AL">Alabama</option>
                <option value="Alaska">Alaska</option>
                <option value="Arizona">Arizona</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="7">
                <option value="">Select your country...</option>
                <option value="United States">United States</option> 
                <option value="United Kingdom">United Kingdom</option> 
            </select>
        </div>
    </div>
    <br><br>   
    <p>If you would like to email a specific staff member, please select one below.</p>
    <br><br>
    <div class="row">
        <div class="col-25" id="selectStaff">
            <label for="emailTo">Staff Member</label>
        </div>
        <div class="col-75">
            <select name="emailTo" id="emailTo" type="select" tabindex="1">
                <option value="select">Select One</option>
                <option value="john@mlc.org">Pastor John</option>
                <option value="dennis@mlc.org">Assistant Pastor Dennis</option>
                <option value="bryan@mlc.org">Assistant Pastor Bryan</option>
                <option value="dennis@missionlighthouse.org">Kid's Pastor Kellie</option>
            </select>
        </div> 
    </div>
    <br><br>
    <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="8"></textarea>
        </div>
    </div><br><br>
        <input type="checkbox" name="contact_me_by_email_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);
$email = $_POST['email'];
$state = $_POST['state'];
$country = $_POST['country'];
$emailto = $_POST['emailTo'];
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

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

$honeypot = FALSE;
if (!empty($_REQUEST['contact_us_by_email_only']) && (bool) $_REQUEST['contact_us_by_email_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.domain.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'test@mlc.org';                 // SMTP username
        $mail->Password = 'Password';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        //Recipients
        $mail->setFrom('test@mlc.org', 'MLC Website');
        $mail->addAddress('noone@nowhere.net', 'John G.');
          $mail->addAddress('someone@gmail.com', 'Someone');
        // 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;
    }
}

0 个答案:

没有答案