我们可以在服务器上通过 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定时群发邮件相关推荐

  1. 用PHP写一个每天给女朋友发暖心邮件的脚本

    偶然逛githu发现有一个用node写的一个每天定时给女友发一个暖心小邮件,运用到的技术有邮件.爬虫和定时 我就产生了浓厚的兴趣,这个PHP可以完成,随即动手写了一个PHP版本的 环境需求(高版本环境 ...

  2. mysql查询结果作为附件_python3+mysql查询数据并通过邮件群发excel附件

    python3+mysql查询数据并通过邮件群发excel附件 来源:中文源码网    浏览: 次    日期:2018年9月2日 python3+mysql查询数据并通过邮件群发excel附件 本文 ...

  3. kettle 邮件服务器,kettle 实用功能之三 ---- 使用 kettle 群发动态内容的邮件。

    有个客户是做外贸生意的,经常需要群发邮件,要求邮件里商品名称,收信人称谓等内容是动态的. 他一直找不到一个合适的免费的群发工具,于是我推荐他使用了 kettle,并帮他画了一个流程来群发邮件.(这位朋 ...

  4. 二次开发WinWebMail邮件系统接口 - 企业邮件服务器解决方案

    WinWebMail,是一个比较轻量级的邮件服务器系统,适用于中小型企业的邮件系统,功能也比较齐全,关于它的详细介绍可参见官网地址:http://www.winwebmail.com/.从网上去下载一 ...

  5. Laravel5.6使用定时任务实现定时发邮件

    本文方法是利用Linux的crontab定时任务来协助实现Laravel的任务调度 一.创建项目 1.打开终端,执行命令:laravel new crontab,包括数据库,确保本地能正常运行.随后部 ...

  6. Python 每日定时查询数据库生成Excel报表,并群发邮件

    最近在做游戏打点数据的一些统计处理,写了个Python脚本完成每日定时自动查询生成Excel报表并群发邮件的小功能. 拆解几个需求点: 一.连接数据库并查询 以下是一个查询总注册人数的示例代码,hos ...

  7. php 按照laravel5.5,Laravel5.5 综合使用

    使用 Laravel5.5 开发一个自动交割的项目,把使用到的开源扩展包及特性整理起来,以供后续使用. 一.安装IDE提示工具 Laravel IDE Helper 是一个极其好用的代码提示及补全工具 ...

  8. 邮件撤回成功后对方还能看到吗?163邮箱能群发邮件吗?

    相比免费邮箱,163VIP邮箱邮件群发数量更多,还有邮件撤回功能.TOMvip邮箱单次群发500封,24小时内发出的邮件都可以撤回.关于163邮箱的相关小常识,今天给大家做一个全面分享吧! 163邮箱 ...

  9. 如何群发邮件不进垃圾邮箱?群发邮件进了垃圾箱怎么办?

    邮件进入邮箱后叫做垃圾邮件这个大家都知道,但是对于开拓客户这一类型的的企业来说这是一个让人非常头痛的问题.那如何群发邮件不进垃圾邮箱?群发邮件进了垃圾箱怎么办? 怎么才可以让群发邮件不进垃圾箱?发的邮 ...

最新文章

  1. Nature:手机1秒生成3D全息图;MIT团队新作,索尼投钱支持
  2. BZOJ3632:外太空旅行(最大团,DFS)
  3. SAP OData编程指南
  4. node服务器给客户端发消息,上的node.js发送消息客户端 - 服务器 - 客户端上socket.io(Sen...
  5. 2021年高考呼和浩特市二中成绩查询,呼和浩特市第二中学,是一所百强中学,2020年高考用实力来证明!...
  6. 相似文档查找算法之 simHash
  7. PostgreSQL bloat 检查与处理
  8. Windows桌面文件夹删除被提示“找不到该项目”——顽固目录、文件的删除
  9. 网络安全中非常好用的webshell检测工具!
  10. 干货:一个案例看懂“结巴”分词(Jieba),入行NLP必备
  11. iDRAC 7 Enterp license
  12. AccessibilityService的学习,抢红包实现
  13. 计算机里为什么显示cpu主频高,cpu主频显示不对是什么原因怎么解决
  14. 极大似然估计详解,写的太好了!
  15. Hive 3.x详细笔记
  16. 转载 蓝牙ssp和ble
  17. AS2.0列表带图片视频播放器【附原码下载】
  18. 基于魔兽哈希算法的Ogre资源文件扩展的设计与实现
  19. python re 数据正则化匹配--笔记总览
  20. Sql Server 2008日志满的解决办法

热门文章

  1. 互联网晚报 |12/8 星期四| 购火车票乘车将停止查验核酸证明;iPhone 15折叠屏渲染图曝光;前三季度京津冀经济持续恢复...
  2. 利用requests库和Xpath爬取猫眼电影榜单【Python】
  3. Fedformer中的小波变换(FEB-w模块)
  4. 电脑各修改项【自用】
  5. 获取控制台程序的返回值
  6. unity课设小游戏_unity3d游戏课程设计报告
  7. Hbuilder快捷键教程
  8. 由于找不到mscoree.dll无法继续执行代码
  9. 用4K屏幕的笔记本跑虚拟机vmware如何放大字体?
  10. 实现谷歌浏览器打开海康视频等OCX控件