Home > Article > PHP Framework > How to send emails in ThinkPHP6?
In recent years, email, as the most common communication method, has been widely used in various application scenarios. In different WEB applications, it is often necessary to send emails for notification, verification and other functions. In the process of developing WEB applications using the ThinkPHP6 framework, we need to understand how to perform email sending operations in order to better implement various functions. Below we will introduce how to send emails in ThinkPHP6.
It is very convenient to configure email in ThinkPHP6. You only need to add the following code to the .env file in the root directory of the project:
MAIL_DRIVER=smtp MAIL_HOST=smtp.qq.com MAIL_PORT=465 MAIL_USERNAME=xxx@qq.com MAIL_PASSWORD=xxx MAIL_ENCRYPTION=ssl MAIL_FROM_ADDRESS=xxx@qq.com MAIL_FROM_NAME=xxx
In the above code, MAIL_DRIVER specifies the driver used, here we use smtp; MAIL_HOST specifies the address of the mail server, Here we use the address of the QQ corporate mailbox; MAIL_PORT specifies the port number, here we use port 465; MAIL_USERNAME specifies the account of the sender's mailbox; MAIL_PASSWORD specifies the password of the sender's mailbox; MAIL_ENCRYPTION specifies the email encryption method. Here we use SSL encryption; MAIL_FROM_ADDRESS specifies the sender's email address; MAIL_FROM_NAME specifies the sender's name.
After configuring the email, we can start sending emails. Using the mail class Mail provided by ThinkPHP6, you can realize the mail sending operation very conveniently. The following is a simple email sending example:
use thinkacadeMail; Mail::to('xxx@qq.com')->subject('测试邮件')->html('<h1>这是一封测试邮件</h1>');
In the above code, we use the to, subject, and html methods of the Mail class. The to method specifies the recipient's email address; the subject method specifies the email subject; the html method specifies the email body content, and can use HTML format.
Note: Before sending emails, we need to ensure that the PHPMailer class library has been installed. You can install it using Composer, which can be installed with the following command:
composer require phpmailer/phpmailer
In the above example, we did it by doing it in the .env file Configure to implement the email sending function. But sometimes, we may need to dynamically change the email configuration in the code based on the scenario. At this time, we can use the Config class to achieve custom configuration through the following code:
use thinkacadeConfig; use thinkacadeMail; $config = [ 'smtp_host' => 'smtp.qq.com', 'smtp_port' => '465', 'smtp_user' => 'xxx@qq.com', 'smtp_pass' => 'xxx', 'smtp_secure' => 'ssl' ]; Config::set('mail', $config); Mail::to('xxx@qq.com')->subject('测试邮件')->html('<h1>这是一封测试邮件</h1>');
In the above code, we use the set method of the Config class to configure the mail, and then use the Mail class to Perform email sending operations. In this way, we can configure the email according to our own needs.
Summary
This article introduces the relevant content of sending emails in ThinkPHP6. Simply configure the email parameters and then call the Mail class to quickly implement the email sending function. In actual use, we can customize the configuration according to our own needs to ensure that the email sending function can be completed more flexibly and efficiently.
The above is the detailed content of How to send emails in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!