This article mainly introduces the implementation of using QQ mailbox to send emails in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it
In PHP application development In , it is often necessary to verify the user's mailbox and send message notifications, and using PHP's built-in mail() function requires the support of the mail system.
If you are familiar with the IMAP/SMTP protocol, you can write an email sending program by combining the Socket function, but developing such a program is not easy.
Fortunately, the PHPMailer package is powerful enough, and it can be used to send emails more conveniently, saving us a lot of extra trouble.
PHPMailer
PHPMailer is an encapsulated PHP mail sending class that supports sending emails with HTML content and can add attachments for sending. It is not like PHP's own mail() function. It requires server environment support. You only need to set up the mail server with relevant information to realize the mail sending function.
PHPMailer project address:https://github.com/PHPMailer/PHPMailer
PHP extension support
PHPMailer requires PHP's sockets extension support, and logging into the QQ mailbox SMTP server must be encrypted through SSL, so PHP must also include openssl support.
↑ Use the phpinfo() function to view socket and openssl extension information (wamp server enables this extension by default).
PHPMailer core files
↑ In this article only class.phpmailer.php and PHPMailer/class.smtp.php are needed.
QQ Mailbox Settings
All mainstream mailboxes support the SMTP protocol, but not all mailboxes are enabled by default. You can manually enable it in the mailbox settings.
After providing the account and password, the third-party service can log in to the SMTP server and use it to control the mail transfer method.
Enable SMTP service
↑ Select the IMAP/SMTP service and click to enable the service.
Verify password
↑ Send the SMS "Configure Email Client" to 1069-0700-69.
Get the authorization code
↑ The SMTP server authentication password needs to be kept properly (PS: There are no spaces in the password).
PHP sends mail
Basic code
The following code demonstrates the use of PHPMailer. Pay attention to the configuration process of the PHPMailer instance.
// 引入PHPMailer的核心文件 require_once("PHPMailer/class.phpmailer.php"); require_once("PHPMailer/class.smtp.php"); // 实例化PHPMailer核心类 $mail = new PHPMailer(); // 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式 $mail->SMTPDebug = 1; // 使用smtp鉴权方式发送邮件 $mail->isSMTP(); // smtp需要鉴权 这个必须是true $mail->SMTPAuth = true; // 链接qq域名邮箱的服务器地址 $mail->Host = 'smtp.qq.com'; // 设置使用ssl加密方式登录鉴权 $mail->SMTPSecure = 'ssl'; // 设置ssl连接smtp服务器的远程服务器端口号 $mail->Port = 465; // 设置发送的邮件的编码 $mail->CharSet = 'UTF-8'; // 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名 $mail->FromName = '发件人昵称'; // smtp登录的账号 QQ邮箱即可 $mail->Username = '12345678@qq.com'; // smtp登录的密码 使用生成的授权码 $mail->Password = '**********'; // 设置发件人邮箱地址 同登录账号 $mail->From = '12345678@qq.com'; // 邮件正文是否为html编码 注意此处是一个方法 $mail->isHTML(true); // 设置收件人邮箱地址 $mail->addAddress('87654321@qq.com'); // 添加多个收件人 则多次调用方法即可 $mail->addAddress('87654321@163.com'); // 添加该邮件的主题 $mail->Subject = '邮件主题'; // 添加邮件正文 $mail->Body = '<h1 id="Hello-nbsp-World">Hello World</h1>'; // 为该邮件添加附件 $mail->addAttachment('./example.pdf'); // 发送邮件 返回状态 $status = $mail->send();
Encapsulation method
If you want to use PHPMailer to send emails directly, you need to perform cumbersome configuration, which will reduce efficiency to some extent.
In order to simplify the calling process, I made a secondary encapsulation based on it. You only need to configure the account, password and nickname to customize your own QQMailer class.
<?php require_once 'PHPMailer/class.phpmailer.php';require_once 'PHPMailer/class.smtp.php'; class QQMailer { public static $HOST = 'smtp.qq.com'; // QQ 邮箱的服务器地址 public static $PORT = 465; // smtp 服务器的远程服务器端口号 public static $SMTP = 'ssl'; // 使用 ssl 加密方式登录 public static $CHARSET = 'UTF-8'; // 设置发送的邮件的编码 private static $USERNAME = '123456789@qq.com'; // 授权登录的账号 private static $PASSWORD = '****************'; // 授权登录的密码 private static $NICKNAME = 'woider'; // 发件人的昵称 /** * QQMailer constructor. * @param bool $debug [调试模式] */ public function __construct($debug = false) { $this->mailer = new PHPMailer(); $this->mailer->SMTPDebug = $debug ? 1 : 0; $this->mailer->isSMTP(); // 使用 SMTP 方式发送邮件 } /** * @return PHPMailer */ public function getMailer() { return $this->mailer; } private function loadConfig() { /* Server Settings */ $this->mailer->SMTPAuth = true; // 开启 SMTP 认证 $this->mailer->Host = self::$HOST; // SMTP 服务器地址 $this->mailer->Port = self::$PORT; // 远程服务器端口号 $this->mailer->SMTPSecure = self::$SMTP; // 登录认证方式 /* Account Settings */ $this->mailer->Username = self::$USERNAME; // SMTP 登录账号 $this->mailer->Password = self::$PASSWORD; // SMTP 登录密码 $this->mailer->From = self::$USERNAME; // 发件人邮箱地址 $this->mailer->FromName = self::$NICKNAME; // 发件人昵称(任意内容) /* Content Setting */ $this->mailer->isHTML(true); // 邮件正文是否为 HTML $this->mailer->CharSet = self::$CHARSET; // 发送的邮件的编码 } /** * Add attachment * @param $path [附件路径] */ public function addFile($path) { $this->mailer->addAttachment($path); } /** * Send Email * @param $email [收件人] * @param $title [主题] * @param $content [正文] * @return bool [发送状态] */ public function send($email, $title, $content) { $this->loadConfig(); $this->mailer->addAddress($email); // 收件人邮箱 $this->mailer->Subject = $title; // 邮件主题 $this->mailer->Body = $content; // 邮件信息 return (bool)$this->mailer->send(); // 发送邮件 } }
QQMailer.php
require_once 'QQMailer.php';// 实例化 QQMailer$mailer = new QQMailer(true);// 添加附件 $mailer->addFile('20130VL.jpg');// 邮件标题 $title = '愿得一人心,白首不相离。';// 邮件内容 $content = 皑如山上雪,皎若云间月。<br>闻君有两意,故来相决绝。<br>今日斗酒会,明旦沟水头。<br>躞蹀御沟上,沟水东西流。<br>凄凄复凄凄,嫁娶不须啼。<br>愿得一人心,白首不相离。<br>竹竿何袅袅,鱼尾何簁簁!<br>男儿重意气,何用钱刀为!EOF; // 发送QQ邮件 $mailer->send('123456789@qq.com', $title, $content);
Test results
php implements calling Baidu’s ocr text recognition interface
php method to implement arithmetic verification code
The above is the detailed content of Implementation of PHP using QQ mailbox to send emails. For more information, please follow other related articles on the PHP Chinese website!

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor