#124: Updated the user self-registration required and user self-activated e-mails to be notifications so they can be queued.

This commit is contained in:
Andy Heathershaw 2019-07-14 14:18:15 +01:00
parent 216c93a750
commit 624334570f
10 changed files with 153 additions and 51 deletions

View File

@ -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)

View File

@ -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'));
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -0,0 +1,53 @@
<?php
namespace App\Notifications;
use App\EmailLog;
use App\Facade\UserConfig;
use Illuminate\Mail\Mailable;
/**
* Enables a notification to use a Mailable to write to the database.
*/
trait DatabaseEmailNotification
{
/**
* 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;
}
/**
* Creates the EmailLog entry to write to the database.
* @param $notifiable
* @return EmailLog
*/
public function toEmailDatabase($notifiable)
{
return $this->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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Notifications;
use App\Mail\MailableBase;
use App\Mail\UserActivationRequired as UserActivationRequiredMailable;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
class UserActivationRequired extends Notification
{
use Queueable;
use DatabaseEmailNotification;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage|MailableBase
*/
public function toMail($notifiable)
{
$mailable = new UserActivationRequiredMailable($notifiable);
$this->setPropertiesOnMailable($mailable, $notifiable);
return $mailable;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace App\Notifications;
use App\Mail\MailableBase;
use App\Mail\UserSelfActivated as UserSelfActivatedMailable;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
class UserSelfActivated extends Notification
{
use Queueable;
use DatabaseEmailNotification;
private $createdUser;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(User $createdUser)
{
$this->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;
}
}

View File

@ -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));
}
}
}