Uniform and PHPMailer works on localhost but not on server

Hi,

I’m using Uniform and PHPMailer as it is explained here.

Everything is working fine on my localhost server (I use SMTP and everything, I receive emails as expected) but on my server, I got the following error after submitting the form :

There was an error sending the form: The email service is not available: phpmailer

I guess this is because Uniform can’t find the phpmailer service, but I don’t know why and how to fix this.

PHPMailer files are in site/plugins/PHPMailer-driver/.

My phpmailer-driver.php looks like that :

<?php
use PHPMailer\PHPMailer\PHPMailer;

email::$services['phpmailer'] = function($email) {
    require_once(__DIR__ . DS . 'PHPMailer.php');
    require_once(__DIR__ . DS . 'SMTP.php');

    $mail = new PHPMailer();

    //Tell PHPMailer to use SMTP
    $mail->isSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 2;
    //Set the hostname of the mail server
    $mail->Host = 'ssl0.ovh.net';
    //Set the SMTP port number - likely to be 25, 465 or 587
    $mail->Port = 587;
    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'tls';
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication
    $mail->Username = 'myemail@email.com';
    //Password to use for SMTP authentication
    $mail->Password = 'mypassword';

    $mail->CharSet = 'UTF-8';
    $mail->setFrom($email->from);
    $mail->addReplyTo($email->replyTo);
    $mail->addAddress($email->to);


    $mail->isHTML(true);
    $mail->Subject = $email->subject;
    $mail->Body = $email->body;

    if (!$mail->send()) {
        throw new Error('PHPMailer error: ' . $mail->ErrorInfo);
    }
}
?>

and the action in my controller looks like that :

// ... 
if (r::is('POST')) {
        $form->emailAction([
            'to' => 'to@exemple.com',
            'from' => 'from@exemple.com',
            'service' => 'phpmailer',
            'snippet' => 'email-html'
        ]);
    }

Do you have an idea from where the error The email service is not available: phpmailer came from ? Why Uniform can find it on localhost but not on my server ?

Thank you ! :grin:

1 Like

One possible issue: use lower case characters in both your folder and file name.

1 Like

Thank you for your quick answer !

I guess that was it, because I have now an issue with PHPMailer not founding Exception (Kirby CMS Debugger tells me : Class 'PHPMailer\PHPMailer\Exception' not found, even though I added Exception.php to my folder and require it in my phpmailer-driver.php file ) ; but at least phpmailer service is found now !

I will try to debug it myself.

Thank you for your help.

You have to add the use directive as well

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

Yes I did that, the beggining of my phpmailer-driver.php file looks like that :

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

email::$services['phpmailer'] = function($email) {
    require_once(__DIR__ . DS . 'PHPMailer.php');
    require_once(__DIR__ . DS . 'SMTP.php');
    require_once(__DIR__ . DS . 'Exception.php');

    $mail = new PHPMailer();

// options...etc

but the error still happening (Exception.php file is on the server, I checked with Cyberduck, just to be sure)

Maybe you have to require the Exception class first.

It works now ! Thank you for your help, I know it was not a Kirby-related issue but it could help someone else to know that you need to name your folder with lowercase !

Have a nice day ! :grin:

1 Like