2019-12-26 23:46:09 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
2024-02-17 11:58:37 +00:00
|
|
|
use App\Models\MailLog;
|
|
|
|
use Exception;
|
2019-12-26 23:46:09 +08:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-08-22 14:28:47 +08:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2019-12-26 23:46:09 +08:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2024-02-17 11:58:37 +00:00
|
|
|
use Postal\Client;
|
|
|
|
use Postal\Send\Message;
|
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;
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2023-04-04 17:58:25 +08:00
|
|
|
public $tries = 3;
|
|
|
|
public $timeout = 10;
|
2024-02-17 11:58:37 +00:00
|
|
|
protected $params;
|
|
|
|
|
2019-12-26 23:46:09 +08:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-12-26 19:21:29 +08:00
|
|
|
public function __construct($params, $queue = 'send_email')
|
2019-12-26 23:46:09 +08:00
|
|
|
{
|
2021-12-26 19:21:29 +08:00
|
|
|
$this->onQueue($queue);
|
2019-12-26 23:46:09 +08:00
|
|
|
$this->params = $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-02-17 11:58:37 +00:00
|
|
|
$driver = "";
|
2020-08-22 14:28:47 +08:00
|
|
|
if (config('v2board.email_host')) {
|
2024-02-17 11:58:37 +00:00
|
|
|
$driver = "SMTP";
|
2020-08-22 14:28:47 +08:00
|
|
|
Config::set('mail.host', config('v2board.email_host', env('mail.host')));
|
|
|
|
Config::set('mail.port', config('v2board.email_port', env('mail.port')));
|
|
|
|
Config::set('mail.encryption', config('v2board.email_encryption', env('mail.encryption')));
|
|
|
|
Config::set('mail.username', config('v2board.email_username', env('mail.username')));
|
|
|
|
Config::set('mail.password', config('v2board.email_password', env('mail.password')));
|
2024-02-17 11:58:37 +00:00
|
|
|
} elseif (config('v2board.email_postal_host')) {
|
|
|
|
$driver = "Postal";
|
2020-08-22 14:28:47 +08:00
|
|
|
}
|
2024-02-17 11:58:37 +00:00
|
|
|
Config::set('mail.from.address', config('v2board.email_from_address', env('mail.from.address')));
|
|
|
|
Config::set('mail.from.name', config('v2board.app_name', 'V2Board'));
|
2019-12-26 23:46:09 +08:00
|
|
|
$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 {
|
2024-02-17 11:58:37 +00:00
|
|
|
switch ($driver) {
|
|
|
|
case 'SMTP':
|
|
|
|
Mail::send(
|
|
|
|
$params['template_name'],
|
|
|
|
$params['template_value'],
|
|
|
|
function ($message) use ($email, $subject) {
|
|
|
|
$message->to($email)->subject($subject);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'Postal':
|
|
|
|
$senderName = Config::get('mail.from.name');
|
|
|
|
$senderAddress = Config::get('mail.from.address');
|
|
|
|
$client = new Client(config('v2board.email_postal_host'), config('v2board.email_postal_key'));
|
|
|
|
$message = new Message();
|
|
|
|
$message->to($email);
|
|
|
|
$message->from("$senderName <$senderAddress>");
|
|
|
|
$message->sender($senderAddress);
|
|
|
|
$message->subject($subject);
|
|
|
|
$message->htmlBody(view($params['template_name'], $params['template_value'])->render());
|
|
|
|
$client->send->message($message);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
2019-12-30 17:48:26 +08:00
|
|
|
$error = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
2021-11-30 16:23:46 +08:00
|
|
|
$log = [
|
2019-12-30 17:48:26 +08:00
|
|
|
'email' => $params['email'],
|
|
|
|
'subject' => $params['subject'],
|
|
|
|
'template_name' => $params['template_name'],
|
2024-02-17 11:58:37 +00:00
|
|
|
'error' => $error ?? NULL
|
2021-11-30 16:23:46 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
MailLog::create($log);
|
|
|
|
$log['config'] = config('mail');
|
|
|
|
return $log;
|
2019-12-26 23:46:09 +08:00
|
|
|
}
|
|
|
|
}
|