Gary Dale
Gary Dale

Reputation: 81

How do I get PHPMailer to send a test email?

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('[email protected]'); //Name is optional
//    $mail->addReplyTo('[email protected]', ‘Your Name’);
//    $mail->addCC('[email protected]');
//    $mail->addBCC('[email protected]');

    //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.

Upvotes: 0

Views: 2296

Answers (1)

Gary Dale
Gary Dale

Reputation: 81

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...

Upvotes: 0

Related Questions