php 如何在没有作曲家的情况下使用 PHPMailer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48128618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use PHPMailer without composer?
提问by HerrimanCoder
I'd like to use the latest PHPMailerlibrary with require_once()
instead of messing around with Composer. I'd like a pure xcopy deployment with minimal fuss.
我想使用最新的PHPMailer库,require_once()
而不是使用 Composer。我想要一个没有大惊小怪的纯 xcopy 部署。
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!";
}
I get the error message: Fatal error: Class PHPMailer not found in [....]\EmailTester.php on line 21
我收到错误消息: Fatal error: Class PHPMailer not found in [....]\EmailTester.php on line 21
Line 21 is this: $mail = new PHPMailer;
第 21 行是这样的: $mail = new PHPMailer;
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.
这行只是我的猜测:require_once("src/PHPMailer.php");
- 显然我需要包含一些文件或文件,但我不知道是哪个。
I'm working from the gmail example on githubwhich is also not included in the zip download. But I can navigate to it in github. In that example file it begins like this:
我正在使用 github 上的gmail 示例,该示例也不包含在 zip 下载中。但是我可以在 github 中导航到它。在那个示例文件中,它是这样开始的:
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$mail = new PHPMailer;
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.
我autoload.php
在 zip 下载中没有看到任何文件,在谷歌搜索之后我发现这意味着使用 Composer。但是必须有某种方法可以简单地执行包含并获取我需要的文件。
A few things puzzle me about this PHPMailer library and perhaps githubin general:
关于这个 PHPMailer 库和github 的一些事情让我感到困惑:
- When I download PHP Mailer from GitHub, why are so many listed files and folders not included in the downloaded zip file?
- 当我从 GitHub 下载 PHP Mailer 时,为什么下载的 zip 文件中没有包含这么多列出的文件和文件夹?
- Why do they reference
autoload.php
which doesn't exist in the zip download? - 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?
- 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?
- 为什么他们引用
autoload.php
zip 下载中不存在的内容? - 显然我不了解 github 的一些事情,但为什么不提供一个工作代码示例而不是引用下载中不存在的依赖项,迫使人们在别处找到它并希望他们能弄清楚如何回来并插入它正确吗?
- 在这个名为用 PHP 和 Gmail 发送电子邮件的YouTube 视频中,他从同一个地方下载了我下载的相同 zip,但他的 zip 包含不同的文件,包括
PHPMailerAutoload.php
. 为什么我得到的文件与他得到的文件完全不同?该视频于 2017 年 3 月 4 日发布 - 所以,不到一年前 - 从那时起它真的发生了很大变化吗?
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?
总结:如何让 PHPMailer 在没有外部依赖项和安装(例如Composer )的情况下工作,而是require_once()
用来获取我需要的东西?
回答by HerrimanCoder
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!";
}
回答by YakovGdl35
This worked for me, I also downloaded phpmailer from this address
这对我有用,我也从这个地址下载了 phpmailer
https://sourceforge.net/projects/phpmailer/
https://sourceforge.net/projects/phpmailer/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require '/source/PHPMailer2/src/Exception.php';
require '/source/PHPMailer2/src/PHPMailer.php';
require '/source/PHPMailer2/src/SMTP.php';