I installed PHPMailer 6.01 and added the following lines to the PHP script,
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
The page loading in browser ends up with the error message:
HTTP ERROR 500 This page is not working
Should I set any path somewhere?
Solved the problem by setting the full path autoload.php.
This is how I solved such problem.
Code
//Load Composer's autoloader
require dirname(__FILE__).'/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
I hope this can help you too.
Related
This question already has answers here:
Class Firebase\JWT\JWT not found
(5 answers)
Closed last year.
I feel like I am almost there. (I could be wrong though)
from the root of my apache server (/usr/local/var/www)
I ran
composer require phpmailer/phpmailer
I've now got the folder system
/usr/local/var/www/vendor/phpmailer/phpmailer
In this folder, there is a src folder, and in that folder are basically all the files I think I need to include on the php page where I want to use PHPmailer.
On the PHP page where I want to use PHPMailer, I have at the top:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
and in the page I am calling
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions
I am getting an error message
Error: Class "PHPMailer\PHPMailer\PHPMailer" not found in /usr/local/var/www/xxx.php
It seems like something isn't linking up correctly.
Alex Howansky posted a comment:
You need to source composer's autoloader, typically via something like require_once 'vendor/autoload.php';.
Ken Lee a comment:
Add the three lines: require './Exception.php'; require './PHPMailer.php'; require './SMTP.php'; after the line use PHPMailer\PHPMailer\Exception; (change to the actual path for the 3 files please) , retry and see what happens
Both suggestions fixed the problem. I'm going with the vendor/autoload.php solution.
I have a problem with setting up PHPMailer. It was working before, but now all of a sudden it stopped and this is the error I'm getting:
PHP Fatal error: require(): Failed opening required '../src/PHPMailer.php' (include_path='.:/opt/cpanel/ea-php53/root/usr/share/pear:/opt/cpanel/ea-php53/root/usr/share/php') in /home/pandatra/site.com/contacts_form/contact_form.php on line 9
Here is the code in contact_form.php:
<?php
include 'config.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require ''.$d['include_path'].'PHPMailer/src/Exception.php';
require ''.$d['include_path'].'PHPMailer/src/PHPMailer.php';
require ''.$d['include_path'].'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
if (isset($_POST['Send'])) {
How to fix this? Any ideas? I downloaded version 6.1.7 of PHPMailer.
The error you mentioned is that, the path in your require is getting Wrong. To avoid this type of problem , you should always use absolute path
e.g.
require __DIR__.'/PHPMailer/src/Exception.php';
require __DIR__.'/PHPMailer/src/PHPMailer.php';
require __DIR__.'/PHPMailer/src/SMTP.php';
# use "use" after include or require
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
__DIR__ is the absolute path of running file's directory.
Here's the problem:
I just replaced the old version with the new one
If you upgraded from 5.x to 6.x, you needed to read either the readme, the upgrade guide, or this question and answer that was specifically created to deal with this issue.
I have a problem with PHPMailer. I developed HTML project over Visual Studio Empty Project. I wanna design contact form with PHPMailer because of the server where the project will be loaded is working with PHP(cPanel). I add PHPMailer: https://github.com/PHPMailer/PHPMailer, configure SMTP mail system. Everything is OK I think, but that error when form sending:
"Fatal error: Class 'PHPMailer' not found in /ssd2/myplastech/public_html/test/PHPMailer/src/Index.php on line 29"
I try to all directions solutions but, I didn't solve this problem. What can I do?
Related Parts->
HTML Form:
<form id="contact-form" action="PHPMailer/src/Index.php" method="post">
...
<input type="submit" name="buttonsubmit" id="btn-contact-form"></input>
composer.json:
"require": {
"php": ">=5.5.0",
"phpmailer/phpmailer": "~6.1",
"ext-ctype": "*",
"ext-filter": "*" },
Index.php:
include 'PHPMailer.php';
include 'SMTP.php';
include 'Exception.php';
//Load Composer's autoloader
//require 'vendor/autoload.php';
$mail = new PHPMailer(true);
Do I need to add the vendor or 'vendor/autoload.php' externally? Where can I add if I add?
I am using two libraries on the same page. One is for pdf generation and one is for sending emails. But, this giving me error 500 - llease advise .
After some debugging, I found that it's phpmailer mails not working because of dompdf:
require_once 'lib/dompdf/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}
By using alias:
use \PHPMailer\PHPMailer\{PHPMailer as mailerClass, Exception as mailerException}; // PHP 7+
I think the problem in your code is the duplicate autoloader. The best way is when you install both packages over composer and use the composer autoloader.
In both libs you can find an example how to install them over composer.
composer require dompdf/dompdf
composer require phpmailer/phpmailer
Then you have to include the autoloader for composer.
require 'vendor/autoload.php';
After that you can use the autoloader to load all packages. In the DomPDF you find good examples how to use.
https://github.com/dompdf/dompdf
500 errors are a bit hard to find. You should enable your error logs and check directly your logs. In the log a php error should be shown.
Having problem with upgrading phpMailer 5.xx to 6.0. Used several versions of phpmailer for years, no problems like this. I prefer to not use composer.
From upgrade guide "Alternatively, if you're not using composer, copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each one manually." I prefer to not modify the include_path file on a shared host.
I used the PHPMailerAutoload.php for my 5.xx versions
I used this from the 6.0 package:
"Upgrading from PHPMailer 5.2 to 6.0
If you're not using composer, you can still load the classes manually, depending on what you're using:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/PHPMailer.php';
require 'src/SMTP.php';
No errors reported on these
$mail = new PHPMailer(true);
Fatal error: Uncaught Error: Class 'PHPMailer' not found in ///path to my code
Tried $mail = new \PHPMailer(true);
Tried lower case file names for PHPMailer.php and SMTP.php
Tried class.PHPMailer.php and class.SMTP.php; and with lowercase filenames
Look at the examples for reference - you need to either use a namespaced class or reference it absolutely, for example:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
or
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
As came to light in your github ticket for the same question, use statements only apply in the file in which they are defined (see docs), and so your split into two files (which you didn't mention initially) breaks it. You have them split like this; in the first file:
use PHPMailer\PHPMailer\PHPMailer;
require 'src/PHPMailer.php';
and in the second file:
$mail = new PHPMailer(true);
The use statements do not apply to the second file, so the class is indeed undefined, though the requires are still valid as they are global. You need to split it like this:
require 'src/PHPMailer.php';
and:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
For further info on upgrading to 6.0, read the upgrade guide.