Home > Article > Backend Development > How to use PHPMailer to send emails in PHP
As a widely used programming language, PHP has many commonly used function modules, among which email sending is one of the most widely used functions. As a very excellent email sending library, PHPMailer can help PHP programmers realize the email sending function simply and quickly.
In this article, we will introduce in detail how to use PHPMailer to send emails.
1. Introduction to PHPMailer
PHPMailer is a PHP class library for sending emails. It provides efficient email sending functions based on the SMTP protocol. This class library can be easily integrated with PHP programs. While implementing the email sending function, it also supports functions such as attachment sending, CC/BCC address addition, and custom email headers.
2. Install the PHPMailer class library
Before using PHPMailer, we need to complete the installation of the class library first, copy the PHPMailer class library to the appropriate location in the project, and then introduce it through the include command Class library is enough.
3. Configure email sending parameters
Before starting to use PHPMailer to send emails, we need to configure the email sending parameters. These parameters include SMTP server address, account and password, sender and recipient’s email address and other information.
The following is a simple example:
<?php require_once 'PHPMailer/PHPMailer.php'; require_once 'PHPMailer/SMTP.php'; $mail = new PHPMailerPHPMailerPHPMailer(); $mail->SMTPDebug = 2; $mail->isSMTP(); $mail->Host = 'smtp.qq.com'; $mail->SMTPAuth = true; $mail->Username = 'sender@qq.com'; $mail->Password = '****'; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; $mail->setFrom('sender@qq.com', 'Sender Name'); $mail->addAddress('receiver@163.com', 'Receiver Name'); $mail->isHTML(true); $mail->Subject = 'Subject of Email'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the plain text message body for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } ?>
In the above example, we specified the SMTP server address as "smtp.qq.com", the account name and password as "sender@qq" .com" and "**", the sender's email is "sender@qq.com", the recipient's email is "receiver@163.com", and the email subject and content have also been specified.
4. Implement email sending
After completing the configuration parameters, we can use the send() method of PHPMailer to implement email sending:
if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }
In the process of implementing email sending, We can also add some other parameters as needed, such as adding CC, BCC addresses, adding attachment information, etc.
$mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); $mail->addAttachment('/var/tmp/file.tar.gz'); $mail->addAttachment('/tmp/image.jpg', 'new.jpg');
5. Summary
PHPMailer is a very excellent email sending class library that can help PHP programmers quickly and easily implement email sending, and supports functions such as adding attachments, adding CC, password, etc. Send address and other advanced functions.
When using PHPMailer, you should pay attention to the configuration of email sending parameters to ensure that emails can be sent normally.
The above is the detailed content of How to use PHPMailer to send emails in PHP. For more information, please follow other related articles on the PHP Chinese website!