PHPMailer install without Composer

不问归期 提交于 2019-12-07 08:23:57

问题


Please forgive my ignorance. I am trying to install PHPMailer 6.0.1 under PHP 5.6 on Linux. 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).

Of the various ways to install PHPMailer, Composer is preferred, but this is where I come unstuck. 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. But no PHPMailerAutoload.php (is this created by Composer?)

So I try to do a manual install of PHPMailer. I download, unzip and FTP upload the resulting directories to my webspace in folder PHPMailer. I then insert the following at the head of my PHP code and outside of any functions:

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; but this fails with a ‘class 'PHPMailer' not found

What please am I doing wrong and how can I do better?


回答1:


This isn't specific to PHPMailer - it's just the things you need to deal with for any of the myriad PHP packages that use namespaces. The PHP docs on how to use use are here.

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:

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 example in the readme and all the other examples provided with PHPMailer. You may find the upgrade guide useful too.

The PHPMailerAutoload.php file no longer exists - composer's autoloader does a much better job. 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.

Developing without a local PHP instance is hard work - developing on your live server is, shall we say, "discouraged"! If you can't install PHP directly, run it in a VM using VirtualBox or something like XAMPP that's completely self-contained.




回答2:


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):

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);



回答3:


Modify you require, and try set like the wiki of PHPMailer says:

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

Link of wiki



来源:https://stackoverflow.com/questions/46247149/phpmailer-install-without-composer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!