Home > Backend Development > PHP Tutorial > How to send emails using PHPMailer in php (code attached)

How to send emails using PHPMailer in php (code attached)

不言
Release: 2023-04-03 16:56:02
Original
2748 people have browsed it

This article introduces to you how to send emails using PHPMailer in PHP (with code attached). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article records my code arrangement for sending emails. Use PHPMailer to implement email sending function

Download the phpmailer address https://github.com/PHPMailer/PHPMailer

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require &#39;./PHPMailer/src/Exception.php&#39;;
require &#39;./PHPMailer/src/PHPMailer.php&#39;;
require &#39;./PHPMailer/src/SMTP.php&#39;;


$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = &#39;smtp.qq.com&#39;;  							// Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = &#39;xxx@qq.com&#39;;                 // SMTP username
    $mail->Password = &#39;xxxx&#39;;                           // SMTP password  QQ邮箱授权码
    $mail->SMTPSecure = &#39;tls&#39;;                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom(&#39;xxx@qq.com&#39;, &#39;Mailer&#39;);
    $mail->addAddress(&#39;xxx@qq.com&#39;, &#39;Joe User&#39;);     // Add a recipient
    $mail->addAddress(&#39;xxx@qq.com&#39;);               // Name is optional
    $mail->addReplyTo(&#39;xxx@qq.com&#39;, &#39;Information&#39;);
    $mail->addCC(&#39;xxx@qq.com&#39;);
    $mail->addBCC(&#39;xxx@qq.com&#39;);

    //Attachments
    //$mail->addAttachment(&#39;/var/tmp/file.tar.gz&#39;);         // Add attachments
    //$mail->addAttachment(&#39;/tmp/image.jpg&#39;, &#39;new.jpg&#39;);    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = &#39;Email title&#39;;
    $mail->Body    = &#39;Email body&#39;;
    $mail->AltBody = &#39;This is the body in plain text for non-HTML mail clients&#39;;

    $mail->send();
    echo &#39;Message has been sent&#39;;
} catch (Exception $e) {
    echo &#39;Message could not be sent. Mailer Error: &#39;, $mail->ErrorInfo;
}
Copy after login

How to obtain the QQ email authorization code is as shown below :

Recommended related articles:

Explanation of views in thinkPHP framework (with code)

php redis mysq How to handle high concurrency (example code)

How to use php to obtain the visitor's IP address (code)

The above is the detailed content of How to send emails using PHPMailer in php (code attached). 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