laravel5.7定时群发邮件
我们可以在服务器上通过 crontab -e
来新增或编辑 Cron
条目,通过 crontab -l
查看已存在的 Cron
条目。
开启调度器
下面是你唯一需要添加到服务器的 Cron
条目,如果你不知道如何添加 Cron
条目到服务器,可以考虑使用诸如 Laravel Forge 这样的服务来为管理 Cron
条目:
* * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
该 Cron
将会每分钟调用一次 Laravel
命令调度器,当 schedule:run
命令执行后,Laravel
评估你的调度任务并运行到期的任务。
创建:command
php artisan make:command Mailsend
定时代码
<?phpnamespace App\Console\Commands;use App\Models\Email;
use App\Models\Text;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class Mailsend extends Command
{/*** The name and signature of the console command.** @var string*/protected $signature = 'sendMail';//command名字/*** The console command description.** @var string*/protected $description = 'send mail';//描述无关紧要/*** Create a new command instance.** @return void*/public function __construct(){parent::__construct();}/*** Execute the console command.** @return mixed*/public function handle(){$url = "XXXXXXXXX";//抓取图片$html = file_get_contents($url);preg_match_all('/<img[^>]*\/>/',$html,$img);//如果出现中文乱码使用下面代码//$getcontent = iconv("gb2312", "utf-8",$html);$preg = '/http:\/\/.*/';preg_match_all($preg,$img[0][0],$oneimg);$oneimg =substr($oneimg[0][0],0,-11);//图片$text_preg = '/<a .*?>.*?<\/a>/';preg_match_all($text_preg,$html,$text);$reg3="/>(.*)<\/a>/";preg_match_all($reg3,$text[0][2],$title);$title = $title[1][0];$text=['title'=>$title,'img'=>$oneimg];Text::create($text);$emails = Email::where('type',1)->select('email','oneday','city_code')->get();//用户列表foreach ($emails as $email){$watcher = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;$wundu = $this->HttpGet($watcher);$wundu = json_decode($wundu, true);$day = time()-$email['oneday'];$day = ceil($day/86400);foreach ($wundu['data']['forecast'] as &$wather ){$falg = array_key_exists('aqi',$wather);$wather['low']=mb_substr($wather['low'],2,3);$wather['high']=mb_substr($wather['high'],2,3);if ($falg){$aqi = $wather['aqi'];$wather['quality']=$this->aqi($aqi);}}Mail::send('admin.emails.test', ['tt' => $wundu,'day'=>$day,'img'=>$oneimg,'title'=>$title], function ($message) use ($email) {$to =$email['email'];
// $to = ['396656156@qq.com','997707514@qq.com','528758879@qq.com'];$message->to($to)->subject('带你去映雪湖');});// 返回的一个错误数组,利用此可以判断是否发送成功}
// echo "<textarea style='width:800px;height:600px;'>".$html."</textarea>";}//空气质量public function aqi($aqi){if ($aqi < 51) {return '优';}if ($aqi > 50 && $aqi < 101) {return '良';}if ($aqi > 100 && $aqi < 151) {return '轻度污染';}if ($aqi > 150 && $aqi < 201) {return '中度污染';}if ($aqi > 200 && $aqi < 301) {return '重度污染';}if ($aqi>300){return '严重污染';}}public function HttpGet($url){$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);// curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );// curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36');//如果用的协议是https则打开鞋面这个注释 //curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);$res = curl_exec($curl);curl_close($curl);return $res;}}
配置app/Console/Kernel.php
<?phpnamespace App\Console;use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;class Kernel extends ConsoleKernel
{/*** The Artisan commands provided by your application.** @var array*/protected $commands = [Commands\Mailsend::class//类名//];/*** Define the application's command schedule.** @param \Illuminate\Console\Scheduling\Schedule $schedule* @return void*/protected function schedule(Schedule $schedule){$schedule->command('sendMail')->dailyAt('07:30');//command名// $schedule->command('inspire')// ->hourly();}/*** Register the commands for the application.** @return void*/protected function commands(){$this->load(__DIR__.'/Commands');require base_path('routes/console.php');}
}
模板view
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=no"/><link href="/css/main.css" type="text/css" rel="stylesheet" /><title>测试</title>
</head>
<body>
<div class="content" style="margin: 0 auto;box-shadow: 0 0 10px #333; width: 560px;margin-top: 3.125rem;margin-bottom: 3.125rem; font-size: 1.25rem;color: dimgrey;font-family: '微软雅黑';display: flex;flex-direction: column;text-align: center;"><p class="title">今天是我们认识的第 <span style="color: crimson;font-size: 2rem;">{{$day}}</span> 天</p><p class="mind" style="margin-top: 1.5rem;">{{$tt['data']['forecast']['0']['notice']}}</p><p class="wa" style="font-size: 0.875rem;margin-top: 1rem;">最近几天天气</p><p class="wather" style="margin-top: 1rem;font-size: 1rem;">今天 <span></span>{{$tt['data']['forecast']['0']['type']}} {{$tt['data']['forecast']['0']['low']}}°/{{$tt['data']['forecast']['0']['high']}}° 空气指数:{{$tt['data']['forecast']['0']['aqi']}} {{$tt['data']['forecast']['0']['quality']}}</P><p class="wather" style="margin-top: 1rem;font-size: 1rem;">明天 <span></span>{{$tt['data']['forecast']['1']['type']}} {{$tt['data']['forecast']['1']['low']}}°/{{$tt['data']['forecast']['1']['high']}}° 空气指数:{{$tt['data']['forecast']['1']['aqi']}} {{$tt['data']['forecast']['1']['quality']}}</P><p class="wather" style="margin-top: 1rem;font-size: 1rem;">后天 <span></span>{{$tt['data']['forecast']['2']['type']}} {{$tt['data']['forecast']['2']['low']}}°/{{$tt['data']['forecast']['2']['high']}}° 空气指数:{{$tt['data']['forecast']['2']['aqi']}} {{$tt['data']['forecast']['2']['quality']}}</P><p class="one" style="margin-top: 1.25rem; color: cornflowerblue;font-size: 1rem;">{{date('Y/m/d',time())}}</p><div class="img" style="margin-top: 1.25rem;"><img src="{{$img}}" style=" width: 90%;"></div><p class="imgt" style="font-size: 12px;color: darkgray;margin-bottom: 1.25rem;">摄影</p><div class="text" style="font-size: 0.875rem;width: 80%;margin: 0 auto;line-height: 1.5rem;margin-bottom: 3rem;">{{$title}}</div>
</div>
</body>
</html>
php artisan schedule:run 本地调成每分钟执行就能调试
更多定时方法见:任务调度
laravel5.7定时群发邮件相关推荐
- 用PHP写一个每天给女朋友发暖心邮件的脚本
偶然逛githu发现有一个用node写的一个每天定时给女友发一个暖心小邮件,运用到的技术有邮件.爬虫和定时 我就产生了浓厚的兴趣,这个PHP可以完成,随即动手写了一个PHP版本的 环境需求(高版本环境 ...
- mysql查询结果作为附件_python3+mysql查询数据并通过邮件群发excel附件
python3+mysql查询数据并通过邮件群发excel附件 来源:中文源码网 浏览: 次 日期:2018年9月2日 python3+mysql查询数据并通过邮件群发excel附件 本文 ...
- kettle 邮件服务器,kettle 实用功能之三 ---- 使用 kettle 群发动态内容的邮件。
有个客户是做外贸生意的,经常需要群发邮件,要求邮件里商品名称,收信人称谓等内容是动态的. 他一直找不到一个合适的免费的群发工具,于是我推荐他使用了 kettle,并帮他画了一个流程来群发邮件.(这位朋 ...
- 二次开发WinWebMail邮件系统接口 - 企业邮件服务器解决方案
WinWebMail,是一个比较轻量级的邮件服务器系统,适用于中小型企业的邮件系统,功能也比较齐全,关于它的详细介绍可参见官网地址:http://www.winwebmail.com/.从网上去下载一 ...
- Laravel5.6使用定时任务实现定时发邮件
本文方法是利用Linux的crontab定时任务来协助实现Laravel的任务调度 一.创建项目 1.打开终端,执行命令:laravel new crontab,包括数据库,确保本地能正常运行.随后部 ...
- Python 每日定时查询数据库生成Excel报表,并群发邮件
最近在做游戏打点数据的一些统计处理,写了个Python脚本完成每日定时自动查询生成Excel报表并群发邮件的小功能. 拆解几个需求点: 一.连接数据库并查询 以下是一个查询总注册人数的示例代码,hos ...
- php 按照laravel5.5,Laravel5.5 综合使用
使用 Laravel5.5 开发一个自动交割的项目,把使用到的开源扩展包及特性整理起来,以供后续使用. 一.安装IDE提示工具 Laravel IDE Helper 是一个极其好用的代码提示及补全工具 ...
- 邮件撤回成功后对方还能看到吗?163邮箱能群发邮件吗?
相比免费邮箱,163VIP邮箱邮件群发数量更多,还有邮件撤回功能.TOMvip邮箱单次群发500封,24小时内发出的邮件都可以撤回.关于163邮箱的相关小常识,今天给大家做一个全面分享吧! 163邮箱 ...
- 如何群发邮件不进垃圾邮箱?群发邮件进了垃圾箱怎么办?
邮件进入邮箱后叫做垃圾邮件这个大家都知道,但是对于开拓客户这一类型的的企业来说这是一个让人非常头痛的问题.那如何群发邮件不进垃圾邮箱?群发邮件进了垃圾箱怎么办? 怎么才可以让群发邮件不进垃圾箱?发的邮 ...
最新文章
- 【C++算法与数据结构学习笔记------用循环数组实现队列】
- pandas 笔记:multi-index
- 网络流24题之餐巾计划问题
- HDU 4911 Inversion 树状数组求逆序数对
- 1075 链表元素分类 (25 分)
- 对应oracle生成java对象,Java学习笔记(十三)——通过Netbeans开发环境生成oracle数据库中表的对应hibernate映射文件...
- 带有示例的Python File readline()方法
- 服务器系统日志6008,DELL服务器宕机事件6008
- mysql数据库 集群_mysql数据库集群
- 软工网络15团队作业4-DAY5
- Wings-让单元测试智能全自动生成
- STM32零碎问题记录
- 论文降重从80%降到1%的方法
- Linux之glibc2.17版本升级至2.31
- EverMonkey-VSCode上最好用的印象笔记插件
- HP5100常见错误代码
- 3d人体智能测试软件,人体系统女性3Dapp
- python123随机密码生成_Python生成对应随机密码文件
- SLG游戏中绘制六边形网格地形
- 2018蓝桥杯 题解
热门文章
- 三代测序的简介和原理
- 思考题:一条信息可通过如图所示的网络线由上(A点)往下向各站点传送,例如信息到达b2点可由经a1的站点送达,也可以由经a2的站点送达,共有两条途径传送,那么信息由A点到达d3的不同途径共有多少条
- Spring Data Redis 让 NoSQL 快如闪电(2)
- 时间管理-系统管理员
- 直面大数据撞击这个时代——畅享网成功举办大数据应用沙龙
- 视频拍摄——视点和视角
- wex5 mysql闪退_WeX5软件里无法启动MySQL管理工具
- ufs2.1 android bench,小米6使用的是UFS2.1闪存吗?小米6闪存AndroBench跑分成绩
- php lumen timestamp,一次 lumen 调优的记录
- 电子设计硬件可靠性设计----总结2