ThinkPHP uses PHPMailer to send emails_PHP tutorial
This article uses ThinkPHP version 2.1 and PHPMailer version 5.1. (The latter is recommended that you download it directly from this blog, because we cannot guarantee that the following code will run normally in all versions of PHPMailer)
The following are the specific steps:
The first step, add the PHPMailer class library
PHPMailer email sending class V5.1 download address
Extract the downloaded file and move the PHPMail directory to the Vendor in the ThinkPHP directory. (Please make sure that the class.phpmailer.php file is in ThinkPHPVendorPHPMailerclass.phpmailer.php)
The second step is to add the send mail function
The common.php file in the Common folder in the project directory (please create it if it does not exist) )Add the following code:
<?<span php </span><span /*</span><span ********* * 发送邮件 *</span>
<span * by www.jbxue.com</span>
<span *********</span><span */</span>
<em id="__mceDel"><span function</span> SendMail(<span $address</span>,<span $title</span>,<span $message</span><span ) { vendor(</span>'PHPMailer.class#PHPMailer'<span ); </span><span $mail</span>=<span new</span><span PHPMailer(); </span><span //</span><span 设置PHPMailer使用SMTP服务器发送Email</span> <span $mail</span>-><span IsSMTP(); </span><span //</span><span 设置邮件的字符编码,若不指定,则为'UTF-8'</span> <span $mail</span>->CharSet='UTF-8'<span ; </span><span //</span><span 添加收件人地址,可以多次使用来添加多个收件人</span> <span $mail</span>->AddAddress(<span $address</span><span ); </span><span //</span><span 设置邮件正文</span> <span $mail</span>->Body=<span $message</span><span ; </span><span //</span><span 设置邮件头的From字段。</span> <span $mail</span>->From=C('MAIL_ADDRESS'<span ); </span><span //</span><span 设置发件人名字</span> <span $mail</span>->FromName='LilyRecruit'<span ; </span><span //</span><span 设置邮件标题</span> <span $mail</span>->Subject=<span $title</span><span ; </span><span //</span><span 设置SMTP服务器。</span> <span $mail</span>->Host=C('MAIL_SMTP'<span ); </span><span //</span><span 设置为“需要验证”</span> <span $mail</span>->SMTPAuth=<span true</span><span ; </span><span //</span><span 设置用户名和密码。</span> <span $mail</span>->Username=C('MAIL_LOGINNAME'<span ); </span><span $mail</span>->Password=C('MAIL_PASSWORD'<span ); </span><span //</span><span 发送邮件。</span> <span return</span>(<span $mail</span>-><span Send()); } </span>?></em>
Edit config.php in the Conf directory and add the following content in the return array
<?<span php </span>'MAIL_ADDRESS'=>'xxx@126.com', <span //</span><span 邮箱地址</span> 'MAIL_SMTP'=>'smtp.126.com', <span //</span><span 邮箱SMTP服务器</span> 'MAIL_LOGINNAME'=>'xxx', <span //</span><span 邮箱登录帐号</span> 'MAIL_PASSWORD'=>'xxx', <span //</span><span 邮箱密码</span>
The fourth step is to send an email in Action
Since ThinkPHP will automatically load the functions in common.php, when you need to send an email, you only need to use the following code.
SendMail("xxx@xxx.com","Email title","Email text");
At this point, this tutorial has ended. Spread flowers~~~
Welcome students who have successfully configured SendMail("dreamrunner@foxmail.com","I can also use ThinkPHP to send emails","Wow wow~~");
There are also some instructions
What should I do if I need to wrap the line in the email body? In fact, the simplest method is -
SendMail("xxx@xxx.com", "Email title", "Email text
I changed the line~~!");
QQ mailbox (including foxmail ) and NetEase's 126 and 163 were tested successfully. Because it has been difficult to access GMail recently, I did not test it. I heard that GMail requires the use of SSL. Students who need it can Google Baidu PHPMailer on their own.

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

On Douyin, users can not only share their life details and talents, but also interact with other users. In this process, sometimes we need to send files to other users, such as pictures, videos, etc. So, how to send files to others on Douyin? 1. How to send files to others on Douyin? 1. Open Douyin and enter the chat interface where you want to send files. 2. Click the "+" sign in the chat interface and select "File". 3. In the file options, you can choose to send pictures, videos, audio and other files. After selecting the file you want to send, click "Send". 4. Wait for the other party to accept your file. Once the other party accepts it, the file will be transferred successfully. 2. How to delete files sent to others on Douyin? 1. Open Douyin and enter the text you sent.

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.
