With the rapid development of the Internet, email, as the most traditional and stable communication tool, has become increasingly important in various industries. As developers, how to provide users with reliable and efficient email subscription and push services has become a problem that we need to think about and solve. This article will introduce how to use the ThinkPHP6 framework for email subscription and push operations. I hope it will be helpful to everyone.
First, we need to install PHP, Apache/Nginx and other web servers, as well as MySQL and other databases on the local installation or remote server. At the same time, we need to use the SMTP protocol to send emails, so we also need an SMTP server account and password.
Before proceeding with specific operations, we need to use composer to install the ThinkPHP6 framework and extension pack. Enter the following command on the command line to install.
composer create-project topthink/think tp6 --prefer-dist wget https://github.com/phpmailer/phpmailer/archive/master.zip unzip master.zip cp -r phpmailer-master/ tp6/vendor/phpmailer/phpmailer
The first command is to install the ThinkPHP6 framework, the second command is to download the PHPMailer extension package, and the third command is to copy the PHPMailer extension package to the vendor directory of ThinkPHP6.
Before performing the email subscription function, we need to configure the SMTP server account and password in the .env
file As well as the sender's name and address so that the program can send the email smoothly. At the same time, we also need to create a new subscription information table to store the user's subscription information. In the ThinkPHP6 framework, we can use the migration command to create a subscription information table named subscribe_info.
php think migrate:run --seed
After executing the above command, we need to add the following fields to the subscribe_info
table:
When we complete the configuration file and subscription information After the table is created, we need to start writing the subscription page. In the ThinkPHP6 framework, we can use index.php
and index.html
in the tp6/public
directory for page development. For the convenience of presentation, here we add a simple form directly to index.html
for inputting the user's email address and submitting it.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>邮件订阅</title> </head> <body> <h1>邮件订阅</h1> <div> <form method="POST" action="{:url('/api/subscribe/submit')}"> 邮箱:<input name="email" type="email" required> <button type="submit">提交</button> </form> </div> </body> </html>
After the user submits the form, we need to save the email address entered by the user into the subscription information table for email push.
In order to save the email address entered by the user into the subscription information table, we need to write a file called Subscribe.php
API. In the ThinkPHP6 framework, we can use the tp6/application/api
directory for API development. Below is a simple Subscribe.php
file.
<?php namespace apppicontroller; use appcommonmodelSubscribeInfo; use PHPMailerPHPMailerPHPMailer; use thinkacadeConfig; use thinkRequest; class Subscribe { /** * 用户提交订阅信息 * @param Request $request [description] * @return [type] [description] */ public function submit(Request $request) { $email = $request->param('email'); $subscribeInfo = SubscribeInfo::where('email', $email)->find(); if (empty($subscribeInfo)) { $subscribeInfo = new SubscribeInfo(); $subscribeInfo->email = $email; $subscribeInfo->is_subscribed = true; $subscribeInfo->save(); } else { $subscribeInfo->is_subscribed = true; $subscribeInfo->save(); } $mail = new PHPMailer(true); $mail->SMTPDebug = 1; $mail->isSMTP(); $mail->CharSet = 'utf-8'; $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Host = Config::get('mail_host'); $mail->Port = Config::get('mail_port'); $mail->Username = Config::get('mail_username'); $mail->Password = Config::get('mail_password'); $mail->setFrom(Config::get('mail_from_email'), Config::get('mail_from_name')); $mail->addAddress($email); $mail->Subject = '欢迎订阅本站邮件'; $mail->Body = '你好,欢迎订阅本站邮件'; $mail->send(); return ['code' => 0, 'message' => '订阅成功']; } }
In the above code, we first obtain the email address entered by the user from the request, and check whether there is already a record of the user in the subscription information table. If not, create a new record; if it already exists, set the is_subscribed field of the record to true.
Next, we can use the PHPMailer extension package to send emails. We first add the following configuration information to the mail.php
file in the config
directory.
# mail.php <?php return [ 'mail_host' => 'smtp.exmail.qq.com', 'mail_port' => '465', 'mail_username' => 'xxx@xxx.com', 'mail_password' => 'xxxx', 'mail_from_email' => 'xxx@xxx.com', 'mail_from_name' => 'xxx', ];
In the above configuration information, we filled in the address, port, account, password and other information of the SMTP server. In the Subscribe.php
file, we can read this information and use the PHPMailer extension package to send emails. After successfully sending the email, we return a successful subscription message to the user.
After the user successfully subscribes to the email, we need to write an email push script so that the latest article content can be pushed to the subscribed user regularly. In the ThinkPHP6 framework, we can use the tp6/application/command
directory to develop command scripts. The following is a simple MailPush.php
script.
# MailPush.php namespace appcommand; use appcommonmodelSubscribeInfo; use PHPMailerPHPMailerPHPMailer; use thinkconsoleCommand; use thinkconsoleInput; use thinkconsoleOutput; use thinkacadeConfig; class MailPush extends Command { /** * The configuration. * * @var array */ private $config; /** * @inheritdoc */ protected function configure() { $this->setName('mail') ->setDescription('Push article to subscribers'); } /** * Execute the console command. * * @param Input $input * @param Output $output * @return void */ public function execute(Input $input, Output $output) { $subscribeInfos = SubscribeInfo::where('is_subscribed', true)->select(); if ($subscribeInfos) { $mail = new PHPMailer(true); $mail->SMTPDebug = 1; $mail->isSMTP(); $mail->CharSet = 'utf-8'; $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Host = Config::get('mail_host'); $mail->Port = Config::get('mail_port'); $mail->Username = Config::get('mail_username'); $mail->Password = Config::get('mail_password'); $mail->setFrom(Config::get('mail_from_email'), Config::get('mail_from_name')); $mail->isHTML(true); $mail->Subject = '本站新文章通知'; $mail->Body = '亲爱的订阅者,我们有新的文章发布了,快来看看吧!'; foreach ($subscribeInfos as $subscribeInfo) { $mail->addAddress($subscribeInfo->email); } $mail->send(); } } }
In the above code, we first obtain the subscribed user information from the subscription information table, and then send the email through the PHPMailer extension package. We also need to add the path to the command script in config/app.php
.
# app.php <?php return [ // ... 'commands' => [ 'appcommandMailPush' ], // ... ];
After we write the email push script, we need to configure the scheduled task so that the email push script can be executed regularly. Under Linux systems, we can use the crontab
command to configure scheduled tasks. Enter the following command on the command line to open the scheduled task configuration file.
crontab -e
In the scheduled task configuration file, we add the following content, which means that the email push script will be executed at 6 o'clock every afternoon.
0 18 * * * /path/to/php /path/to/tp6/think mail
After completing the above configuration, we can fully use the ThinkPHP6 framework for email subscription and push. After the user enters their email address and submits a subscription application, the email push script will send the latest article content to the user at the specified time of the scheduled task. Hope it helps everyone.
The above is the detailed content of How to use ThinkPHP6 for email subscription and push?. For more information, please follow other related articles on the PHP Chinese website!