Home  >  Article  >  Backend Development  >  PHP mass email: send to multiple recipients at once.

PHP mass email: send to multiple recipients at once.

WBOY
WBOYOriginal
2023-09-21 13:30:401456browse

PHP mass email: send to multiple recipients at once.

PHP Bulk Email: Send to multiple recipients at once.

In modern society, email has become one of the indispensable communication tools in people's daily life and work. In some cases, we need to send emails to multiple recipients at one time, such as sending mass event notifications, sending newsletter subscriptions, etc. This article will introduce how to use PHP to write code to send emails to multiple recipients at once.

First, we need to use the SMTP protocol in PHP to send emails. SMTP (Simple Mail Transfer Protocol) is a protocol used to transfer email over the Internet. For convenience, we can use the third-party library PHPMailer to simplify the process of sending SMTP emails.

Next let’s look at specific code examples:

require 'PHPMailerAutoload.php';

// 创建一个PHPMailer对象
$mail = new PHPMailer;

// 设置邮件发送使用SMTP协议
$mail->isSMTP();

// SMTP服务器地址
$mail->Host = 'smtp.example.com';

// 设置SMTP服务器端口号
$mail->Port = 587;

// 启用SMTP身份验证
$mail->SMTPAuth = true;

// 邮件发送账号
$mail->Username = 'your_email@example.com';

// 邮件发送密码
$mail->Password = 'your_email_password';

// 设置发件人名称
$mail->setFrom('your_email@example.com', 'Your Name');

// 添加多个收件人地址
$mail->addAddress('recipient1@example.com');
$mail->addAddress('recipient2@example.com');

// 设置抄送人地址
$mail->addCC('cc@example.com');

// 设置密送人地址
$mail->addBCC('bcc@example.com');

// 设置邮件主题
$mail->Subject = '邮件主题';

// 设置邮件内容
$mail->Body = '邮件内容';

// 设置邮件格式为HTML
$mail->isHTML(true);

// 发送邮件并判断是否发送成功
if(!$mail->send()) {
    echo '邮件发送失败: ' . $mail->ErrorInfo;
} else {
    echo '邮件发送成功';
}

In the above code, we first introduced the PHPMailer library and created a PHPMailer object. We then set up the SMTP server address and port number and enabled SMTP authentication. Then, we set the email sending account and password, and set the sender name. Next, we added multiple recipient addresses to the email through the addAddress method, and set up CC and BCC through the addCC and addBCC methods person's address. We also set the subject, content, and format of the email. Finally, we use the send method to send the email, and judge whether the email is sent successfully by judging the return result.

Through the above code example, we can easily implement the function of sending emails to multiple recipients at one time. At the same time, it is worth noting that in actual applications, we may need to further format and process the content of the email to meet the needs. I hope this article will help you understand and use the PHP mass email function.

The above is the detailed content of PHP mass email: send to multiple recipients at once.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn