Home >Backend Development >PHP Tutorial >Implementation of PHP using QQ mailbox to send emails
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 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
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).
↑ In this article only class.phpmailer.php and PHPMailer/class.smtp.php are needed.
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.
↑ Select the IMAP/SMTP service and click to enable the service.
↑ Send the SMS "Configure Email Client" to 1069-0700-69.
↑ The SMTP server authentication password needs to be kept properly (PS: There are no spaces in the password).
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>Hello World</h1>'; // 为该邮件添加附件 $mail->addAttachment('./example.pdf'); // 发送邮件 返回状态 $status = $mail->send();
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 = 41f381271291a4d6375e2131cc9c31c9皑如山上雪,皎若云间月。0c6dc11e160d3b678d68754cc175188a闻君有两意,故来相决绝。0c6dc11e160d3b678d68754cc175188a今日斗酒会,明旦沟水头。0c6dc11e160d3b678d68754cc175188a躞蹀御沟上,沟水东西流。0c6dc11e160d3b678d68754cc175188a凄凄复凄凄,嫁娶不须啼。0c6dc11e160d3b678d68754cc175188a愿得一人心,白首不相离。0c6dc11e160d3b678d68754cc175188a竹竿何袅袅,鱼尾何簁簁!0c6dc11e160d3b678d68754cc175188a男儿重意气,何用钱刀为!94b3e26ee717c64999d7867364b1b4a3EOF; // 发送QQ邮件 $mailer->send('123456789@qq.com', $title, $content);
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!