1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Services/Email/EmailService.php

164 lines
4.8 KiB
PHP
Raw Normal View History

2023-01-15 04:44:23 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2023-01-15 04:44:23 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Email;
use App\Models\Company;
2023-01-15 11:16:10 +01:00
use App\Utils\Ninja;
2023-01-15 04:44:23 +01:00
use Illuminate\Mail\Mailable;
class EmailService
{
2023-01-15 11:16:10 +01:00
/**
* Used to flag whether we force send the email regardless
2023-02-16 02:36:09 +01:00
*
2023-01-15 11:16:10 +01:00
* @var bool $override;
*/
2023-01-15 04:44:23 +01:00
protected bool $override;
public Mailable $mailable;
2023-02-16 02:36:09 +01:00
public function __construct(public EmailObject $email_object, public Company $company)
{
}
2023-01-15 04:44:23 +01:00
2023-01-15 11:16:10 +01:00
/**
* Sends the email via a dispatched job
* @param boolean $override Whether the email should send regardless
* @return void
*/
2023-01-15 04:44:23 +01:00
public function send($override = false) :void
{
$this->override = $override;
$this->setDefaults()
->updateMailable()
->email();
}
2023-01-15 11:16:10 +01:00
public function sendNow($force = false) :void
2023-01-15 04:44:23 +01:00
{
$this->setDefaults()
->updateMailable()
2023-01-15 11:16:10 +01:00
->email($force);
2023-01-15 04:44:23 +01:00
}
private function email($force = false): void
{
2023-02-16 02:36:09 +01:00
if ($force) {
2023-01-15 04:44:23 +01:00
(new EmailMailer($this, $this->mailable))->handle();
2023-02-16 02:36:09 +01:00
} else {
2023-01-15 04:44:23 +01:00
EmailMailer::dispatch($this, $this->mailable)->delay(2);
2023-02-16 02:36:09 +01:00
}
2023-01-15 04:44:23 +01:00
}
private function setDefaults(): self
{
$defaults = new EmailDefaults($this, $this->email_object);
$defaults->run();
return $this;
}
private function updateMailable()
{
$this->mailable = new EmailMailable($this->email_object);
return $this;
}
2023-01-15 11:16:10 +01:00
/**
2023-02-16 02:36:09 +01:00
* On the hosted platform we scan all outbound email for
2023-01-15 11:16:10 +01:00
* spam. This sequence processes the filters we use on all
* emails.
2023-02-16 02:36:09 +01:00
*
2023-01-15 11:16:10 +01:00
* @return bool
*/
public function preFlightChecksFail(): bool
2023-01-15 04:57:24 +01:00
{
2023-01-15 11:16:10 +01:00
/* If we are migrating data we don't want to fire any emails */
2023-02-16 02:36:09 +01:00
if ($this->company->is_disabled && !$this->override) {
2023-01-15 11:16:10 +01:00
return true;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
2023-02-16 02:36:09 +01:00
if (Ninja::isSelfHost()) {
2023-01-17 02:21:36 +01:00
return false;
2023-02-16 02:36:09 +01:00
}
2023-01-17 02:21:36 +01:00
2023-01-15 11:16:10 +01:00
/* To handle spam users we drop all emails from flagged accounts */
2023-02-16 02:36:09 +01:00
if ($this->company->account && $this->company->account->is_flagged) {
2023-01-15 11:16:10 +01:00
return true;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
/* On the hosted platform we set default contacts a @example.com email address - we shouldn't send emails to these types of addresses */
2023-02-16 02:36:09 +01:00
if ($this->hasInValidEmails()) {
2023-01-15 11:16:10 +01:00
return true;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
/* GMail users are uncapped */
2023-02-16 02:36:09 +01:00
if (in_array($this->email_object->settings->email_sending_method, ['gmail', 'office365', 'client_postmark', 'client_mailgun'])) {
2023-01-15 11:16:10 +01:00
return false;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
/* On the hosted platform, if the user is over the email quotas, we do not send the email. */
2023-02-16 02:36:09 +01:00
if ($this->company->account && $this->company->account->emailQuotaExceeded()) {
2023-01-15 11:16:10 +01:00
return true;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
/* If the account is verified, we allow emails to flow */
2023-02-16 02:36:09 +01:00
if ($this->company->account && $this->company->account->is_verified_account) {
2023-01-15 11:16:10 +01:00
//11-01-2022
/* Continue to analyse verified accounts in case they later start sending poor quality emails*/
// if(class_exists(\Modules\Admin\Jobs\Account\EmailQuality::class))
// (new \Modules\Admin\Jobs\Account\EmailQuality($this->nmo, $this->company))->run();
return false;
}
/* On the hosted platform if the user has not verified their account we fail here - but still check what they are trying to send! */
2023-02-16 02:36:09 +01:00
if ($this->company->account && !$this->company->account->account_sms_verified) {
if (class_exists(\Modules\Admin\Jobs\Account\EmailFilter::class)) {
2023-01-15 11:16:10 +01:00
return (new \Modules\Admin\Jobs\Account\EmailFilter($this->email_object, $this->company))->run();
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
return true;
}
/* On the hosted platform we actively scan all outbound emails to ensure outbound email quality remains high */
2023-02-16 02:36:09 +01:00
if (class_exists(\Modules\Admin\Jobs\Account\EmailFilter::class)) {
2023-01-15 11:16:10 +01:00
return (new \Modules\Admin\Jobs\Account\EmailFilter($this->email_object, $this->company))->run();
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
return false;
2023-01-15 04:57:24 +01:00
}
2023-01-17 02:21:36 +01:00
private function hasInValidEmails(): bool
2023-01-15 11:16:10 +01:00
{
2023-02-16 02:36:09 +01:00
foreach ($this->email_object->to as $address_object) {
if (strpos($address_object->address, '@example.com') !== false) {
2023-01-15 11:16:10 +01:00
return true;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
2023-02-16 02:36:09 +01:00
if (!str_contains($address_object->address, "@")) {
2023-01-15 11:16:10 +01:00
return true;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
2023-02-16 02:36:09 +01:00
if ($address_object->address == " ") {
2023-01-17 02:21:36 +01:00
return true;
2023-02-16 02:36:09 +01:00
}
2023-01-15 11:16:10 +01:00
}
return false;
}
2023-02-16 02:36:09 +01:00
}