如何在没有作曲家的情况下使用PHPMailer? [英] How to use PHPMailer without composer?

查看:64
本文介绍了如何在没有作曲家的情况下使用PHPMailer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将最新的 PHPMailer 库与require_once()一起使用,而不是与Composer混为一谈.我想要一个最小化的纯xcopy部署.

I'd like to use the latest PHPMailer library with require_once() instead of messing around with Composer. I'd like a pure xcopy deployment with minimal fuss.

这是我正在尝试做的事情:

Here's what I'm attempting to do:

require_once("src/PHPMailer.php");
$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); 
$mail->AltBody = 'HTML messaging not supported';

if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

我收到错误消息:Fatal error: Class PHPMailer not found in [....]\EmailTester.php on line 21

第21行是:$mail = new PHPMailer;

这行只是我的一个猜测:require_once("src/PHPMailer.php");-显然我需要包含一个或多个文件,但是我不知道是哪个.

This line is just a guess on my part: require_once("src/PHPMailer.php"); - clearly I need to include some file or files, but I can't tell which.

我正在从github上的 gmail示例工作 zip下载中也不包含该文件.但是我可以在github中导航到它.在该示例文件中,它的开始是这样的:

I'm working from the gmail example on github which is also not included in the zip download. But I can navigate to it in github. In that example file it begins like this:

use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$mail = new PHPMailer;

我在zip下载中看不到autoload.php文件,并且在全部搜索之后,我发现这意味着使用Composer.但是必须有某种方法可以简单地进行包含并获取我需要的文件.

I see no autoload.php file in the zip download, and after googling all over I see this implies using Composer. But there must be some way to simply do an include and get the files I need.

关于此PHPMailer库,也许一般来说 github ,有些事情使我感到困惑:

A few things puzzle me about this PHPMailer library and perhaps github in general:

  1. 当我从GitHub下载PHP Mailer时,为什么下载的zip文件中没有包含这么多列出的文件和文件夹?

  1. 为什么他们引用zip下载中不存在的autoload.php?
  2. 很明显,我不了解github的某些知识,但是为什么不提供一个有效的代码示例,而不是引用下载中不存在的依赖项,迫使人们在其他地方找到它,并希望他们能弄清楚如何实现返回并正确插入?
  3. 在此YouTube视频中,标题为使用PHP和Gmail ,他从同一地点下载了与我下载的相同的zip,但他的zip包含了不同的文件,包括PHPMailerAutoload.php.为什么我得到的文件与他得到的文件完全不同?该视频于2017年3月4日发布-不到一年前-自那时以来真的发生了太大变化吗?
  1. Why do they reference autoload.php which doesn't exist in the zip download?
  2. Clearly I don't understand some things about github, but why not provide a working code sample instead of referencing dependencies that don't exist in the download, forcing people to find it elsewhere and hope they can figure out how to come back and plug it in correctly?
  3. In this YouTube video titled Send Emails with PHP & Gmail, he downloads the same zip I downloaded and from the same place, yet his zip contains different files, including PHPMailerAutoload.php. Why am I getting completely different files than he gets? That video was published March 4, 2017 -- so, less than 1 year ago -- has it really changed so much since then?

总结:如何在没有外部依赖项和诸如 Composer 之类的安装的情况下如何使PHPMailer正常工作,而是使用require_once()来获取我需要的东西?

In summary: How can I get PHPMailer working without external dependencies and installations such as Composer, and instead use require_once() to get what I need?

推荐答案

下面是完整的示例(尽管您看到了一些必须定义和设置的变量):

Here's the full working example (though you see a few variables that must be defined and set):

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

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported';
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file

if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

这篇关于如何在没有作曲家的情况下使用PHPMailer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆