Skip to content

Commit

Permalink
Customizable notification email mailable (#7)
Browse files Browse the repository at this point in the history
* Add translations for email

* Add custom view for mail

* Add translation

* Add "Kind regards" translation

* Pint

* Remove view

* Undo

* Rename folder to `emails`

* Format again (sorry)

* Update README

* Add TwoFactorCodeMail for sending OTP emails

* Format
  • Loading branch information
CodeWithDennis authored Aug 23, 2024
1 parent 3bc2b21 commit 65c4cc3
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ protected $listen = [
];
```

If you want to customize the views (including email), you can publish them using the following command:

```bash
php artisan vendor:publish --tag=filament-two-factor-auth-views
```

## Usage

### Configuration
Expand Down
6 changes: 5 additions & 1 deletion resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"Your security code is": "Your security code is",
"The provided two factor authentication code was invalid.": "The provided two factor authentication code was invalid.",
"Generate new recovery codes": "Generate new recovery codes",
"You can disable two factor authentication at any time by using the button below": "You can disable two factor authentication at any time by using the button below"
"You can disable two factor authentication at any time by using the button below": "You can disable two factor authentication at any time by using the button below",
"If you didn't try to log in, please change your password immediately to protect your account.": "If you didn't try to log in, please change your password immediately to protect your account.",
"You recently requested to log in to your account. To complete the login, please use the following two-factor authentication (2FA) code:": "You recently requested to log in to your account. To complete the login, please use the following two-factor authentication (2FA) code:",
"Kind regards": "Kind regards",
"Hello": "Hello"
}
6 changes: 5 additions & 1 deletion resources/lang/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"Your security code is": "Uw beveiligingscode is",
"The provided two factor authentication code was invalid.": "De verstrekte tweestaps-verificatiecode was ongeldig.",
"Generate new recovery codes": "Genereer nieuwe herstelcodes",
"You can disable two factor authentication at any time by using the button below": "U kunt tweestaps-verificatie op elk moment uitschakelen met de onderstaande knop"
"You can disable two factor authentication at any time by using the button below": "U kunt tweestaps-verificatie op elk moment uitschakelen met de onderstaande knop",
"You recently requested to log in to your account. To complete the login, please use the following two-factor authentication (2FA) code:": "U heeft onlangs gevraagd om in te loggen op uw account. Gebruik de volgende tweestaps-verificatie (2FA) code om de login te voltooien:",
"If you didn't try to log in, please change your password immediately to protect your account.": "Als u niet heeft geprobeerd in te loggen, wijzig dan onmiddellijk uw wachtwoord om uw account te beschermen.",
"Kind regards": "Met vriendelijke groet",
"Hello": "Hallo"
}
17 changes: 17 additions & 0 deletions resources/views/emails/two-factor-code.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<x-mail::message>
{{ __('Hello') }},

{{ __('You recently requested to log in to your account. To complete the login, please use the following two-factor authentication (2FA) code:') }}

<div style="background-color: #f0f0f0; padding: 10px; border-radius: 5px; text-align: center; max-width: 200px; font-family: monospace; margin: 25px auto 25px;">
<span style="font-size: 1.5em; color: #333;">
{{ $code }}
</span>
</div>


{{ __('If you didn\'t try to log in, please change your password immediately to protect your account.') }}

{{ __('Kind regards') }},<br>
{{ config('app.name') }}
</x-mail::message>
53 changes: 53 additions & 0 deletions src/Mail/TwoFactorCodeMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Vormkracht10\TwoFactorAuth\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class TwoFactorCodeMail extends Mailable
{
use Queueable;
use SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(public string $code)
{
//
}

/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: __('Your security code for :app', ['app' => config('app.name')]),
);
}

/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'filament-two-factor-auth::emails.two-factor-code',
);
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
10 changes: 5 additions & 5 deletions src/Notifications/SendOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Notification;
use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException;
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
use Vormkracht10\TwoFactorAuth\Actions\GenerateOTP;
use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType;
use Vormkracht10\TwoFactorAuth\Mail\TwoFactorCodeMail;

class SendOTP extends Notification implements ShouldQueue
{
Expand Down Expand Up @@ -48,11 +49,10 @@ public function via(object $notifiable): array
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
public function toMail(object $notifiable): Mailable
{
return (new MailMessage)
->subject(__('Your security code for :app', ['app' => config('app.name')]))
->line(__('Your security code is') . ' ' . $this->getTwoFactorCode($notifiable));
return (new TwoFactorCodeMail($this->getTwoFactorCode($notifiable)))
->to($notifiable->email);
}

/**
Expand Down

0 comments on commit 65c4cc3

Please sign in to comment.