Home > Backend Development > PHP Tutorial > PHP and PHPMAILER: How to implement email sending log function in the website?

PHP and PHPMAILER: How to implement email sending log function in the website?

王林
Release: 2023-07-21 12:52:02
Original
1277 people have browsed it

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.

  1. Install and introduce the PHPMailer library:
    First, we need to download and install the PHPMailer library. You can find the installation package on PHPMailer's official website (https://github.com/PHPMailer/PHPMailer), or install it through Composer. After downloading the library file, unzip it to your project directory and introduce the autoload.php file into the PHP file:
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
require 'path/to/PHPMailer/src/Exception.php';
Copy after login
  1. Create an email sending log table:
    In Create a table in the database to save email sending logs. The structure of the table is as follows:
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;
Copy after login
  1. Writing an email sending function:
    Next, we need to write a function for sending emails and recording sending logs. Here is an example:
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;
    }
}
Copy after login
  1. Call the send mail function:
    Now we can call the above send mail function in other parts of the website and pass the recipient, subject and body and other parameters. The following is an example call:
$to = 'recipient@example.com';
$subject = '邮件主题';
$message = '邮件正文';

sendEmail($to, $subject, $message);
Copy after login
  1. View the email sending log:
    You can view the email sending log by querying the email_logs table. The following is a query example:
$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();
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template