phpmailerHow can I use PHPMailer to send bulk emails?
PHPMailer is a library for sending emails from PHP. To send bulk emails with PHPMailer, you can use a loop to iterate over an array of email addresses and send an email to each one. For example:
<?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\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Create a new PHPMailer instance
$mail = new PHPMailer;
// Email addresses to send to
$emails = array('[email protected]', '[email protected]', '[email protected]');
// Loop through each email address
foreach ($emails as $email) {
// Set who the message is to be sent to
$mail->addAddress($email);
// Set the subject line
$mail->Subject = 'This is a bulk email';
// Set the body of the message
$mail->Body = 'This is a message sent in bulk.';
// Send the message
$mail->send();
}
This code will send an email with the subject line "This is a bulk email" and body "This is a message sent in bulk." to each address in the $emails
array.
Code explanation
use PHPMailer\PHPMailer\PHPMailer;
&use PHPMailer\PHPMailer\Exception;
: These lines import the PHPMailer classes into the global namespace.require 'vendor/autoload.php';
: This line loads Composer's autoloader.$mail = new PHPMailer;
: This line creates a new PHPMailer instance.$emails = array('[email protected]', '[email protected]', '[email protected]');
: This line creates an array of email addresses.$mail->addAddress($email);
: This line sets the recipient of the email.$mail->Subject = 'This is a bulk email';
: This line sets the subject line of the email.$mail->Body = 'This is a message sent in bulk.';
: This line sets the body of the email.$mail->send();
: This line sends the email.
Helpful links
More of Phpmailer
- How do I use PHPMailer to attach a ZIP file?
- How can I use PHPMailer without SMTP secure?
- How can I configure PHPMailer to support Polish characters?
- How can I configure PHPMailer to work with GoDaddy?
- How do I view the log for my PHPMailer emails?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I use PHPMailer in Yii 1?
- How do I use PHPMailer with Yii2?
- How can I use PHPMailer with XAMPP on Windows?
- How can I use PHPMailer with React?
See more codes...