×ðÁú¿­Ê±

ÔõÑùʹÓÃThinkPHP6¾ÙÐÐÓʼþ¶©ÔĺÍÍÆËÍ£¿

Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬µç×ÓÓʼþ×÷ΪһÖÖ×îΪ¹Å°åºÍÎȹ̵ÄͨѶ¹¤¾ß£¬ÆäÔÚ¸÷ÐÐÒµÖеÄÖ÷ÒªÐÔÓú·¢Í¹ÏÔ ¡£×÷Ϊ¿ª·¢Õߣ¬Èç×÷ÉõÓû§Ìṩ¿É¿¿¡¢¸ßЧµÄÓʼþ¶©ÔĺÍÍÆËÍЧÀÍ£¬³ÉΪÁËÎÒÃÇÐèҪ˼Ë÷Ï¢Õù¾öµÄÎÊÌâ ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp6¿ò¼Ü¾ÙÐÐÓʼþ¶©ÔĺÍÍÆË͵IJÙ×÷Á÷³Ì£¬Ï£ÍûÄܹ»¶Ô¸÷ÈËÓÐËù×ÊÖú ¡£

×¼±¸ÊÂÇé

Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÍâµØ×°ÖûòÔ¶³ÌЧÀÍÆ÷ÉÏ×°ÖúÃPHP¡¢Apache/NginxµÈWebЧÀÍÆ÷£¬ÒÔ¼°MySQLµÈÊý¾Ý¿â ¡£Í¬Ê±£¬ÎÒÃÇÐèÒªÓõ½SMTPЭÒéÀ´·¢ËÍÓʼþ£¬Òò´ËÎÒÃÇ»¹ÐèÒªÒ»·ÝSMTPЧÀÍÆ÷µÄÕ˺źÍÃÜÂë ¡£

×°Öÿò¼ÜºÍÀ©Õ¹°ü

ÔÚ¾ÙÐÐÏêϸ²Ù×÷֮ǰ£¬ÎÒÃÇÐèҪʹÓÃcomposer×°ÖÃThinkPHP6¿ò¼ÜºÍÀ©Õ¹°ü ¡£ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁî¾ÙÐÐ×°Öà ¡£

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

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬µÚÒ»¸öÏÂÁîÊÇ×°ÖÃThinkPHP6¿ò¼Ü£¬µÚ¶þ¸öÏÂÁîÊÇÏÂÔØPHPMailerÀ©Õ¹°ü£¬µÚÈý¸öÏÂÁîÔòÊǽ«PHPMailerÀ©Õ¹°ü¸´ÖƵ½ThinkPHP6µÄvendorĿ¼Ï ¡£

ÉèÖÃÓʼþºÍ¶©ÔÄÐÅÏ¢

ÔÚ¾ÙÐÐÓʼþ¶©ÔĹ¦Ð§Ö®Ç°£¬ÎÒÃÇÐèÒªÔÚ.envÎļþÖÐÉèÖÃSMTPЧÀÍÆ÷µÄÕ˺š¢ÃÜÂëÒÔ¼°·¢¼þÈËÐÕÃûºÍµØµã£¬ÒÔ±ã³ÌÐò˳Ëì·¢ËÍÓʼþ ¡£Í¬Ê±£¬ÎÒÃÇ»¹ÐèҪн¨Ò»¸ö¶©ÔÄÐÅÏ¢±í£¬ÒÔ±ã´æ´¢Óû§µÄ¶©ÔÄÐÅÏ¢ ¡£ÔÚThinkPHP6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃmigrationÏÂÁÉèÒ»ÕÅÃûΪsubscribe_infoµÄ¶©ÔÄÐÅÏ¢±í ¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

php think migrate:run --seed

µÇ¼ºó¸´ÖÆ

ÔÚÖ´ÐÐÍêÒÔÉÏÏÂÁîºó£¬ÎÒÃÇÐèÒªÔÚsubscribe_info±íÖÐÌí¼ÓÒÔÏÂ×ֶΣº

id£ºÖ÷¼ü£¬×ÔÔöÌí

email£ºÓû§µÄÓÊÏä

is_subscribed£ºÊÇ·ñ¶©ÔÄÓʼþ

±à䶩ÔÄÒ³Ãæ

µ±ÎÒÃÇÍê³ÉÁËÉèÖÃÎļþºÍ¶©ÔÄÐÅÏ¢±íµÄ½¨Éèºó£¬ÎÒÃÇÐèÒª×îÏȱà䶩ÔÄÒ³Ãæ ¡£ÔÚThinkPHP6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃtp6/publicĿ¼ÏµÄindex.phpºÍindex.html¾ÙÐÐÒ³ÃæµÄ¿ª·¢ ¡£ÎªÁËÕ¹ÏÖÀû±ã£¬ÕâÀïÎÒÃÇÖ±½ÓÔÚindex.htmlÖÐÌí¼ÓÒ»¸ö¼òÆÓµÄ±íµ¥£¬ÓÃÓÚÊäÈëÓû§µÄÓÊÏäµØµã²¢Ìá½» ¡£

<!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>

µÇ¼ºó¸´ÖÆ

µ±Óû§Ìá½»±íµ¥ºó£¬ÎÒÃÇÐèÒª½«Óû§ÊäÈëµÄÓÊÏäµØµãÉúÑĵ½¶©ÔÄÐÅÏ¢±íÖУ¬ÒÔ±ã¾ÙÐÐÓʼþÍÆËÍ ¡£

±à䶩ÔÄAPI

ΪÁ˽«Óû§ÊäÈëµÄÓÊÏäµØµãÉúÑĵ½¶©ÔÄÐÅÏ¢±íÖУ¬ÎÒÃÇÐèÒª±àдһ¸öÃûΪSubscribe.phpµÄAPI ¡£ÔÚThinkPHP6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃtp6/application/apiĿ¼¾ÙÐÐAPIµÄ¿ª·¢ ¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄSubscribe.phpÎļþ ¡£

<?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' => '¶©ÔÄÀÖ³É'];
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÏÈ´ÓÇëÇóÖлñÈ¡µ½Óû§ÊäÈëµÄÓÊÏäµØµã£¬²¢²éÕÒ¶©ÔÄÐÅÏ¢±íÖÐÊÇ·ñÒѾ­ÓиÃÓû§µÄ¼Í¼ ¡£ÈôûÓУ¬Ôòн¨Ò»Ìõ¼Í¼£»ÈôÒÑÓУ¬Ôò½«¸Ã¼Í¼µÄis_subscribed×Ö¶ÎÉèÖÃΪtrue ¡£

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔʹÓÃPHPMailerÀ©Õ¹°üÀ´·¢ËÍÓʼþ ¡£ÎÒÃÇÏÈÔÚconfigĿ¼ÏµÄmail.phpÎļþÖÐÌí¼ÓÒÔÏÂÉèÖÃÐÅÏ¢ ¡£

# 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',
];

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏÉèÖÃÐÅÏ¢ÖУ¬ÎÒÃÇÌîдÁËSMTPЧÀÍÆ÷µÄµØµã¡¢¶Ë¿Ú¡¢Õ˺š¢ÃÜÂëµÈÐÅÏ¢ ¡£ÔÚSubscribe.phpÎļþÖУ¬ÎÒÃÇ¿ÉÒÔ½«ÕâЩÐÅÏ¢¶ÁÈ¡³öÀ´£¬²¢Ê¹ÓÃPHPMailerÀ©Õ¹°ü·¢ËÍÓʼþ ¡£Àֳɷ¢ËÍÓʼþºó£¬ÎÒÃÇ·µ»Ø¸øÓû§Ò»¸ö¶©ÔÄÀֳɵÄÐÂÎÅ ¡£

±àдÓʼþÍÆË;籾

µ±Óû§Àֳɶ©ÔÄÓʼþºó£¬ÎÒÃÇÐèÒª±àдһ¸öÓʼþÍÆË;籾£¬ÒÔ±ãÄܹ»×¼Ê±½«×îеÄÎÄÕÂÄÚÈÝÍÆË͸ø¶©ÔÄÓû§ ¡£ÔÚThinkPHP6¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃtp6/application/commandĿ¼¾ÙÐÐÏÂÁî¾ç±¾µÄ¿ª·¢ ¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄMailPush.php¾ç±¾ ¡£

# 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();
        }
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÏÈ´Ó¶©ÔÄÐÅÏ¢±íÖлñÈ¡µ½ÒѾ­¶©ÔĵÄÓû§ÐÅÏ¢£¬È»ºóͨ¹ýPHPMailerÀ©Õ¹°ü·¢ËÍÓʼþ ¡£ÎÒÃÇ»¹ÐèÒªÔÚconfig/app.phpÖÐÌí¼ÓÏÂÁî¾ç±¾µÄ·¾¶ ¡£

# app.php
<?php
return [
    // ...
    'commands'       => [
        'appcommandMailPush'
    ],
    // ...
];

µÇ¼ºó¸´ÖÆ

ÉèÖÃ׼ʱʹÃü

µ±ÎÒÃDZàдºÃÓʼþÍÆË;籾ºó£¬ÎÒÃÇÐèÒªÉèÖÃ׼ʱʹÃü£¬ÒÔ±ã׼ʱִÐÐÓʼþÍÆË;籾 ¡£ÔÚLinuxϵͳÏ£¬ÎÒÃÇ¿ÉÒÔʹÓÃcrontabÏÂÁîÀ´ÉèÖÃ׼ʱʹÃü ¡£ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁî¼´¿É·­¿ª×¼Ê±Ê¹ÃüÉèÖÃÎļþ ¡£

crontab -e

µÇ¼ºó¸´ÖÆ

ÔÚ׼ʱʹÃüÉèÖÃÎļþÖУ¬ÎÒÃÇÌí¼ÓÒÔÏÂÄÚÈÝ£¬ÌåÏÖÌìÌìÏÂÖçµÄ6µãÖÓ£¬Ö´ÐÐÒ»´ÎÓʼþÍÆË;籾 ¡£

0 18 * * * /path/to/php /path/to/tp6/think mail

µÇ¼ºó¸´ÖÆ

Íê³ÉÒÔÉÏÉèÖúó£¬ÎÒÃǾͿÉÒÔÍêÕûµØʹÓÃThinkPHP6¿ò¼Ü¾ÙÐÐÓʼþ¶©ÔĺÍÍÆËÍÁË ¡£Óû§ÊäÈëÓÊÏäµØµã²¢Ìá½»¶©ÔÄÉêÇëºó£¬ÓʼþÍÆË;籾»áÔÚ׼ʱʹÃüµÄ»®×¼Ê±¼ä½«×îÐÂÎÄÕÂÄÚÈÝ·¢Ë͸øÓû§ ¡£Ï£ÍûÄܹ»¶Ô¸÷ÈËÓÐËù×ÊÖú ¡£

ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐÓʼþ¶©ÔĺÍÍÆËÍ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿