Skip to content

Commit

Permalink
Use email notifications instead of mailables
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcage committed Sep 21, 2022
1 parent 76d6eaf commit 9996133
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 88 deletions.
9 changes: 5 additions & 4 deletions app/Listeners/SendUserRegisteredNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace App\Listeners;

use App\Events\UserSelfRegistered;
use App\Mail\UserRegistered;
use App\Mail\UserRegisteredConfirmation;
use App\Models\User;
use App\Notifications\UserRegistered;
use App\Notifications\UserRegisteredConfirmation;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;

class SendUserRegisteredNotification
Expand All @@ -21,9 +22,9 @@ public function handle(UserSelfRegistered $event): void
$admins = User::where('is_super_admin', true)->get();
try {
if ($admins->isNotEmpty()) {
Mail::to($admins)->send(new UserRegistered($user));
Notification::send($admins, new UserRegistered($user));
}
Mail::to($user)->send(new UserRegisteredConfirmation($user));
$user->notify(new UserRegisteredConfirmation($user));
} catch (TransportExceptionInterface $ex) {
Log::error("Failed to send email to newly registered user $user->email.", $ex);
}
Expand Down
29 changes: 0 additions & 29 deletions app/Mail/UserRegistered.php

This file was deleted.

29 changes: 0 additions & 29 deletions app/Mail/UserRegisteredConfirmation.php

This file was deleted.

48 changes: 48 additions & 0 deletions app/Notifications/UserRegistered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Notifications;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserRegistered extends Notification
{
use Queueable;

public function __construct(public readonly User $user)
{
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject(__('New user registered: :name', [
'name' => $this->user->name,
]))
->greeting(__('User registered'))
->line(__('The user :name (:email) has created a new account.', [
'name' => $this->user->name,
'email' => $this->user->email,
]))
->action(__('View User'), route('users.show', $this->user));
}
}
51 changes: 51 additions & 0 deletions app/Notifications/UserRegisteredConfirmation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Notifications;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserRegisteredConfirmation extends Notification
{
use Queueable;

public function __construct(public readonly User $user)
{
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject(__('New user account registered at :app_name', [
'app_name' => config('app.name'),
]))
->greeting(__('Registration confirmation'))
->line(__('Dear :name.', [
'name' => $this->user->name,
]))
->line(__('Thanks for registering an account at :app_name with your e-mail address :email.', [
'app_name' => config('app.name'),
'email' => $this->user->email,
]))
->action(__('View your profile'), route('userprofile'));
}
}
12 changes: 0 additions & 12 deletions resources/views/emails/users/registered.blade.php

This file was deleted.

14 changes: 0 additions & 14 deletions resources/views/emails/users/registered_confirmation.blade.php

This file was deleted.

0 comments on commit 9996133

Please sign in to comment.