From 624334570fc3d8a11bfd697583b40fa28442f834 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Sun, 14 Jul 2019 14:18:15 +0100 Subject: [PATCH] #124: Updated the user self-registration required and user self-activated e-mails to be notifications so they can be queued. --- app/Console/Commands/SendEmailsCommand.php | 8 +-- .../Controllers/Auth/RegisterController.php | 9 ++-- app/Mail/TestMailConfig.php | 3 ++ app/Mail/UserActivationRequired.php | 4 +- app/Mail/UserSelfActivated.php | 3 +- .../DatabaseEmailNotification.php | 53 +++++++++++++++++++ app/Notifications/ResetPassword.php | 38 ++----------- app/Notifications/UserActivationRequired.php | 39 ++++++++++++++ app/Notifications/UserSelfActivated.php | 42 +++++++++++++++ app/Traits/ActivatesUsers.php | 5 +- 10 files changed, 153 insertions(+), 51 deletions(-) create mode 100644 app/Notifications/DatabaseEmailNotification.php create mode 100644 app/Notifications/UserActivationRequired.php create mode 100644 app/Notifications/UserSelfActivated.php diff --git a/app/Console/Commands/SendEmailsCommand.php b/app/Console/Commands/SendEmailsCommand.php index 153a7eb..a26ea22 100644 --- a/app/Console/Commands/SendEmailsCommand.php +++ b/app/Console/Commands/SendEmailsCommand.php @@ -6,9 +6,7 @@ use App\EmailLog; use App\Facade\UserConfig; use App\Http\Middleware\GlobalConfiguration; use Illuminate\Console\Command; -use Illuminate\Mail\Mailer; use Illuminate\Mail\Message; -use Illuminate\Support\Facades\Mail; class SendEmailsCommand extends Command { @@ -40,8 +38,6 @@ class SendEmailsCommand extends Command public function __construct() { parent::__construct(); - - GlobalConfiguration::updateMailConfig(); } /** @@ -56,6 +52,10 @@ class SendEmailsCommand extends Command $this->output->error('E-mail queueing is not enabled. E-mails are being sent immediately.'); } + $this->output->writeln('Setting mail configuration'); + + GlobalConfiguration::updateMailConfig(); + $this->output->writeln('E-mail queue runner started'); while (true) diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 68b1f8d..99b3e69 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -6,13 +6,12 @@ use App\Facade\Theme; use App\Facade\UserConfig; use App\Helpers\MiscHelper; use App\Helpers\RecaptchaHelper; -use App\Mail\UserActivationRequired; +use App\Http\Controllers\Controller; +use App\Notifications\UserActivationRequired; use App\Traits\ActivatesUsers; use App\User; -use Illuminate\Http\Request; -use Illuminate\Support\Facades\Mail; -use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\RegistersUsers; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class RegisterController extends Controller @@ -135,7 +134,7 @@ class RegisterController extends Controller else { // Send activation e-mail - Mail::to($user)->send(new UserActivationRequired($user)); + $user->notify(new UserActivationRequired()); $request->session()->flash('info', trans('auth.activation_required_message')); } diff --git a/app/Mail/TestMailConfig.php b/app/Mail/TestMailConfig.php index 86df647..ff3dee8 100644 --- a/app/Mail/TestMailConfig.php +++ b/app/Mail/TestMailConfig.php @@ -8,6 +8,9 @@ use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +/** + * NOTE: This does not need converting to a notification. It should always be sent immediately and not queued. + */ class TestMailConfig extends Mailable { use Queueable, SerializesModels; diff --git a/app/Mail/UserActivationRequired.php b/app/Mail/UserActivationRequired.php index 9b94c0e..3aab52f 100644 --- a/app/Mail/UserActivationRequired.php +++ b/app/Mail/UserActivationRequired.php @@ -6,11 +6,9 @@ use App\Facade\Theme; use App\Facade\UserConfig; use App\User; use Illuminate\Bus\Queueable; -use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; -use Illuminate\Contracts\Queue\ShouldQueue; -class UserActivationRequired extends Mailable +class UserActivationRequired extends MailableBase { use Queueable, SerializesModels; diff --git a/app/Mail/UserSelfActivated.php b/app/Mail/UserSelfActivated.php index cca206b..780981f 100644 --- a/app/Mail/UserSelfActivated.php +++ b/app/Mail/UserSelfActivated.php @@ -6,10 +6,9 @@ use App\Facade\Theme; use App\Facade\UserConfig; use App\User; use Illuminate\Bus\Queueable; -use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; -class UserSelfActivated extends Mailable +class UserSelfActivated extends MailableBase { use Queueable, SerializesModels; diff --git a/app/Notifications/DatabaseEmailNotification.php b/app/Notifications/DatabaseEmailNotification.php new file mode 100644 index 0000000..4ded180 --- /dev/null +++ b/app/Notifications/DatabaseEmailNotification.php @@ -0,0 +1,53 @@ +toMail($notifiable)->buildEmailLog(); + } + + protected function setPropertiesOnMailable(Mailable $mailable, $notifiable) + { + // Set to and from properties accordingly + $mailable->from(UserConfig::get('sender_address'), UserConfig::get('sender_name')); + $mailable->to($notifiable->email, $notifiable->name); + } +} \ No newline at end of file diff --git a/app/Notifications/ResetPassword.php b/app/Notifications/ResetPassword.php index 6109253..776083b 100644 --- a/app/Notifications/ResetPassword.php +++ b/app/Notifications/ResetPassword.php @@ -2,7 +2,6 @@ namespace App\Notifications; -use App\Facade\UserConfig; use App\Mail\MailableBase; use App\Mail\ResetMyPassword; use Illuminate\Bus\Queueable; @@ -11,6 +10,7 @@ use Illuminate\Notifications\Notification; class ResetPassword extends Notification { use Queueable; + use DatabaseEmailNotification; /** * The password reset token. @@ -30,34 +30,6 @@ class ResetPassword extends Notification $this->token = $token; } - /** - * Get the notification's delivery channels. - * - * @param mixed $notifiable - * @return array - */ - public function via($notifiable) - { - $drivers = []; - - if (UserConfig::get('queue_emails')) - { - $drivers[] = QueueEmailDatabaseChannel::class; - } - else - { - $drivers[] = 'mail'; - $drivers[] = SentEmailDatabaseChannel::class; - } - - return $drivers; - } - - public function toEmailDatabase($notifiable) - { - return $this->toMail($notifiable)->buildEmailLog(); - } - /** * Get the mail representation of the notification. * @@ -66,12 +38,10 @@ class ResetPassword extends Notification */ public function toMail($notifiable) { - $notification = new ResetMyPassword($notifiable, $this->token); + $mailable = new ResetMyPassword($notifiable, $this->token); - // Set to and from properties accordingly - $notification->from(UserConfig::get('sender_address'), UserConfig::get('sender_name')); - $notification->to($notifiable->email, $notifiable->name); + $this->setPropertiesOnMailable($mailable, $notifiable); - return $notification; + return $mailable; } } diff --git a/app/Notifications/UserActivationRequired.php b/app/Notifications/UserActivationRequired.php new file mode 100644 index 0000000..26852bb --- /dev/null +++ b/app/Notifications/UserActivationRequired.php @@ -0,0 +1,39 @@ +setPropertiesOnMailable($mailable, $notifiable); + + return $mailable; + } +} diff --git a/app/Notifications/UserSelfActivated.php b/app/Notifications/UserSelfActivated.php new file mode 100644 index 0000000..2ab73f2 --- /dev/null +++ b/app/Notifications/UserSelfActivated.php @@ -0,0 +1,42 @@ +createdUser = $createdUser; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage|MailableBase + */ + public function toMail($notifiable) + { + $mailable = new UserSelfActivatedMailable($notifiable, $this->createdUser); + + $this->setPropertiesOnMailable($mailable, $notifiable); + + return $mailable; + } +} diff --git a/app/Traits/ActivatesUsers.php b/app/Traits/ActivatesUsers.php index 1c3a6f7..113a1e8 100644 --- a/app/Traits/ActivatesUsers.php +++ b/app/Traits/ActivatesUsers.php @@ -2,10 +2,9 @@ namespace App\Traits; -use App\Mail\UserSelfActivated; +use App\Notifications\UserSelfActivated; use App\User; use App\UserActivity; -use Illuminate\Support\Facades\Mail; trait ActivatesUsers { @@ -30,7 +29,7 @@ trait ActivatesUsers /** @var User $adminUser */ foreach ($adminUsers as $adminUser) { - Mail::to($adminUser)->send(new UserSelfActivated($adminUser, $createdUser)); + $adminUser->notify(new UserSelfActivated($createdUser)); } } } \ No newline at end of file