简体   繁体   English

在没有 Composer 的情况下添加 PHPMailer

[英]Adding PHPMailer Without Composer

I have this contact form but I am confused as to how I can insert PHPMailer (without Composer) into the script?我有这个联系表格,但我对如何将 PHPMailer(没有 Composer)插入脚本感到困惑?

I am not sure how to properly add it so that way, once it processes and sends the form it alerts the user.我不确定如何正确添加它,一旦它处理并发送表单,它就会提醒用户。 I do not have the ability to utilize composer, so I would need to upload PHPMailer into the directory.我没有使用作曲家的能力,所以我需要将 PHPMailer 上传到目录中。

<?php
function validateRecaptcha($secret, $clientResponse, $clientIp)
{
    $data = http_build_query([
        "secret"   => $secret,
        "response" => $clientResponse,
        "remoteip" => $clientIp,
    ]);

    $options = [
        "http" => [
            "header" =>
                "Content-Type: application/x-www-form-urlencoded\r\n".
                "Content-Length: ".strlen($data)."\r\n",
            "method"  => "POST",
            "content" => $data,
        ],
    ];

    $response = file_get_contents(
        "https://www.google.com/recaptcha/api/siteverify",
        false,
        stream_context_create($options)
    );

    if($response === false)
    {
        return false;
    }
    else if(($arr = json_decode($response, true)) === null)
    {
        return false;
    }
    else
    {
        return $arr["success"];
    }
}

$errors         = array();      // array to hold validation errors
$data             = array();         // array to pass back data

// validate the variables ======================================================
    // if any of these variables don't exist, add an error to our $errors array

    if (empty($_POST['firstName']))
        $errors['firstName'] = 'First Name is required.';

    if (empty($_POST['lastName']))
        $errors['lastName'] = 'Last Name is required.';

    if (empty($_POST['companyName']))
        $errors['companyName'] = 'Company Name is required.';

    if (empty($_POST['companyAddress']))
        $errors['companyAddress'] = 'Company Address is required.';

    if (empty($_POST['city']))
        $errors['city'] = 'City is required.';

    if (empty($_POST['state']))
        $errors['state'] = 'State is required.';

    if (empty($_POST['emailAddress']))
        $errors['emailAddress'] = 'Email Address is required.';

    if (empty($_POST['comment']))
        $errors['comment'] = 'Comment is required.';

    if (empty($_POST['g-recaptcha-response']))
        $errors['captcha'] = 'Captcha is required.';


// return a response ===========================================================

    // if there are any errors in our errors array, return a success boolean of false

    if(!validateRecaptcha($secret, $_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"]))
    {
        $errors['recaptcha'] = 'Captcha is required.';
    }

    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    } else {

        // if there are no errors process our form, then return a message

        // DO ALL YOUR FORM PROCESSING HERE
        // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)



        // show a message of success and provide a true success variable
        $data['success'] = true;
        $data['message'] = 'Success!';
    }

    // return all our data to an AJAX call
    echo json_encode($data);

Without autoloader:没有自动加载器:

<?php
//You shall use the following exact namespaces no 
//matter in whathever directory you upload your 
//phpmailer files.

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

//Now include the following following files based 
//on the correct file path. Third file is required only if you want to enable SMTP.

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
?>

You shall add the following class to initiate the mailer after checking if your query or condition is executed.在检查您的查询或条件是否被执行后,您应该添加以下类来启动邮件程序。

<?php
$mail = new PHPMailer(true); 
?>

You shall find a nice and simple example at https://github.com/PHPMailer/PHPMailer/blob/master/README.md to start with.您将在https://github.com/PHPMailer/PHPMailer/blob/master/README.md上找到一个不错的简单示例。

I hope it helps.我希望它有帮助。

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

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