Implementation of PHP using QQ mailbox to send emails

不言
Release: 2023-04-02 14:50:02
Original
11606 people have browsed it

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!