This commit is contained in:
pplulee 2024-02-26 21:39:45 +00:00 committed by GitHub
commit 7e24e200b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 14 deletions

View File

@ -61,6 +61,8 @@ class ConfigSave extends FormRequest
'email_password' => '',
'email_encryption' => '',
'email_from_address' => '',
'email_postal_host' => '',
'email_postal_key' => '',
// telegram
'telegram_bot_enable' => 'in:0,1',
'telegram_bot_token' => '',

View File

@ -2,6 +2,8 @@
namespace App\Jobs;
use App\Models\MailLog;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -9,15 +11,17 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
use App\Models\MailLog;
use Postal\Client;
use Postal\Send\Message;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $params;
public $tries = 3;
public $timeout = 10;
protected $params;
/**
* Create a new job instance.
*
@ -36,20 +40,26 @@ class SendEmailJob implements ShouldQueue
*/
public function handle()
{
$driver = "";
if (config('v2board.email_host')) {
$driver = "SMTP";
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')));
} elseif (config('v2board.email_postal_host')) {
$driver = "Postal";
}
Config::set('mail.from.address', config('v2board.email_from_address', env('mail.from.address')));
Config::set('mail.from.name', config('v2board.app_name', 'V2Board'));
}
$params = $this->params;
$email = $params['email'];
$subject = $params['subject'];
$params['template_name'] = 'mail.' . config('v2board.email_template', 'default') . '.' . $params['template_name'];
try {
switch ($driver) {
case 'SMTP':
Mail::send(
$params['template_name'],
$params['template_value'],
@ -57,7 +67,23 @@ class SendEmailJob implements ShouldQueue
$message->to($email)->subject($subject);
}
);
} catch (\Exception $e) {
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) {
$error = $e->getMessage();
}
@ -65,7 +91,7 @@ class SendEmailJob implements ShouldQueue
'email' => $params['email'],
'subject' => $params['subject'],
'template_name' => $params['template_name'],
'error' => isset($error) ? $error : NULL
'error' => $error ?? NULL
];
MailLog::create($log);

View File

@ -68,7 +68,9 @@ class ConfigService {
'email_username' => config('v2board.email_username'),
'email_password' => config('v2board.email_password'),
'email_encryption' => config('v2board.email_encryption'),
'email_from_address' => config('v2board.email_from_address')
'email_from_address' => config('v2board.email_from_address'),
'email_postal_host' => config('v2board.email_postal_host'),
'email_postal_key' => config('v2board.email_postal_key'),
],
'telegram' => [
'telegram_bot_enable' => config('v2board.telegram_bot_enable', 0),

View File

@ -22,6 +22,7 @@
"laravel/tinker": "^2.5",
"linfo/linfo": "^4.0",
"php-curl-class/php-curl-class": "^8.6",
"postal/postal": "^2.0",
"stripe/stripe-php": "^7.36.1",
"symfony/yaml": "^4.3",
"paragonie/sodium_compat": "^1.20"