Home > Database > Mysql Tutorial > body text

PHP development practice: Use PHPMailer to send emails to users in the MySQL database

WBOY
Release: 2023-08-05 18:21:22
Original
1515 people have browsed it

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

Introduction:
In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database.

1. Install the PHPMailer library
PHPMailer is a powerful PHP email sending library. It not only supports sending ordinary text emails, but also emails in HTML format, and also supports attachments, SMTP authentication, etc. Function. First, we need to download the PHPMailer library and install it.

  1. Download the PHPMailer library:
    Download the latest version of the PHPMailer library on the official website (https://github.com/PHPMailer/PHPMailer). After unzipping, copy the PHPMailer.php and SMTP.php files to your project.

2. Create a MySQL database
We need to create a MySQL database to store user information and email sending records. Suppose we have created a table named "users" with the following fields:

  • id (primary key, auto-increment)
  • name (user name)
  • email (user email)
  • created_at (creation time)

3. Connect to the MySQL database
In our PHP script, we need to use the MySQL connection string to connect to the database. The following is a basic example:

<?php
$hostname = 'localhost';
$username = 'root';
$password = 'your_password';
$database = 'your_database';

$conn = new mysqli($hostname, $username, $password, $database);

if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}
Copy after login

4. Send emails and save them to the database
The following is a sample code that uses PHPMailer to send emails and save related information to the database:

<?php
require 'PHPMailer.php';
require 'SMTP.php';

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

// 创建PHPMailer对象
$mail = new PHPMailer(true);

try {
    // 配置SMTP服务器
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your_username';
    $mail->Password = 'your_password';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    // 设置邮件信息
    $mail->setFrom('from@example.com', 'Your Name');
    $mail->addAddress('to@example.com', 'Recipient Name');
    $mail->Subject = 'Test Email';
    $mail->Body = 'This is a test email sent using PHPMailer';

    // 发送邮件
    $mail->send();

    // 保存邮件信息到数据库
    $name = 'John Doe';
    $email = 'johndoe@example.com';
    
    $sql = "INSERT INTO users (name, email, created_at) VALUES ('$name', '$email', NOW())";
    $result = $conn->query($sql);
    
    if ($result) {
        echo '邮件发送成功,并已保存到数据库。';
    } else {
        echo '邮件发送成功,但保存到数据库失败。';
    }

} catch (Exception $e) {
    echo '邮件发送失败:' . $mail->ErrorInfo;
}

$conn->close();
?>
Copy after login

Summary:
This article introduces how to use the PHPMailer library to send emails and save email information to the user table in the MySQL database. Through the above practices, we can implement the function of sending emails with user information and record relevant information, adding more practicality and functions to our website or application. I hope this article can provide some help to everyone in using PHPMailer to send emails in PHP development.

The above is the detailed content of PHP development practice: Use PHPMailer to send emails to users in the MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!