PHPMailer Pro Examples
All of the links at the left are examples designed to show how to send emails using PHPMailer Pro version 2024.1.3.0 or higher . The advanced tests fully use Exception Handling and provide in-depth error messages.
MySQLi Database - SMTP basic test with authentication
Demonstrates how to a Database Mailing List using SMTP as email transport – basic test.
<html>
<head>
<title>PHPMailer Pro - MySQLi Database - SMTP basic test with authentication</title>
</head>
<body>
<?php
/*
* This test designed for PHPMailer Pro version 2024.1.2.1 and up ...
*/
// There are a number of ways to test your scripts. One of those is
// aboutmy.email test service ...
// if you prefer NOT to use a test service, please change
// the email addresses as you would like them to be for your tests
require_once('assets/lib/PHPMailer.Pro.php');
class_alias("codeworxtech\PHPMailerPro\PHPMailerPro", "PHPMailerPro");
$mail = new PHPMailerPro();
$recipient = ['recipient@example.com'=>'Recipient Name'];
$sender = ['sender@example.net'=>'List manager'];
$mail->SetSender($sender);
$mail->smtpKeepAlive = true; // SMTP connection will not close after each email sent
$mail->smtpHost = "mail.example.com"; // sets the SMTP server
$mail->smtpPort = 587; // set the SMTP port for the mail server
$mail->smtpUsername = "yourname@yourdomain"; // SMTP account username
$mail->smtpPassword = "yourpassword"; // SMTP account password
$mail->subject = "PHPMailer Pro Database SMTP test with authentication";
$cnx = mysqli_connect('localhost', 'username', 'password');
mysqli_select_db($cnx, 'my_mailing_list');
$res = mysqli_query($cnx, 'SELECT `full_name`, `email`, `photo`, `photo_filename` FROM `mailinglist` WHERE `sent` = FALSE');
while ($row = mysql_fetch_array ($res)) {
$name = $row["full_name"];
$email = $row["email"];
$pix = $row["photo"];
$file = $row["photo_filename"];
$mail->messageText = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->messageHTML = "contents.html";
$mail->AddRecipient([$email=>$name]);
$mail->AddStringAttachment($pix, $file);
if($mail->Send()) { // sends using SMTP transport
echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "@", $row["email"]) . ')<br>';
} else {
echo "Mailer Error (" . str_replace("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>';
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
</body>
</html>
Download example