如何解决如何在laravel中发送带有计划队列的电子邮件
我想在 laravel 项目上使用 artisan 命令发送电子邮件
所以我创建了命令并尝试使用 artisan 命令发送电子邮件。
<?php
namespace 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\DailyQuote::class,];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('quote:daily')
->everyMinute();;
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
我已经像上面那样制作了 app/console/kernal.php。
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Mail\InvoiceNotification;
use Mail;
class DailyQuote extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'quote:daily';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send notification to user who will expire';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$details = [
'lastname' => '$client->lastname','invoiceUrl' => 'https://billing.hutv.me/invoice_view/invoice_id','invoiceDueDate' => '$client->expiration'
];
Mail::to('receiver@mail.com')->send(new InvoiceNotification($details));
$this->info('Successfully sent daily quote to everyone.');
}
}
这是我的app/console/commands/DailyQuote.php
我已在 Ubuntu 服务器上使用 php artisan quote:daily
命令执行
但电子邮件发送没有错误。
所以我试图在我的另一个控制器上调用命令
Artisan::call('quote:daily');
它正在工作并正在发送电子邮件。
我不知道那个问题的问题是什么。
如果有人有好的解决方案,请帮助我。
谢谢。
解决方法
通过 php artisan make:notification InvoiceNotification
生成 laravel 通知,这应该实现 ShouldQueue 如下:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Mail\BaseMail; // optionaly your email template
class InvoiceNotification extends Notification implements ShouldQueue
{
use Queueable;
public function toMail($notifiable)
{
return (new BaseMail())
->subject('your subject')
->to($notifiable->email);
}
// ...
}
然后发送例如通过
public function handle()
{
$details = [
'lastname' => '$client->lastname','invoiceUrl' => 'https://billing.hutv.me/invoice_view/invoice_id','invoiceDueDate' => '$client->expiration'
];
Notification::route('mail','receiver@mail.com')
->notify(new InvoiceNotification($details))
$this->info('Successfully sent daily quote to everyone.');
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。