Home  >  Article  >  Backend Development  >  How to send email using PHP and PHPMAILER?

How to send email using PHP and PHPMAILER?

WBOY
WBOYOriginal
2023-07-21 09:04:49636browse

How to send email using PHP and PHPMAILER?

With the development of the Internet era, email has become an indispensable part of our daily lives. Today, PHP has become a widely used server-side programming language, and with the PHPMAILER library, you can easily send emails. This article will show you how to send emails using PHP and PHPMAILER.

  1. Download and install the PHPMAILER library
    First, you need to download and install the PHPMAILER library. You can find the download link for the latest version on PHPMAILER's official website (https://github.com/PHPMailer/PHPMailer). Once downloaded, unzip the file and copy the PHPMAILER folder to your project directory.
  2. Create a PHP file
    Create a PHP file named send_email.php in the project directory. This file will be the main script we use to send emails.
  3. Introduce the PHPMAILER library
    Add the following code at the beginning of the send_email.php file to introduce the necessary files for the PHPMAILER library.
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
  1. Write the PHP code to send the email
    In the send_email.php file, you need to write the PHP code to send the email. Below is a basic example showing you how to send an email using PHPMAILER.
// 创建一个新的PHPMailer对象
$mail = new PHPMailer(true);

try {
    // 配置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->setFrom('your_email@example.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');

    // 设置邮件主题和内容
    $mail->Subject = 'Test Email';
    $mail->Body = 'This is a test email sent from PHPMAILER.';

    // 添加附件(可选)
    //$mail->addAttachment('path_to_file');

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

    echo 'Email sent successfully!';
} catch (Exception $e) {
    echo 'Email could not be sent. Error: ', $mail->ErrorInfo;
}

In the above code, you need to replace the SMTP server configuration information with the actual values. In addition, you can customize the subject, content, attachments, etc. of the email.

  1. Test sending email in browser
    Upload send_email.php to your server and access the file in your browser. If everything goes well, you should see the "Email sent successfully!" prompt.

Summary:
By using PHP and the PHPMAILER library, you can easily send emails. It only takes a few steps to configure the SMTP server, set the sender, recipients and email content. The PHPMAILER library provides many other functions and options, such as adding attachments, HTML emails, etc. You can further study and practice as needed. Wish you success in sending emails using PHPMAILER!

The above is the detailed content of How to send email using PHP and PHPMAILER?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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