i just updated my phpmailer installation via composer and previously everything worked fine.
just now i noticed phpmailer is using namespaces so i had to make some chances.
i used to require vendor/autoload.php just within a plugin and it seemed to be working fine for the most part. however with the namespaces, i could not seem to get it working via a kirby hook function.
i have tried several places but couldn’t decide where i should require autoload to make it most accessable everywhere. while controllers/templates work, plugins do seem to have an issue.
<?php
use PHPMailer\PHPMailer\PHPMailer;
$kirby = kirby();
$site = site();
$user = site()->user();
$users = site()->users();
kirby()->hook('panel.page.update', function($page) {
// Use Email Function below ...................
$mail = new PHPMailer;
// WIth the line above, it'll say class 'PHPMailer\PHPMailer\PHPMailer' not found in file: (this file)
im not using PHPMailer but i do have a site that uses third party libraries installed via Composer (specifically, its the google calender API for PHP library.)
All i did was require this library at the top of all the template files (for some reason a Kirby snippet call broke it, but a require works.):
Im probably going to get into trouble for this because its a hack, but what happens if you require the library into Kirbies index.php. I’m not hugely knowledgable about PHP but i think that will technically make it available eveywhere.
isn’t it the point of composer to make a library available thoughout the whole project?
it’s not like anyone could just access it…
so to clarify, each file which needs to use phpmailer in this case, would have a separate line to require autoload?
so having 20 templates/controllers which send email though php, mean 20 times manually adding the require… meh.
No need to put “use Namespace” there, it seems that it’s only limited within the same file each, so in each template or controller / plugin i’d have to post it yet again…
<?php
// controller.php
use PHPMailer\PHPMailer\PHPMailer;
return function($site, $pages, $page){
// here it'S important to declare it before any function starts.
// template.php
use PHPMailer\PHPMailer\PHPMailer;
// do more
// plugin
use PHPMailer\PHPMailer\PHPMailer;
// do more
i have seen it’s getting more and more common to use namespaces, e.g. paypal ipn also suggests that on github, so for reference this would be similar.