initial changings

This commit is contained in:
paulwer 2023-12-18 19:37:13 +01:00
parent 9dd6ead39e
commit 3206e46349
13 changed files with 1623 additions and 1380 deletions

View File

@ -13,5 +13,10 @@ namespace Illuminate\Contracts\Mail
{ {
return true; return true;
} }
public function brevo_config(string $key)
{
return true;
}
} }
} }

View File

@ -229,7 +229,7 @@ class CompanySettings extends BaseSettings
public $require_quote_signature = false; //@TODO ben to confirm public $require_quote_signature = false; //@TODO ben to confirm
//email settings //email settings
public $email_sending_method = 'default'; //enum 'default','gmail','office365' 'client_postmark', 'client_mailgun' //@implemented public $email_sending_method = 'default'; //enum 'default','gmail','office365' 'client_postmark', 'client_mailgun' , 'client_brevo' //@implemented
public $gmail_sending_user_id = '0'; //@implemented public $gmail_sending_user_id = '0'; //@implemented
@ -451,6 +451,12 @@ class CompanySettings extends BaseSettings
public $mailgun_endpoint = 'api.mailgun.net'; //api.eu.mailgun.net public $mailgun_endpoint = 'api.mailgun.net'; //api.eu.mailgun.net
public $brevo_secret = '';
public $brevo_domain = '';
public $brevo_endpoint = 'api.mailgun.net'; //api.eu.mailgun.net
public $auto_bill_standard_invoices = false; public $auto_bill_standard_invoices = false;
public $email_alignment = 'center'; // center , left, right public $email_alignment = 'center'; // center , left, right
@ -505,6 +511,7 @@ class CompanySettings extends BaseSettings
'default_expense_payment_type_id' => 'string', 'default_expense_payment_type_id' => 'string',
'e_invoice_type' => 'string', 'e_invoice_type' => 'string',
'mailgun_endpoint' => 'string', 'mailgun_endpoint' => 'string',
'brevo_endpoint' => 'string',
'client_initiated_payments' => 'bool', 'client_initiated_payments' => 'bool',
'client_initiated_payments_minimum' => 'float', 'client_initiated_payments_minimum' => 'float',
'sync_invoice_quote_columns' => 'bool', 'sync_invoice_quote_columns' => 'bool',
@ -521,6 +528,8 @@ class CompanySettings extends BaseSettings
'postmark_secret' => 'string', 'postmark_secret' => 'string',
'mailgun_secret' => 'string', 'mailgun_secret' => 'string',
'mailgun_domain' => 'string', 'mailgun_domain' => 'string',
'brevo_secret' => 'string',
'brevo_domain' => 'string',
'send_email_on_mark_paid' => 'bool', 'send_email_on_mark_paid' => 'bool',
'vendor_portal_enable_uploads' => 'bool', 'vendor_portal_enable_uploads' => 'bool',
'besr_id' => 'string', 'besr_id' => 'string',
@ -546,7 +555,7 @@ class CompanySettings extends BaseSettings
'use_credits_payment' => 'string', 'use_credits_payment' => 'string',
'recurring_invoice_number_pattern' => 'string', 'recurring_invoice_number_pattern' => 'string',
'recurring_invoice_number_counter' => 'int', 'recurring_invoice_number_counter' => 'int',
'client_portal_under_payment_minimum'=> 'float', 'client_portal_under_payment_minimum' => 'float',
'auto_bill_date' => 'string', 'auto_bill_date' => 'string',
'primary_color' => 'string', 'primary_color' => 'string',
'secondary_color' => 'string', 'secondary_color' => 'string',
@ -842,7 +851,7 @@ class CompanySettings extends BaseSettings
$company_settings = (object) get_class_vars(self::class); $company_settings = (object) get_class_vars(self::class);
foreach ($company_settings as $key => $value) { foreach ($company_settings as $key => $value) {
if (! property_exists($settings, $key)) { if (!property_exists($settings, $key)) {
$settings->{$key} = self::castAttribute($key, $company_settings->{$key}); $settings->{$key} = self::castAttribute($key, $company_settings->{$key});
} }
} }
@ -855,7 +864,7 @@ class CompanySettings extends BaseSettings
* *
* @return stdClass * @return stdClass
*/ */
public static function notificationDefaults() :stdClass public static function notificationDefaults(): stdClass
{ {
$notification = new stdClass; $notification = new stdClass;
$notification->email = []; $notification->email = [];
@ -871,7 +880,7 @@ class CompanySettings extends BaseSettings
* *
* @return stdClass * @return stdClass
*/ */
public static function notificationAdminDefaults() :stdClass public static function notificationAdminDefaults(): stdClass
{ {
$notification = new stdClass; $notification = new stdClass;
$notification->email = []; $notification->email = [];
@ -888,7 +897,7 @@ class CompanySettings extends BaseSettings
* *
* @return stdClass The stdClass of PDF variables * @return stdClass The stdClass of PDF variables
*/ */
public static function getEntityVariableDefaults() :stdClass public static function getEntityVariableDefaults(): stdClass
{ {
$variables = [ $variables = [
'client_details' => [ 'client_details' => [
@ -975,7 +984,7 @@ class CompanySettings extends BaseSettings
'$product.tax', '$product.tax',
'$product.line_total', '$product.line_total',
], ],
'task_columns' =>[ 'task_columns' => [
'$task.service', '$task.service',
'$task.description', '$task.description',
'$task.rate', '$task.rate',

View File

@ -213,7 +213,7 @@ class SettingsData
public bool $show_accept_quote_terms = false; //@TODO ben to confirm public bool $show_accept_quote_terms = false; //@TODO ben to confirm
public string $email_sending_method = 'default'; // enum 'default', 'gmail', 'office365', 'client_postmark', 'client_mailgun' //@implemented public string $email_sending_method = 'default'; // enum 'default', 'gmail', 'office365', 'client_postmark', 'client_mailgun' , 'brevo_mailgun' //@implemented
public string $gmail_sending_user_id = '0'; //@implemented public string $gmail_sending_user_id = '0'; //@implemented
@ -433,6 +433,12 @@ class SettingsData
public string $mailgun_endpoint = 'api.mailgun.net'; // api.eu.mailgun.net public string $mailgun_endpoint = 'api.mailgun.net'; // api.eu.mailgun.net
public string $brevo_secret = '';
public string $brevo_domain = '';
public string $brevo_endpoint = 'api.mailgun.net'; // api.eu.mailgun.net
public bool $auto_bill_standard_invoices = false; public bool $auto_bill_standard_invoices = false;
public string $email_alignment = 'center'; // center, left, right public string $email_alignment = 'center'; // center, left, right
@ -469,8 +475,8 @@ class SettingsData
public function cast(mixed $object) public function cast(mixed $object)
{ {
if(is_array($object)) { if (is_array($object)) {
$object = (object)$object; $object = (object) $object;
} }
if (is_object($object)) { if (is_object($object)) {
@ -478,9 +484,9 @@ class SettingsData
try { try {
settype($object->{$key}, gettype($this->{$key})); settype($object->{$key}, gettype($this->{$key}));
} catch(\Exception | \Error | \Throwable $e) { } catch (\Exception | \Error | \Throwable $e) {
if(property_exists($this, $key)) { if (property_exists($this, $key)) {
$object->{$key} = $this->{$key}; $object->{$key} = $this->{$key};
} else { } else {
unset($object->{$key}); unset($object->{$key});
@ -506,11 +512,11 @@ class SettingsData
public function toObject(): object public function toObject(): object
{ {
return (object)$this->object; return (object) $this->object;
} }
public function toArray(): array public function toArray(): array
{ {
return (array)$this->object; return (array) $this->object;
} }
} }

View File

@ -62,6 +62,9 @@ class NinjaMailerJob implements ShouldQueue
protected $client_mailgun_domain = false; protected $client_mailgun_domain = false;
protected $client_brevo_secret = false;
protected $client_brevo_domain = false;
public function __construct(NinjaMailerObject $nmo, bool $override = false) public function __construct(NinjaMailerObject $nmo, bool $override = false)
{ {
@ -120,8 +123,8 @@ class NinjaMailerJob implements ShouldQueue
//send email //send email
try { try {
nlog("Trying to send to {$this->nmo->to_user->email} ". now()->toDateTimeString()); nlog("Trying to send to {$this->nmo->to_user->email} " . now()->toDateTimeString());
nlog("Using mailer => ". $this->mailer); nlog("Using mailer => " . $this->mailer);
$mailer = Mail::mailer($this->mailer); $mailer = Mail::mailer($this->mailer);
@ -133,23 +136,27 @@ class NinjaMailerJob implements ShouldQueue
$mailer->mailgun_config($this->client_mailgun_secret, $this->client_mailgun_domain, $this->nmo->settings->mailgun_endpoint); $mailer->mailgun_config($this->client_mailgun_secret, $this->client_mailgun_domain, $this->nmo->settings->mailgun_endpoint);
} }
if ($this->client_brevo_secret) {
$mailer->brevo_config($this->client_brevo_secret, $this->client_brevo_domain, $this->nmo->settings->brevo_endpoint);
}
$mailer $mailer
->to($this->nmo->to_user->email) ->to($this->nmo->to_user->email)
->send($this->nmo->mailable); ->send($this->nmo->mailable);
/* Count the amount of emails sent across all the users accounts */ /* Count the amount of emails sent across all the users accounts */
Cache::increment("email_quota".$this->company->account->key); Cache::increment("email_quota" . $this->company->account->key);
LightLogs::create(new EmailSuccess($this->nmo->company->company_key, $this->nmo->mailable->subject)) LightLogs::create(new EmailSuccess($this->nmo->company->company_key, $this->nmo->mailable->subject))
->send(); ->send();
} catch(\Symfony\Component\Mime\Exception\RfcComplianceException $e) { } catch (\Symfony\Component\Mime\Exception\RfcComplianceException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}"); nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail(); $this->fail();
$this->cleanUpMailers(); $this->cleanUpMailers();
$this->logMailError($e->getMessage(), $this->company->clients()->first()); $this->logMailError($e->getMessage(), $this->company->clients()->first());
return; return;
} catch(\Symfony\Component\Mime\Exception\LogicException $e) { } catch (\Symfony\Component\Mime\Exception\LogicException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}"); nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail(); $this->fail();
$this->cleanUpMailers(); $this->cleanUpMailers();
@ -204,7 +211,7 @@ class NinjaMailerJob implements ShouldQueue
/* Releasing immediately does not add in the backoff */ /* Releasing immediately does not add in the backoff */
sleep(rand(0, 3)); sleep(rand(0, 3));
$this->release($this->backoff()[$this->attempts()-1]); $this->release($this->backoff()[$this->attempts() - 1]);
} }
$this->nmo = null; $this->nmo = null;
@ -272,6 +279,10 @@ class NinjaMailerJob implements ShouldQueue
$this->mailer = 'mailgun'; $this->mailer = 'mailgun';
$this->setMailgunMailer(); $this->setMailgunMailer();
return $this; return $this;
case 'client_brevo':
$this->mailer = 'brevo';
$this->setBrevoMailer();
return $this;
default: default:
break; break;
@ -322,6 +333,10 @@ class NinjaMailerJob implements ShouldQueue
$this->client_mailgun_domain = false; $this->client_mailgun_domain = false;
$this->client_brevo_secret = false;
$this->client_brevo_domain = false;
//always dump the drivers to prevent reuse //always dump the drivers to prevent reuse
app('mail.manager')->forgetMailers(); app('mail.manager')->forgetMailers();
} }
@ -385,6 +400,30 @@ class NinjaMailerJob implements ShouldQueue
->from($sending_email, $sending_user); ->from($sending_email, $sending_user);
} }
/**
* Configures Brevo using client supplied secret
* as the Mailer
*/
private function setBrevoMailer()
{
if (strlen($this->nmo->settings->brevo_secret) > 2 && strlen($this->nmo->settings->brevo_domain) > 2) {
$this->client_brevo_secret = $this->nmo->settings->brevo_secret;
$this->client_brevo_domain = $this->nmo->settings->brevo_domain;
} else {
$this->nmo->settings->email_sending_method = 'default';
return $this->setMailDriver();
}
$user = $this->resolveSendingUser();
$sending_email = (isset($this->nmo->settings->custom_sending_email) && stripos($this->nmo->settings->custom_sending_email, "@")) ? $this->nmo->settings->custom_sending_email : $user->email;
$sending_user = (isset($this->nmo->settings->email_from_name) && strlen($this->nmo->settings->email_from_name) > 2) ? $this->nmo->settings->email_from_name : $user->name();
$this->nmo
->mailable
->from($sending_email, $sending_user);
}
/** /**
* Configures Postmark using client supplied secret * Configures Postmark using client supplied secret
* as the Mailer * as the Mailer
@ -459,7 +498,7 @@ class NinjaMailerJob implements ShouldQueue
} }
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token)); $google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
} catch(\Exception $e) { } catch (\Exception $e) {
$this->logMailError('Gmail Token Invalid', $this->company->clients()->first()); $this->logMailError('Gmail Token Invalid', $this->company->clients()->first());
$this->nmo->settings->email_sending_method = 'default'; $this->nmo->settings->email_sending_method = 'default';
return $this->setMailDriver(); return $this->setMailDriver();
@ -507,7 +546,7 @@ class NinjaMailerJob implements ShouldQueue
private function preFlightChecksFail(): bool private function preFlightChecksFail(): bool
{ {
/* Always send regardless */ /* Always send regardless */
if($this->override) { if ($this->override) {
return false; return false;
} }
@ -527,7 +566,7 @@ class NinjaMailerJob implements ShouldQueue
} }
/* GMail users are uncapped */ /* GMail users are uncapped */
if (Ninja::isHosted() && (in_array($this->nmo->settings->email_sending_method, ['gmail', 'office365', 'client_postmark', 'client_mailgun']))) { if (Ninja::isHosted() && (in_array($this->nmo->settings->email_sending_method, ['gmail', 'office365', 'client_postmark', 'client_mailgun', 'client_brevo']))) {
return false; return false;
} }
@ -570,7 +609,7 @@ class NinjaMailerJob implements ShouldQueue
* @param \App\Models\User | \App\Models\Client | null $recipient_object * @param \App\Models\User | \App\Models\Client | null $recipient_object
* @return void * @return void
*/ */
private function logMailError($errors, $recipient_object) :void private function logMailError($errors, $recipient_object): void
{ {
(new SystemLogger( (new SystemLogger(
$errors, $errors,
@ -611,8 +650,8 @@ class NinjaMailerJob implements ShouldQueue
$token = json_decode($guzzle->post($url, [ $token = json_decode($guzzle->post($url, [
'form_params' => [ 'form_params' => [
'client_id' => config('ninja.o365.client_id') , 'client_id' => config('ninja.o365.client_id'),
'client_secret' => config('ninja.o365.client_secret') , 'client_secret' => config('ninja.o365.client_secret'),
'scope' => 'email Mail.Send offline_access profile User.Read openid', 'scope' => 'email Mail.Send offline_access profile User.Read openid',
'grant_type' => 'refresh_token', 'grant_type' => 'refresh_token',
'refresh_token' => $user->oauth_user_refresh_token 'refresh_token' => $user->oauth_user_refresh_token

View File

@ -100,7 +100,6 @@ class AppServiceProvider extends ServiceProvider
return $this; return $this;
}); });
Mailer::macro('mailgun_config', function (string $secret, string $domain, string $endpoint = 'api.mailgun.net') { Mailer::macro('mailgun_config', function (string $secret, string $domain, string $endpoint = 'api.mailgun.net') {
// @phpstan-ignore /** @phpstan-ignore-next-line **/ // @phpstan-ignore /** @phpstan-ignore-next-line **/
Mailer::setSymfonyTransport(app('mail.manager')->createSymfonyTransport([ Mailer::setSymfonyTransport(app('mail.manager')->createSymfonyTransport([
@ -114,6 +113,19 @@ class AppServiceProvider extends ServiceProvider
return $this; return $this;
}); });
Mailer::macro('brevo_config', function (string $secret, string $domain, string $endpoint = 'api.mailgun.net') {
// @phpstan-ignore /** @phpstan-ignore-next-line **/
Mailer::setSymfonyTransport(app('mail.manager')->createSymfonyTransport([
'transport' => 'brevo',
'secret' => $secret,
'domain' => $domain,
'endpoint' => $endpoint,
'scheme' => config('services.brevo.scheme'),
]));
return $this;
});
} }
public function register(): void public function register(): void

View File

@ -55,6 +55,12 @@ class AdminEmail implements ShouldQueue
protected ?string $client_mailgun_endpoint = null; protected ?string $client_mailgun_endpoint = null;
protected ?string $client_brevo_secret = null;
protected ?string $client_brevo_domain = null;
protected ?string $client_brevo_endpoint = null;
private string $mailer = 'default'; private string $mailer = 'default';
public Mailable $mailable; public Mailable $mailable;
@ -133,24 +139,28 @@ class AdminEmail implements ShouldQueue
$mailer->mailgun_config($this->client_mailgun_secret, $this->client_mailgun_domain, $this->client_mailgun_endpoint); $mailer->mailgun_config($this->client_mailgun_secret, $this->client_mailgun_domain, $this->client_mailgun_endpoint);
} }
if ($this->client_brevo_secret) {
$mailer->brevo_config($this->client_brevo_secret, $this->client_brevo_domain, $this->client_brevo_endpoint);
}
/* Attempt the send! */ /* Attempt the send! */
try { try {
nlog("Using mailer => ". $this->mailer. " ". now()->toDateTimeString()); nlog("Using mailer => " . $this->mailer . " " . now()->toDateTimeString());
$mailer->send($this->mailable); $mailer->send($this->mailable);
Cache::increment("email_quota".$this->company->account->key); Cache::increment("email_quota" . $this->company->account->key);
LightLogs::create(new EmailSuccess($this->company->company_key, $this->mailable->subject)) LightLogs::create(new EmailSuccess($this->company->company_key, $this->mailable->subject))
->send(); ->send();
} catch(\Symfony\Component\Mime\Exception\RfcComplianceException $e) { } catch (\Symfony\Component\Mime\Exception\RfcComplianceException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}"); nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail(); $this->fail();
$this->cleanUpMailers(); $this->cleanUpMailers();
$this->logMailError($e->getMessage(), $this->company->clients()->first()); $this->logMailError($e->getMessage(), $this->company->clients()->first());
return; return;
} catch(\Symfony\Component\Mime\Exception\LogicException $e) { } catch (\Symfony\Component\Mime\Exception\LogicException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}"); nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail(); $this->fail();
$this->cleanUpMailers(); $this->cleanUpMailers();
@ -202,7 +212,7 @@ class AdminEmail implements ShouldQueue
sleep(rand(0, 3)); sleep(rand(0, 3));
$this->release($this->backoff()[$this->attempts()-1]); $this->release($this->backoff()[$this->attempts() - 1]);
$message = null; $message = null;
} }
@ -220,7 +230,7 @@ class AdminEmail implements ShouldQueue
public function preFlightChecksFail(): bool public function preFlightChecksFail(): bool
{ {
/* Always send if disabled */ /* Always send if disabled */
if($this->override) { if ($this->override) {
return false; return false;
} }
@ -333,6 +343,10 @@ class AdminEmail implements ShouldQueue
$this->mailer = 'mailgun'; $this->mailer = 'mailgun';
$this->setMailgunMailer(); $this->setMailgunMailer();
return $this; return $this;
case 'client_brevo':
$this->mailer = 'brevo';
$this->setBrevoMailer();
return $this;
default: default:
$this->mailer = config('mail.default'); $this->mailer = config('mail.default');
@ -386,6 +400,12 @@ class AdminEmail implements ShouldQueue
$this->client_mailgun_endpoint = null; $this->client_mailgun_endpoint = null;
$this->client_brevo_secret = null;
$this->client_brevo_domain = null;
$this->client_brevo_endpoint = null;
//always dump the drivers to prevent reuse //always dump the drivers to prevent reuse
app('mail.manager')->forgetMailers(); app('mail.manager')->forgetMailers();
} }
@ -450,6 +470,30 @@ class AdminEmail implements ShouldQueue
$this->mailable $this->mailable
->from($sending_email, $sending_user); ->from($sending_email, $sending_user);
} }
/**
* Configures Brevo using client supplied secret
* as the Mailer
*/
private function setBrevoMailer()
{
if (strlen($this->email_object->settings->brevo_secret) > 2 && strlen($this->email_object->settings->brevo_domain) > 2) {
$this->client_brevo_secret = $this->email_object->settings->brevo_secret;
$this->client_brevo_domain = $this->email_object->settings->brevo_domain;
$this->client_brevo_endpoint = $this->email_object->settings->brevo_endpoint;
} else {
$this->email_object->settings->email_sending_method = 'default';
return $this->setMailDriver();
}
$user = $this->resolveSendingUser();
$sending_email = (isset($this->email_object->settings->custom_sending_email) && stripos($this->email_object->settings->custom_sending_email, "@")) ? $this->email_object->settings->custom_sending_email : $user->email;
$sending_user = (isset($this->email_object->settings->email_from_name) && strlen($this->email_object->settings->email_from_name) > 2) ? $this->email_object->settings->email_from_name : $user->name();
$this->mailable
->from($sending_email, $sending_user);
}
/** /**
* Configures Postmark using client supplied secret * Configures Postmark using client supplied secret
@ -523,7 +567,7 @@ class AdminEmail implements ShouldQueue
} }
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token)); $google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
} catch(\Exception $e) { } catch (\Exception $e) {
$this->logMailError('Gmail Token Invalid', $this->company->clients()->first()); $this->logMailError('Gmail Token Invalid', $this->company->clients()->first());
$this->email_object->settings->email_sending_method = 'default'; $this->email_object->settings->email_sending_method = 'default';
return $this->setMailDriver(); return $this->setMailDriver();
@ -567,7 +611,7 @@ class AdminEmail implements ShouldQueue
* @param null | \App\Models\Client $recipient_object * @param null | \App\Models\Client $recipient_object
* @return void * @return void
*/ */
private function logMailError($errors, $recipient_object) :void private function logMailError($errors, $recipient_object): void
{ {
(new SystemLogger( (new SystemLogger(
$errors, $errors,
@ -604,8 +648,8 @@ class AdminEmail implements ShouldQueue
$token = json_decode($guzzle->post($url, [ $token = json_decode($guzzle->post($url, [
'form_params' => [ 'form_params' => [
'client_id' => config('ninja.o365.client_id') , 'client_id' => config('ninja.o365.client_id'),
'client_secret' => config('ninja.o365.client_secret') , 'client_secret' => config('ninja.o365.client_secret'),
'scope' => 'email Mail.Send offline_access profile User.Read openid', 'scope' => 'email Mail.Send offline_access profile User.Read openid',
'grant_type' => 'refresh_token', 'grant_type' => 'refresh_token',
'refresh_token' => $user->oauth_user_refresh_token 'refresh_token' => $user->oauth_user_refresh_token

View File

@ -60,6 +60,12 @@ class Email implements ShouldQueue
protected ?string $client_mailgun_endpoint = null; protected ?string $client_mailgun_endpoint = null;
protected ?string $client_brevo_secret = null;
protected ?string $client_brevo_domain = null;
protected ?string $client_brevo_endpoint = null;
private string $mailer = 'default'; private string $mailer = 'default';
public Mailable $mailable; public Mailable $mailable;
@ -245,24 +251,28 @@ class Email implements ShouldQueue
$mailer->mailgun_config($this->client_mailgun_secret, $this->client_mailgun_domain, $this->client_mailgun_endpoint); $mailer->mailgun_config($this->client_mailgun_secret, $this->client_mailgun_domain, $this->client_mailgun_endpoint);
} }
if ($this->client_brevo_secret) {
$mailer->brevo_config($this->client_brevo_secret, $this->client_brevo_domain, $this->client_brevo_endpoint);
}
/* Attempt the send! */ /* Attempt the send! */
try { try {
nlog("Using mailer => ". $this->mailer. " ". now()->toDateTimeString()); nlog("Using mailer => " . $this->mailer . " " . now()->toDateTimeString());
$mailer->send($this->mailable); $mailer->send($this->mailable);
Cache::increment("email_quota".$this->company->account->key); Cache::increment("email_quota" . $this->company->account->key);
LightLogs::create(new EmailSuccess($this->company->company_key, $this->mailable->subject)) LightLogs::create(new EmailSuccess($this->company->company_key, $this->mailable->subject))
->send(); ->send();
} catch(\Symfony\Component\Mime\Exception\RfcComplianceException $e) { } catch (\Symfony\Component\Mime\Exception\RfcComplianceException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}"); nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail(); $this->fail();
$this->cleanUpMailers(); $this->cleanUpMailers();
$this->logMailError($e->getMessage(), $this->company->clients()->first()); $this->logMailError($e->getMessage(), $this->company->clients()->first());
return; return;
} catch(\Symfony\Component\Mime\Exception\LogicException $e) { } catch (\Symfony\Component\Mime\Exception\LogicException $e) {
nlog("Mailer failed with a Logic Exception {$e->getMessage()}"); nlog("Mailer failed with a Logic Exception {$e->getMessage()}");
$this->fail(); $this->fail();
$this->cleanUpMailers(); $this->cleanUpMailers();
@ -333,7 +343,7 @@ class Email implements ShouldQueue
sleep(rand(0, 3)); sleep(rand(0, 3));
$this->release($this->backoff()[$this->attempts()-1]); $this->release($this->backoff()[$this->attempts() - 1]);
$message = null; $message = null;
} }
@ -351,7 +361,7 @@ class Email implements ShouldQueue
public function preFlightChecksFail(): bool public function preFlightChecksFail(): bool
{ {
/* Always send if disabled */ /* Always send if disabled */
if($this->override) { if ($this->override) {
return false; return false;
} }
@ -436,7 +446,7 @@ class Email implements ShouldQueue
return true; return true;
} }
if($address_object->name == " " || $address_object->name == "") { if ($address_object->name == " " || $address_object->name == "") {
return true; return true;
} }
} }
@ -472,6 +482,10 @@ class Email implements ShouldQueue
$this->mailer = 'mailgun'; $this->mailer = 'mailgun';
$this->setMailgunMailer(); $this->setMailgunMailer();
return $this; return $this;
case 'client_brevo':
$this->mailer = 'brevo';
$this->setBrevoMailer();
return $this;
default: default:
$this->mailer = config('mail.default'); $this->mailer = config('mail.default');
@ -525,6 +539,12 @@ class Email implements ShouldQueue
$this->client_mailgun_endpoint = null; $this->client_mailgun_endpoint = null;
$this->client_brevo_secret = null;
$this->client_brevo_domain = null;
$this->client_brevo_endpoint = null;
//always dump the drivers to prevent reuse //always dump the drivers to prevent reuse
app('mail.manager')->forgetMailers(); app('mail.manager')->forgetMailers();
} }
@ -589,6 +609,30 @@ class Email implements ShouldQueue
$this->mailable $this->mailable
->from($sending_email, $sending_user); ->from($sending_email, $sending_user);
} }
/**
* Configures Brevo using client supplied secret
* as the Mailer
*/
private function setBrevoMailer()
{
if (strlen($this->email_object->settings->brevo_secret) > 2 && strlen($this->email_object->settings->brevo_domain) > 2) {
$this->client_brevo_secret = $this->email_object->settings->brevo_secret;
$this->client_brevo_domain = $this->email_object->settings->brevo_domain;
$this->client_brevo_endpoint = $this->email_object->settings->brevo_endpoint;
} else {
$this->email_object->settings->email_sending_method = 'default';
return $this->setMailDriver();
}
$user = $this->resolveSendingUser();
$sending_email = (isset($this->email_object->settings->custom_sending_email) && stripos($this->email_object->settings->custom_sending_email, "@")) ? $this->email_object->settings->custom_sending_email : $user->email;
$sending_user = (isset($this->email_object->settings->email_from_name) && strlen($this->email_object->settings->email_from_name) > 2) ? $this->email_object->settings->email_from_name : $user->name();
$this->mailable
->from($sending_email, $sending_user);
}
/** /**
* Configures Postmark using client supplied secret * Configures Postmark using client supplied secret
@ -662,7 +706,7 @@ class Email implements ShouldQueue
} }
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token)); $google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
} catch(\Exception $e) { } catch (\Exception $e) {
$this->logMailError('Gmail Token Invalid', $this->company->clients()->first()); $this->logMailError('Gmail Token Invalid', $this->company->clients()->first());
$this->email_object->settings->email_sending_method = 'default'; $this->email_object->settings->email_sending_method = 'default';
return $this->setMailDriver(); return $this->setMailDriver();
@ -706,7 +750,7 @@ class Email implements ShouldQueue
* @param null | \App\Models\Client $recipient_object * @param null | \App\Models\Client $recipient_object
* @return void * @return void
*/ */
private function logMailError($errors, $recipient_object) :void private function logMailError($errors, $recipient_object): void
{ {
(new SystemLogger( (new SystemLogger(
$errors, $errors,
@ -743,8 +787,8 @@ class Email implements ShouldQueue
$token = json_decode($guzzle->post($url, [ $token = json_decode($guzzle->post($url, [
'form_params' => [ 'form_params' => [
'client_id' => config('ninja.o365.client_id') , 'client_id' => config('ninja.o365.client_id'),
'client_secret' => config('ninja.o365.client_secret') , 'client_secret' => config('ninja.o365.client_secret'),
'scope' => 'email Mail.Send offline_access profile User.Read openid', 'scope' => 'email Mail.Send offline_access profile User.Read openid',
'grant_type' => 'refresh_token', 'grant_type' => 'refresh_token',
'refresh_token' => $user->oauth_user_refresh_token 'refresh_token' => $user->oauth_user_refresh_token

View File

@ -91,6 +91,7 @@
"sprain/swiss-qr-bill": "^4.3", "sprain/swiss-qr-bill": "^4.3",
"square/square": "30.0.0.*", "square/square": "30.0.0.*",
"stripe/stripe-php": "^12", "stripe/stripe-php": "^12",
"symfony/brevo-mailer": "^7.0",
"symfony/http-client": "^6.0", "symfony/http-client": "^6.0",
"symfony/mailgun-mailer": "^6.1", "symfony/mailgun-mailer": "^6.1",
"symfony/postmark-mailer": "^6.1", "symfony/postmark-mailer": "^6.1",

73
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "28b57fe6eac3d71c607125cda9a6a537", "content-hash": "ba57ee16621201bac10def4422ba9161",
"packages": [ "packages": [
{ {
"name": "afosto/yaac", "name": "afosto/yaac",
@ -10716,6 +10716,75 @@
}, },
"time": "2023-10-16T18:04:12+00:00" "time": "2023-10-16T18:04:12+00:00"
}, },
{
"name": "symfony/brevo-mailer",
"version": "v7.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/brevo-mailer.git",
"reference": "83db87e0f44653cd40aeef54a2f57ab6bfccadfe"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/brevo-mailer/zipball/83db87e0f44653cd40aeef54a2f57ab6bfccadfe",
"reference": "83db87e0f44653cd40aeef54a2f57ab6bfccadfe",
"shasum": ""
},
"require": {
"php": ">=8.1",
"symfony/mailer": "^5.4.21|^6.2.7|^7.0"
},
"conflict": {
"symfony/mime": "<6.2"
},
"require-dev": {
"symfony/http-client": "^6.3|^7.0",
"symfony/webhook": "^6.3|^7.0"
},
"type": "symfony-mailer-bridge",
"autoload": {
"psr-4": {
"Symfony\\Component\\Mailer\\Bridge\\Brevo\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Pierre Tanguy",
"homepage": "https://github.com/petanguy"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Brevo Mailer Bridge",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/brevo-mailer/tree/v7.0.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2023-11-06T17:20:05+00:00"
},
{ {
"name": "symfony/console", "name": "symfony/console",
"version": "v6.4.1", "version": "v6.4.1",
@ -18046,5 +18115,5 @@
"platform-dev": { "platform-dev": {
"php": "^8.1|^8.2" "php": "^8.1|^8.2"
}, },
"plugin-api-version": "2.3.0" "plugin-api-version": "2.6.0"
} }

View File

@ -28,7 +28,7 @@ return [
| sending an e-mail. You will specify which one you are using for your | sending an e-mail. You will specify which one you are using for your
| mailers below. You are free to add additional mailers as required. | mailers below. You are free to add additional mailers as required.
| |
| Supported: "smtp", "sendmail", "mailgun", "ses", | Supported: "smtp", "sendmail", "mailgun", "brevo", "ses",
| "postmark", "log", "array", "failover" | "postmark", "log", "array", "failover"
| |
*/ */
@ -54,6 +54,10 @@ return [
'transport' => 'mailgun', 'transport' => 'mailgun',
], ],
'brevo' => [
'transport' => 'brevo',
],
'postmark' => [ 'postmark' => [
'transport' => 'postmark', 'transport' => 'postmark',
], ],

View File

@ -12,7 +12,7 @@ return [
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| This file is for storing the credentials for third party services such | This file is for storing the credentials for third party services such
| as Mailgun, Postmark, AWS and more. This file provides the de facto | as Mailgun, Brevo, Postmark, AWS and more. This file provides the de facto
| location for this type of information, allowing packages to have | location for this type of information, allowing packages to have
| a conventional file to locate the various service credentials. | a conventional file to locate the various service credentials.
| |
@ -25,6 +25,13 @@ return [
'scheme' => 'https', 'scheme' => 'https',
], ],
'brevo' => [
'domain' => env('BREVO_DOMAIN', ''),
'secret' => env('BREVO_SECRET', ''),
'endpoint' => env('BREVO_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
'postmark' => [ 'postmark' => [
'token' => env('POSTMARK_SECRET', ''), 'token' => env('POSTMARK_SECRET', ''),
], ],

View File

@ -2237,6 +2237,8 @@ $lang = array(
'encryption' => 'Encryption', 'encryption' => 'Encryption',
'mailgun_domain' => 'Mailgun Domain', 'mailgun_domain' => 'Mailgun Domain',
'mailgun_private_key' => 'Mailgun Private Key', 'mailgun_private_key' => 'Mailgun Private Key',
'brevo_domain' => 'Brevo Domain',
'brevo_private_key' => 'Brevo Private Key',
'send_test_email' => 'Send test email', 'send_test_email' => 'Send test email',
'select_label' => 'Select Label', 'select_label' => 'Select Label',
'label' => 'Label', 'label' => 'Label',
@ -4891,6 +4893,7 @@ $lang = array(
'email_alignment' => 'Email Alignment', 'email_alignment' => 'Email Alignment',
'pdf_preview_location' => 'PDF Preview Location', 'pdf_preview_location' => 'PDF Preview Location',
'mailgun' => 'Mailgun', 'mailgun' => 'Mailgun',
'brevo' => 'Brevo',
'postmark' => 'Postmark', 'postmark' => 'Postmark',
'microsoft' => 'Microsoft', 'microsoft' => 'Microsoft',
'click_plus_to_create_record' => 'Click + to create a record', 'click_plus_to_create_record' => 'Click + to create a record',

File diff suppressed because it is too large Load Diff