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!

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


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

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

Hot Article

Hot 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.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

WebStorm Mac version
Useful JavaScript development tools
