Home >Backend Development >PHP Tutorial >PHP and PHPMAILER: How to implement mass mailing function in the website?
PHP and PHPMAILER: How to implement mass mailing function in the website?
Overview:
With the development of the Internet, email has become one of the important ways for people to communicate and communicate. In website development, we often need to implement mass email functions to send large amounts of emails, such as news subscriptions, event notifications, etc. This article will introduce how to use PHP and PHPMailer to implement mass email functionality.
<?php require 'PHPMailer.php'; require 'SMTP.php'; require 'Exception.php'; use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; // 邮件发送函数 function sendEmail($to, $subject, $body) { $mail = new PHPMailer(true); // 实例化PHPMailer类 try { // 配置SMTP服务器 $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'your_email@example.com'; $mail->Password = 'your_email_password'; $mail->Port = 587; // 配置发件人和收件人 $mail->setFrom('your_email@example.com', 'Your Name'); $mail->addAddress($to); // 配置邮件内容 $mail->isHTML(true); $mail->Subject = $subject; $mail->Body = $body; // 发送邮件 $mail->send(); echo '邮件发送成功!'; } catch (Exception $e) { echo '邮件发送失败:' . $mail->ErrorInfo; } } // 读取收件人列表 $recipients = file('recipients.txt', FILE_IGNORE_NEW_LINES); // 发送邮件给每个收件人 foreach ($recipients as $recipient) { $to = trim($recipient); $subject = '邮件标题'; $body = '邮件内容'; sendEmail($to, $subject, $body); } ?>
function is the core code for sending emails. It sends emails by configuring the SMTP server, sender, recipient and email content. You need to modify the corresponding configuration parameters according to your own needs, including SMTP server address, sender email address and password.
file() function, and the email is sent to each recipient using a
foreach loop.
The above is the detailed content of PHP and PHPMAILER: How to implement mass mailing function in the website?. For more information, please follow other related articles on the PHP Chinese website!