0

I have a local Apache2 server running on Debian/Bullseye. I've been pulling my hair out trying to get PHPMailer to do anything. There seem to be 2 different methods for installing PHPMailer - 1 is to use composer, which is the first one I tried. It creates a vendor folder in the site's root, which includes an autoload.php file, among other things. That file contains:

 <?php
// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitd359baac21f520c04e608f4eed750560::getLoader();

Which doesn't look like it's complete (no closing tag). Anyway, I can't get the "test.php" samples to work.

The other method is to download the .zip file from the gethub site and extract it into the site's root. This, after renaming, gives me a PHPMailer folder. Using the "mailer.php" samples doesn't do anything either.

In both cases I've modified the smtp information to use the domain's actual account information (sending e-mail, login password, smtp server name, host's smtp security and port settings) but I'm not even getting it rejecting the mail. Nothing is happening. I'm left with a blank web page.

I do have php running, as a previous php script I'd used still works (from my test site - the live site now insists on smtp and won't let me install PEAR modules).

Here's the mailer.php script I'm working with - with some details hidden:

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function

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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

Try
 {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
    $mail->isSMTP(); //Send using SMTP
    $mail->Host = 'mail.<domain>.ca'; //Set the SMTP server to send through
    $mail->SMTPAuth = true; //Enable SMTP authentication
    $mail->Username = 'mail@<domain>.ca'; //SMTP username
    $mail->Password = '<secret>'; //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
    $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('mail@<domain>.ca', ‘from me’);
    $mail->addAddress('gary@<domain>.ca', ‘to me’); //Add a recipient
//    $mail->addAddress('Recipient@emailaddress.com'); //Name is optional
//    $mail->addReplyTo('yourname@domain.com', ‘Your Name’);
//    $mail->addCC('cc@example.com');
//    $mail->addBCC('bcc@example.com');

    //Attachments Optional
//    $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
//    $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name

    //Content
    $mail->isHTML(true); //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body = 'This is the HTML message body in bold!';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} 
catch (Exception $e) 
{
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

The TLS line are from the hosting company so I assume they are correct.

I've commented out options that I don't need for testing but it still doesn't work. Can anyone figure out what I'm doing wrong?

Thanks.

Gary Dale
  • 79
  • 9
  • 1
    It’s late for me so I didn’t get too far reading (sorry), but “no closing tag” is actually considered a best practice in PHP – Chris Haas Sep 07 '22 at 01:46
  • 1
    Additionally, remove the `try` when debugging, and turn on PHP error reporting. You want things to scream at this point. – Chris Haas Sep 07 '22 at 01:50
  • Lastly, please check through our [canonical answer](https://stackoverflow.com/a/24644450/231316) for troubleshooting, there’s a lot in there. Do you have DNS-based permission to send (SPF)? Is it signed appropriately (DKIM)? Also, sending, transporting and receiving are all separate tasks. – Chris Haas Sep 07 '22 at 01:55
  • Thanks Chris. I'll give it a try tomorrow - too late now. As for the last bit, I can send e-mail locally through my web server but apparently smtp authentication has become a necessity these days. I do it through Exim4 on my server (and on my workstation) so the authentication values should be the same as the ones the hosting company suggests. Certainly they seem to work with Thunderbird for that account. – Gary Dale Sep 07 '22 at 03:45
  • Though you have installed PHPMailer with composer, you're not using the autoloader to load it – you shouldn't need those `require` lines. You have enabled debug output, so you should be seeing lots of output if it's connecting. If it's not connecting, setting `SMTPDebug = SMTP::DEBUG_CONNECTION` might help, and then refer to the PHPMailer troubleshooting guide. – Synchro Sep 07 '22 at 09:25

1 Answers1

0

OK, got it. The host company provided the test code and their sample used smtp.domain.com. In actuality, their smtp server is mail, not smtp. I guess that when faced with a non-existent server, the code hangs...

Gary Dale
  • 79
  • 9