Home >Backend Development >PHP Tutorial >How to send bulk personalized HTML emails using PHP and PHPMAILER?
How to send bulk personalized HTML emails using PHP and PHPMailer?
With the development of the Internet, email has become an indispensable part of people's daily lives. In the business field, sending personalized information to customers or users via email has become a common marketing tool. Using PHP and the PHPMailer library, we can easily send bulk personalized HTML emails. This article will introduce how to use PHP and PHPMailer to achieve this function.
1. Preparation
First, we need to download and install the PHPMailer library. The latest version can be found at https://github.com/PHPMailer/PHPMailer. After unzipping the downloaded library file, introduce the PHPMailer.php
and SMTP.php
files in the src
folder into our project.
Before sending emails, we also need to ensure that our PHP environment has configured the SMTP service. You need to find the following configuration items in the php.ini
file and set them correctly to the available SMTP server information:
SMTP = smtp.example.com smtp_port = 587 sendmail_from = from@example.com
The above is only a sample configuration and needs to be modified according to the actual situation.
2. Implement code logic
Next, let’s write PHP code to implement the batch personalized HTML email sending function.
First, we need to introduce the PHPMailer library and create a new PHPMailer object. The code is as follows:
require 'path/to/PHPMailer.php'; require 'path/to/SMTP.php'; $mailer = new PHPMailerPHPMailerPHPMailer();
Then, we need to configure the SMTP server information and set the email address and nickname of the email sender:
$mailer->IsSMTP(); $mailer->Host = 'smtp.example.com'; $mailer->Port = 587; $mailer->SMTPAuth = true; $mailer->Username = 'your_email@example.com'; $mailer->Password = 'password'; $mailer->SetFrom('from@example.com', 'Your Name');
Next, we need to load the list of received emails and loop through it For each recipient, add the appropriate content for each recipient. This is the key to personalization. The sample code is as follows:
$recipientList = [ ['email' => 'recipient1@example.com', 'name' => 'Recipient 1'], ['email' => 'recipient2@example.com', 'name' => 'Recipient 2'], // 添加更多接收者... ]; foreach ($recipientList as $recipient) { $mailer->AddAddress($recipient['email'], $recipient['name']); // 设置邮件内容 $mailer->Subject = 'Subject of the Email'; $mailer->Body = '<html><body><h1>Hello ' . $recipient['name'] . '</h1><p>This is the content of the email.</p></body></html>'; // 发送邮件 if (!$mailer->Send()) { echo 'Failed to send email to ' . $recipient['email'] . ': ' . $mailer->ErrorInfo . '<br>'; } else { echo 'Email sent to ' . $recipient['email'] . '<br>'; } // 清理邮件配置 $mailer->ClearAddresses(); $mailer->ClearAttachments(); }
Subject
and Body
in the above code can be customized according to the actual situation.
3. Complete sample code
The following is a complete sample code to demonstrate how to use PHP and PHPMailer library to send batch personalized HTML emails:
require 'path/to/PHPMailer.php'; require 'path/to/SMTP.php'; $mailer = new PHPMailerPHPMailerPHPMailer(); $mailer->IsSMTP(); $mailer->Host = 'smtp.example.com'; $mailer->Port = 587; $mailer->SMTPAuth = true; $mailer->Username = 'your_email@example.com'; $mailer->Password = 'password'; $mailer->SetFrom('from@example.com', 'Your Name'); $recipientList = [ ['email' => 'recipient1@example.com', 'name' => 'Recipient 1'], ['email' => 'recipient2@example.com', 'name' => 'Recipient 2'], // 添加更多接收者... ]; foreach ($recipientList as $recipient) { $mailer->AddAddress($recipient['email'], $recipient['name']); $mailer->Subject = 'Subject of the Email'; $mailer->Body = 'Hello ' . $recipient['name'] . '
This is the content of the email.
'; if (!$mailer->Send()) { echo 'Failed to send email to ' . $recipient['email'] . ': ' . $mailer->ErrorInfo . '
'; } else { echo 'Email sent to ' . $recipient['email'] . '
'; } $mailer->ClearAddresses(); $mailer->ClearAttachments(); }
Please make sure before running the code The SMTP server information has been configured correctly and path/to/PHPMailer.php
and path/to/SMTP.php
have been replaced with actual file paths.
The above is the sample code for sending bulk personalized HTML emails using PHP and PHPMailer. With the above code, we can easily send emails to designated recipients and personalize the content of each email. Hope this article is helpful to you!
The above is the detailed content of How to send bulk personalized HTML emails using PHP and PHPMAILER?. For more information, please follow other related articles on the PHP Chinese website!