2019-12-26 23:46:09 +08:00
|
|
|
<?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;
|
2019-12-30 17:48:26 +08:00
|
|
|
use App\Models\MailLog;
|
2019-12-26 23:46:09 +08:00
|
|
|
|
2020-03-18 18:12:19 +08:00
|
|
|
class SendEmailJob implements ShouldQueue
|
2019-12-26 23:46:09 +08:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $params;
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-12-26 23:46:09 +08:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($params)
|
|
|
|
{
|
2020-04-07 01:10:14 +08:00
|
|
|
$this->delay(now()->addSecond(2));
|
2020-03-20 13:57:09 +08:00
|
|
|
$this->onQueue('send_email');
|
2019-12-26 23:46:09 +08:00
|
|
|
$this->params = $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$params = $this->params;
|
|
|
|
$email = $params['email'];
|
|
|
|
$subject = $params['subject'];
|
2020-03-18 18:12:19 +08:00
|
|
|
$params['template_name'] = 'mail.' . config('v2board.email_template', 'default') . '.' . $params['template_name'];
|
2019-12-30 17:48:26 +08:00
|
|
|
try {
|
|
|
|
Mail::send(
|
2020-01-11 13:36:52 +08:00
|
|
|
$params['template_name'],
|
2019-12-30 17:48:26 +08:00
|
|
|
$params['template_value'],
|
2020-01-11 13:36:52 +08:00
|
|
|
function ($message) use ($email, $subject) {
|
|
|
|
$message->to($email)->subject($subject);
|
2019-12-30 17:48:26 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$error = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
MailLog::create([
|
|
|
|
'email' => $params['email'],
|
|
|
|
'subject' => $params['subject'],
|
|
|
|
'template_name' => $params['template_name'],
|
|
|
|
'error' => isset($error) ? $error : NULL
|
|
|
|
]);
|
2019-12-26 23:46:09 +08:00
|
|
|
}
|
|
|
|
}
|