v2board/app/Jobs/SendEmail.php
2019-12-30 16:21:50 +08:00

53 lines
1.3 KiB
PHP

<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
use App\Models\MailLog;
class SendEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $params;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($params)
{
$this->params = $params;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$params = $this->params;
$email = $params['email'];
$subject = $params['subject'];
Mail::send(
$params['template_name'],
$params['template_value'],
function ($message) use($email, $subject) {
$message->to($email)->subject($subject);
}
);
MailLog::create([
'email' => $params['email'],
'subject' => $params['subject'],
'template_name' => $params['template_name'],
'error' => count(Mail::failures()) > 0 ? Mail::failures()[0] : NULL
]);
}
}