Simple Guide: Using PHPMailer to Send Test Emails
P粉344355715
2023-08-26 21:06:15
<p>I am running a local Apache2 server on Debian/Bullseye. I've been trying to get PHPMailer to work but have been unsuccessful. There seem to be two different ways to install PHPMailer - the first is using composer, which was the first method I tried. It creates a vendor folder in the root of the site, which contains an autoload.php file, as well as a few other files. The file contains the following content: </p>
<pre class="brush:php;toolbar:false;"><?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitd359baac21f520c04e608f4eed750560::getLoader();</pre>
<p>This looks incomplete (no closing tag). Anyway, I can't get the "test.php" example to work. </p>
<p>Another method is to download the .zip file from the gethub website and extract it to the root directory of the site. After renaming, I got a PHPMailer folder. Using the "mailer.php" example also has no effect. </p>
<p>In both cases, I modified the smtp information to use the actual account information for the domain (sending email, login password, smtp server name, host's smtp security, and port settings), but I even No reply to the rejection email was received. Nothing happened. All I see is a blank web page. </p>
<p> I do have php running because a php script I used before still works (from my test site - the actual site now insists on using smtp and won't let me install the PEAR module).</p>
<p>Here's the mailer.php script I'm using - some details hidden: </p>
<pre class="brush:php;toolbar:false;"><?php
//Import the PHPMailer class into the global namespace
//These must be at the top of the script, not inside the 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; pass 'true' to enable exceptions
$mail = new PHPMailer(true);
Try
{
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable detailed debugging output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'mail.<domain>.ca'; //Set the SMTP server to be passed
$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; if 'SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS' is set, please use 587
//recipient
$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 attachment
// $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}";
}
?></pre>
<p>The TLS lines are from the hosting company, so I assume they are correct. </p>
<p>I've commented out the options I don't need in my test, but it still doesn't work. Can anyone figure out what I'm doing wrong? </p>
<p>Thank you. </p>
Ok, I see. The hosting company provides test code, and their example uses smtp.domain.com. Actually, their smtp server is mail, not smtp. I guess the code just stops running when faced with a non-existent server...