Table of Contents
PHPMailer
PHP extension support
PHPMailer core files
QQ Mailbox Settings
Enable SMTP service
Verify password
Get the authorization code
PHP sends mail
Basic code
Encapsulation method
Test results
Home Backend Development PHP Tutorial Implementation of PHP using QQ mailbox to send emails

Implementation of PHP using QQ mailbox to send emails

Jul 05, 2018 pm 03:18 PM

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 = &#39;<h1>Hello World</h1>&#39;;
// 为该邮件添加附件
$mail->addAttachment(&#39;./example.pdf&#39;);
// 发送邮件 返回状态
$status = $mail->send();
Copy after login

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 &#39;PHPMailer/class.phpmailer.php&#39;;require_once &#39;PHPMailer/class.smtp.php&#39;;
class QQMailer
{    
    public static $HOST = &#39;smtp.qq.com&#39;; // QQ 邮箱的服务器地址
    public static $PORT = 465; // smtp 服务器的远程服务器端口号
    public static $SMTP = &#39;ssl&#39;; // 使用 ssl 加密方式登录
    public static $CHARSET = &#39;UTF-8&#39;; // 设置发送的邮件的编码

    private static $USERNAME = &#39;123456789@qq.com&#39;; // 授权登录的账号
    private static $PASSWORD = &#39;****************&#39;; // 授权登录的密码
    private static $NICKNAME = &#39;woider&#39;; // 发件人的昵称

    /**
     * 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(); // 发送邮件    }
}
Copy after login

QQMailer.php

require_once &#39;QQMailer.php&#39;;// 实例化 
QQMailer$mailer = new QQMailer(true);// 添加附件
$mailer->addFile(&#39;20130VL.jpg&#39;);// 邮件标题
$title = &#39;愿得一人心,白首不相离。&#39;;// 邮件内容
$content = <<< EOF<p align="center">皑如山上雪,皎若云间月。<br>闻君有两意,故来相决绝。<br>今日斗酒会,明旦沟水头。<br>躞蹀御沟上,沟水东西流。<br>凄凄复凄凄,嫁娶不须啼。<br>愿得一人心,白首不相离。<br>竹竿何袅袅,鱼尾何簁簁!<br>男儿重意气,何用钱刀为!</p>EOF;
// 发送QQ邮件
$mailer->send(&#39;123456789@qq.com&#39;, $title, $content);
Copy after login

Test results

##The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

See all articles