简体   繁体   English

没有 Composer 的 PHPMailer 安装

[英]PHPMailer install without Composer

Please forgive my ignorance.请原谅我的无知。 I am trying to install PHPMailer 6.0.1 under PHP 5.6 on Linux.我正在尝试在 Linux 上的 PHP 5.6 下安装 PHPMailer 6.0.1。 My PHP installation is remote and I manage all my websites' PHP via FTP (I typically download packages as .zips to Win 10, unpack and then FTP the result to my webspace).我的 PHP 安装是远程的,我通过 FTP 管理我所有网站的 PHP(我通常将软件包作为 .zip 下载到 Win 10,解压然后将结果通过 FTP 传输到我的网站空间)。

Of the various ways to install PHPMailer, Composer is preferred, but this is where I come unstuck.在安装 PHPMailer 的各种方法中,Composer 是首选,但这是我遇到麻烦的地方。 None of the Composer instructions seems appropriate to this way of working – the installer wants me to the 'Choose the command line PHP you want to use', but I don't have PHP locally ... Annoyingly, I see PHPMailer's composer.json file installed waiting to be used. Composer 的所有说明似乎都不适用于这种工作方式——安装程序希望我“选择要使用的命令行 PHP”,但我本地没有 PHP ......烦人的是,我看到了 PHPMailer 的 composer.json文件已安装,等待使用。 But no PHPMailerAutoload.php (is this created by Composer?)但是没有 PHPMailerAutoload.php (这是由 Composer 创建的吗?)

So I try to do a manual install of PHPMailer.所以我尝试手动安装 PHPMailer。 I download, unzip and FTP upload the resulting directories to my webspace in folder PHPMailer.我下载、解压缩和 FTP 将生成的目录上传到我的网站空间中的 PHPMailer 文件夹。 I then insert the following at the head of my PHP code and outside of any functions:然后,我在 PHP 代码的开头和任何函数的外部插入以下内容:

require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

With the 'use' statements I get a syntax error unexpected 'use' (T_USE) … Without them I get as far as trying to instantiate: $mail = new PHPMailer;使用 'use' 语句时,我得到了一个语法错误,即意外的 'use' (T_USE) ... 没有它们,我只能尝试实例化: $mail = new PHPMailer; but this fails with a 'class 'PHPMailer' not found但这失败了,找不到'类'PHPMailer'

What please am I doing wrong and how can I do better?请问我做错了什么,我怎样才能做得更好?

This isn't specific to PHPMailer - it's what you need to do to use any of the myriad PHP packages that use namespaces.这不是 PHPMailer 特有的——它是您使用任何使用命名空间的无数 PHP 包所需要做的。 The PHP docs on how to use use are here .关于如何使用PHP的文档use在这里

The short version is, you need to put namespace and use directives before any other scripting, so if you simply reverse the order of your commands, it should work:简短的版本是,您需要在任何其他脚本之前放置namespaceuse指令,因此如果您简单地颠倒命令的顺序,它应该可以工作:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';

Incidentally, this is the order used in the example in the PHPMailer readme and all the other examples provided with PHPMailer.顺便说一下,这是 PHPMailer 自述文件中的示例以及 PHPMailer 提供的所有其他示例中使用的顺序。 You may findthe upgrade guide useful too.您可能会发现升级指南也很有用。

The PHPMailerAutoload.php file no longer exists - composer's autoloader does a much better job. PHPMailerAutoload.php文件不再存在——作曲家的自动加载器做得更好。 PHPMailer's own composer.json file is used to resolve dependencies and flag compatibility requirements for your app's own composer file, that is, it's used to tell your project's composer file how to use PHPMailer – but is not your project's composer file itself – every package you load will have its own. PHPMailer 自己的composer.json文件用于为您的应用程序自己的 Composer 文件解决依赖项和标记兼容性要求,也就是说,它用于告诉您项目的 Composer 文件如何使用 PHPMailer – 但不是您项目的 Composer 文件本身 – 您的每个包负载将有自己的。

Developing without a local PHP instance is hard work – developing on your live server is, shall we say, "discouraged"!在没有本地 PHP 实例的情况下进行开发是一项艰巨的工作 - 在您的实时服务器上进行开发,我们可以说,“不鼓励”! If you can't install PHP directly, run it in a container or VM using Docker, VirtualBox or something like XAMPP that's completely self-contained.如果您不能直接安装 PHP,请使用 Docker、VirtualBox 或类似 XAMPP 的完全独立的东西在容器或 VM 中运行它。

In version 6.02, each of the phpmailer modules contain the namespace PHPMailer\\PHPMailer declaration so the following works (no autoloader needed but this routine should be in /src folder):在 6.02 版本中,每个 phpmailer 模块都包含命名空间PHPMailer\\PHPMailer声明,因此以下工作(不需要自动加载器,但此例程应在/src文件夹中):

include($_SERVER['DOCUMENT_ROOT'].'/path_setup.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/PHPMailer.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/SMTP.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/Exception.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);

Modify you require , and try set like the wiki of PHPMailer says:修改您的require ,并尝试像 PHPMailer 的 wiki 所说的那样设置:

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;

Link of wiki维基链接

Not passable without composer....没有作曲家就无法通过......

Warning: require(src/Exception.php): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\testtest\\test.php on line 5警告:要求(src/Exception.php):无法打开流:第 5 行 C:\\xampp\\htdocs\\testtest\\test.php 中没有这样的文件或目录

Fatal error: require(): Failed opening required 'src/Exception.php' (include_path='C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\testtest\\test.php on line 5致命错误:require(): Failed opening required 'src/Exception.php' (include_path='C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\testtest\\test.php on line 5

First create a folder src and create Exception.php,PHPMailer.php,SMTP.php Liber's then we get results首先创建一个文件夹src并创建Exception.php,PHPMailer.php,SMTP.php Liber的然后我们得到结果

<?php
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 = '@gmail.com';
$mail->Password = '';
$mail->setFrom('@gmail.com', '...');
$mail->addReplyTo('@gmail.com', ' Name');
$mail->addAddress('@gmail.com', '...');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("Hello test SMTP 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!";
}
?>*strong text*

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

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