Home > Database > Mysql Tutorial > PHP Development Practice: Use PHPMailer to send emails to administrators in MySQL database

PHP Development Practice: Use PHPMailer to send emails to administrators in MySQL database

王林
Release: 2023-07-01 15:16:40
Original
709 people have browsed it

PHP Development Practice: Use PHPMailer to send emails to administrators in the MySQL database

During the development process of web applications, email sending is a common requirement. When a user completes an operation or the system generates an event, we often need to notify the relevant administrator via email. This article will introduce how to use the PHPMailer library to send emails and save the email content to a MySQL database.

1. Install the PHPMailer library

  1. Install the PHPMailer library in the project, and you can manage dependencies through Composer. Execute the following command on the command line to install:

    composer require phpmailer/phpmailer
    Copy after login
  2. Introduce the PHPMailer library into the entry file of the project:

    require 'vendor/autoload.php';
    Copy after login

2. Create a database Table
Create a table named "emails" in the MySQL database to store email-related information. The table structure is as follows:

CREATE TABLE `emails` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `to_email` varchar(255) NOT NULL,
  `subject` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);
Copy after login

3. Send emails and save them to the database

  1. Create a function for sending emails:

    function sendEmail($to, $subject, $message) {
     $mail = new PHPMailerPHPMailerPHPMailer();
     // 邮件服务器配置
     $mail->isSMTP();
     $mail->Host = 'smtp.example.com';
     $mail->Username = 'your_email@example.com';
     $mail->Password = 'your_email_password';
     $mail->SMTPAuth = true;
     $mail->SMTPSecure = 'tls';
     $mail->Port = 587;
    
     // 邮件内容设置
     $mail->setFrom('from@example.com', 'Your Name');
     $mail->addAddress($to);
     $mail->Subject = $subject;
     $mail->Body = $message;
    
     // 发送邮件
     if ($mail->send()) {
         // 邮件发送成功,保存到数据库
         $mysqli = new mysqli('localhost', 'username', 'password', 'database');
         $stmt = $mysqli->prepare("INSERT INTO `emails` (`to_email`, `subject`, `message`) VALUES (?, ?, ?)");
         $stmt->bind_param('sss', $to, $subject, $message);
         $stmt->execute();
         $stmt->close();
         $mysqli->close();
         
         return true;
     } else {
         // 邮件发送失败
         return false;
     }
    }
    Copy after login
  2. Call the send email function and pass in the recipient, email subject and content:

    $to = 'admin@example.com';
    $subject = '有新的通知';
    $message = '您有一个新的通知,请登录后台查看。';
    
    if (sendEmail($to, $subject, $message)) {
     echo '邮件发送成功并保存到数据库。';
    } else {
     echo '邮件发送失败。';
    }
    Copy after login

Through the above code example, we can send the email where we need to and save it to the database By calling the sendEmail function, you can send emails through PHPMailer and save the email information to the MySQL database.

Summary:
This article introduces the development practice of using the PHPMailer library to send emails and save the email content to the MySQL database. By encapsulating the function of sending emails and saving them to the database, we can apply email notification functions in web applications more flexibly. At the same time, using PHPMailer can also achieve more complex email sending needs. I hope this article can provide a reference for PHP developers' practice in sending emails.

The above is the detailed content of PHP Development Practice: Use PHPMailer to send emails to administrators in MySQL database. 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