diff --git a/src/app/Jobs/Password/RetentionEmailJob.php b/src/app/Jobs/Password/RetentionEmailJob.php --- a/src/app/Jobs/Password/RetentionEmailJob.php +++ b/src/app/Jobs/Password/RetentionEmailJob.php @@ -53,6 +53,11 @@ */ public function handle() { + if (!$this->user->isLdapReady() || !$this->user->isImapReady()) { + // The account isn't ready for mail delivery + return; + } + // TODO: Should we check if the password didn't update since // the job has been created? diff --git a/src/app/Jobs/PaymentEmail.php b/src/app/Jobs/PaymentEmail.php --- a/src/app/Jobs/PaymentEmail.php +++ b/src/app/Jobs/PaymentEmail.php @@ -80,7 +80,7 @@ list($to, $cc) = \App\Mail\Helper::userEmails($this->controller); - if (!empty($to)) { + if (!empty($to) || !empty($cc)) { $params = [ 'to' => $to, 'cc' => $cc, diff --git a/src/app/Jobs/PaymentMandateDisabledEmail.php b/src/app/Jobs/PaymentMandateDisabledEmail.php --- a/src/app/Jobs/PaymentMandateDisabledEmail.php +++ b/src/app/Jobs/PaymentMandateDisabledEmail.php @@ -67,7 +67,7 @@ list($to, $cc) = \App\Mail\Helper::userEmails($this->controller); - if (!empty($to)) { + if (!empty($to) || !empty($cc)) { $params = [ 'to' => $to, 'cc' => $cc, diff --git a/src/app/Jobs/TrialEndEmail.php b/src/app/Jobs/TrialEndEmail.php --- a/src/app/Jobs/TrialEndEmail.php +++ b/src/app/Jobs/TrialEndEmail.php @@ -56,10 +56,13 @@ */ public function handle() { - \App\Mail\Helper::sendMail( - new TrialEnd($this->account), - $this->account->tenant_id, - ['to' => $this->account->email] - ); + // Skip accounts that aren't ready for mail delivery + if ($this->account->isLdapReady() && $this->account->isImapReady()) { + \App\Mail\Helper::sendMail( + new TrialEnd($this->account), + $this->account->tenant_id, + ['to' => $this->account->email] + ); + } } } diff --git a/src/app/Mail/Helper.php b/src/app/Mail/Helper.php --- a/src/app/Mail/Helper.php +++ b/src/app/Mail/Helper.php @@ -110,12 +110,16 @@ */ public static function userEmails(\App\User $user, bool $external = false): array { - $to = $user->email; + $active = $user->isLdapReady() && $user->isImapReady(); + + // Sending an email to non-(ldap|imap)-ready user will fail, skip it + // (or send to the external email only, when appropriate) + $to = $active ? $user->email : null; $cc = []; // If user has no mailbox entitlement we should not send // the email to his main address, but use external address, if defined - if (!$user->hasSku('mailbox')) { + if ($active && !$user->hasSku('mailbox')) { $to = $user->getSetting('external_email'); } elseif ($external) { $ext_email = $user->getSetting('external_email'); diff --git a/src/tests/Feature/Jobs/Password/RetentionEmailJobTest.php b/src/tests/Feature/Jobs/Password/RetentionEmailJobTest.php --- a/src/tests/Feature/Jobs/Password/RetentionEmailJobTest.php +++ b/src/tests/Feature/Jobs/Password/RetentionEmailJobTest.php @@ -4,6 +4,7 @@ use App\Jobs\Password\RetentionEmailJob; use App\Mail\PasswordExpirationReminder; +use App\User; use Illuminate\Support\Facades\Mail; use Tests\TestCase; @@ -40,7 +41,8 @@ */ public function testHandle() { - $user = $this->getTestUser('PasswordRetention@UserAccount.com'); + $status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; + $user = $this->getTestUser('PasswordRetention@UserAccount.com', ['status' => $status]); $expiresOn = now()->copy()->addDays(7)->toDateString(); Mail::fake(); diff --git a/src/tests/Feature/Jobs/PaymentEmailTest.php b/src/tests/Feature/Jobs/PaymentEmailTest.php --- a/src/tests/Feature/Jobs/PaymentEmailTest.php +++ b/src/tests/Feature/Jobs/PaymentEmailTest.php @@ -7,6 +7,7 @@ use App\Mail\PaymentSuccess; use App\Payment; use App\Providers\PaymentProvider; +use App\User; use Illuminate\Support\Facades\Mail; use Tests\TestCase; @@ -43,7 +44,8 @@ */ public function testHandle() { - $user = $this->getTestUser('PaymentEmail@UserAccount.com'); + $status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; + $user = $this->getTestUser('PaymentEmail@UserAccount.com', ['status' => $status]); $user->setSetting('external_email', 'ext@email.tld'); $wallet = $user->wallets()->first(); diff --git a/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php b/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php --- a/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php +++ b/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php @@ -4,6 +4,7 @@ use App\Jobs\PaymentMandateDisabledEmail; use App\Mail\PaymentMandateDisabled; +use App\User; use Illuminate\Support\Facades\Mail; use Tests\TestCase; @@ -40,7 +41,8 @@ */ public function testHandle() { - $user = $this->getTestUser('PaymentEmail@UserAccount.com'); + $status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; + $user = $this->getTestUser('PaymentEmail@UserAccount.com', ['status' => $status]); $user->setSetting('external_email', 'ext@email.tld'); $wallet = $user->wallets()->first(); diff --git a/src/tests/Feature/Jobs/TrialEndEmailTest.php b/src/tests/Feature/Jobs/TrialEndEmailTest.php --- a/src/tests/Feature/Jobs/TrialEndEmailTest.php +++ b/src/tests/Feature/Jobs/TrialEndEmailTest.php @@ -4,6 +4,7 @@ use App\Jobs\TrialEndEmail; use App\Mail\TrialEnd; +use App\User; use Illuminate\Support\Facades\Mail; use Tests\TestCase; @@ -40,7 +41,8 @@ */ public function testHandle() { - $user = $this->getTestUser('PaymentEmail@UserAccount.com'); + $status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; + $user = $this->getTestUser('PaymentEmail@UserAccount.com', ['status' => $status]); $user->setSetting('external_email', 'ext@email.tld'); Mail::fake(); diff --git a/src/tests/Feature/Jobs/WalletCheckTest.php b/src/tests/Feature/Jobs/WalletCheckTest.php --- a/src/tests/Feature/Jobs/WalletCheckTest.php +++ b/src/tests/Feature/Jobs/WalletCheckTest.php @@ -384,7 +384,8 @@ */ private function prepareTestUser(&$wallet) { - $user = $this->getTestUser('wallet-check@kolabnow.com'); + $status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; + $user = $this->getTestUser('wallet-check@kolabnow.com', ['status' => $status]); $user->setSetting('external_email', 'external@test.com'); $wallet = $user->wallets()->first(); diff --git a/src/tests/Unit/Mail/HelperTest.php b/src/tests/Unit/Mail/HelperTest.php --- a/src/tests/Unit/Mail/HelperTest.php +++ b/src/tests/Unit/Mail/HelperTest.php @@ -3,6 +3,7 @@ namespace Tests\Unit\Mail; use App\Mail\Helper; +use App\User; use Illuminate\Support\Facades\Mail; use Tests\TestCase; @@ -98,7 +99,8 @@ */ public function testUserEmails(): void { - $user = $this->getTestUser('mail-helper-test@kolabnow.com'); + $status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; + $user = $this->getTestUser('mail-helper-test@kolabnow.com', ['status' => $status]); // User with no mailbox and no external email list($to, $cc) = Helper::userEmails($user); @@ -148,5 +150,15 @@ $this->assertSame($user->email, $to); $this->assertSame([], $cc); + + // Use with mailbox, but not ready + $user->setSetting('external_email', 'external@test.com'); + $user->status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY; + $user->save(); + + list($to, $cc) = Helper::userEmails($user, true); + + $this->assertSame(null, $to); + $this->assertSame(['external@test.com'], $cc); } }