首页 > 解决方案 > 使用 PHPMailer 发送表单信息

问题描述

我创建了一个表单,我想使用 PHPMailer 邮寄表单数据。我能做些什么?

我的contact.html文件:

    <script src="../../Scripts/Custom/contact.js"></script>
<div class="contact-container">
    <div class="form-container">
        <p class="contactus-heading">Contact us</p>
        <div class="inner-form-container">
            <div class=" form-row "><input type="text" id="name" class="contact-form-text" placeholder="Your Name here" name="name" /></div>
            <div class="form-row"><input type="email" id="email" class="contact-form-text" placeholder="Email" name="email" /></div>
            <div class=" form-row"><input type="text" id="phone" class="contact-form-text" placeholder="Phone Number" name="phone" /></div>
            <div class="form-row"><textarea class="contact-form-text" id="body" placeholder="your message here" name="message"></textarea></div>
            <div class="form-row buttons">
                <input type="submit" onclick="sendEmail()" name="btn-send" class="send-btn contact-form-btn" value="send" />
            </div>
        </div>
    </div>
</div>

我的 Contact.js 文件:

function sendEmail() {
    var name = $('#name')[0].value;
    var email = $('#email')[0].value;
    var phone = $('#phone')[0].value;
    var body = $('#body')[0].value;
   
    if (isNotEmpty(name, email, phone, body)) {
        debugger;
        $.ajax({
            url: '../../Pages/Shared/mail.php',
            type: 'POST',
            name: name,
            email: email,
            phone: phone,
            body: body,
            success: function (response) {
                console.log(response);
            }, error: function (e) {
                console.log(e);
            }
        });
    }
}
function isNotEmpty(name, email, phone, body) {
    debugger
    if (name != "" && email != "" && phone != "" && body != "") {

        return true;
    } else {
        console.log('some fields are empty');
        return false
    }
}

我的 mail.php 文件:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

 if(isset($_POST['name']) && isset($_POST['email'])){
     $name=$_POST['name'];
     $email=$_POST['email'];
     $phone=$_POST['phone'];
     $body=$_POST['body'];

     require_once "../../Libraries/PHPMailer.php"
     require_once "../../Libraries/SMTP.php"
     require_once "../../Libraries/Exception.php"

     $mail = new PHPMailer();
     $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'mailhere';                     // SMTP username
    $mail->Password   = 'password here';                               // SMTP password
    $mail->SMTPSecure = 'TLS';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;  
    
    $mail->isHTML(true);  
    $mail->setFrom($email, $name);
    $mail->addAddress('mailhere');     // Add a recipient
    $mail->Subject = 'It is a subject';
    $mail->addReplyTo('mailhere');
    $mail->Body    = $body;

    if($mail->send())
        $response="Email is sent!";
    else
        $response= "Something went wrong :<br><br>".$mail->ErrorInfo;
    exit(json_encode(array("response" =>$response)));

 }
?>

当我在表格中填写信息时,我得到的错误是:

发送到 Web 服务器的请求使用了配置为处理请求的模块所不允许的 HTTP 谓词。\t

  • 向服务器发送了一个包含无效 HTTP 谓词的请求。
  • \t
  • 该请求是针对静态内容的,并且包含除 GET 或 HEAD 之外的 HTTP 动词。
  • \t
  • 使用 HTTP 谓词 POST 将请求发送到虚拟目录,默认文档是不支持 GET 或 HEAD 以外的 HTTP 谓词的静态文件。

  • 标签: phpjqueryhttpbackend

    解决方案


    推荐阅读