PHP and PHPMAILER: How to implement the email sending log function in the website?
Overview:
When developing a website, the email sending function is indispensable. However, when a website needs to send a large number of emails or needs to track and record email delivery, we often need to implement an email sending log function to view information such as which emails were successfully sent and which ones failed. This article will introduce how to use PHP and the PHPMailer library to implement the email sending log function, and provide corresponding code examples.
require 'path/to/PHPMailer/src/PHPMailer.php'; require 'path/to/PHPMailer/src/SMTP.php'; require 'path/to/PHPMailer/src/Exception.php';
CREATE TABLE `email_logs` ( `id` int(11) NOT NULL AUTO_INCREMENT, `to_email` varchar(255) NOT NULL, `subject` varchar(255) NOT NULL, `message` text NOT NULL, `sent` tinyint(1) NOT NULL DEFAULT '0', `sent_date` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
function sendEmail($to, $subject, $message) { // 实例化PHPMailer对象 $mail = new PHPMailerPHPMailerPHPMailer(); // 配置SMTP服务器 $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'your_email@example.com'; $mail->Password = 'your_password'; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; // 设置邮件内容和收件人 $mail->CharSet = 'UTF-8'; $mail->setFrom('your_email@example.com', 'Your Name'); $mail->addAddress($to); $mail->Subject = $subject; $mail->Body = $message; // 发送邮件 if ($mail->send()) { // 邮件发送成功,将发送日志保存到数据库中 $sent_date = date('Y-m-d H:i:s'); $insert_query = "INSERT INTO email_logs (to_email, subject, message, sent, sent_date) VALUES ('$to', '$subject', '$message', 1, '$sent_date')"; // 执行插入操作并检查是否成功 if ($conn->query($insert_query) === TRUE) { echo "邮件发送成功!"; } else { echo "邮件发送成功,但保存发送日志失败:" . $conn->error; } } else { echo "邮件发送失败:" . $mail->ErrorInfo; } }
$to = 'recipient@example.com'; $subject = '邮件主题'; $message = '邮件正文'; sendEmail($to, $subject, $message);
$select_query = "SELECT * FROM email_logs"; $result = $conn->query($select_query); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "收件人:" . $row['to_email'] . "<br>"; echo "主题:" . $row['subject'] . "<br>"; echo "内容:" . $row['message'] . "<br>"; echo "发送时间:" . $row['sent_date'] . "<br>"; echo "<br>"; } } else { echo "没有邮件发送日志。"; } $conn->close();
Summary:
By using PHP and the PHPMailer library, we can implement a simple email sending log function to record and track the status of email sending in the website. This article provides the steps to install and introduce the PHPMailer library, as well as how to create an email sending log table and write a function for sending emails. I hope this article will be helpful to you when implementing the email sending log function.
Note: In actual applications, you need to adjust the code according to your own needs and ensure that operations such as database connections and queries can run normally.
The above is the detailed content of PHP and PHPMAILER: How to implement email sending log function in the website?. For more information, please follow other related articles on the PHP Chinese website!