Skip to content

Commit

Permalink
handle filament email verification override. Fix long urls in email
Browse files Browse the repository at this point in the history
  • Loading branch information
cannycookie committed Sep 15, 2023
1 parent e0749c1 commit 8976a22
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The following email templates are included to get you started and show different

- **User Registered** - Welcome them to the platform
- **User Verify Email** - Check they are human
- **User Verified Email** - Yes they are
- **User Verification Success** - Yes they are
- **User Request Password Reset** - Let them change the password
- **User Password Reset Success** - Yay, you changed your password
- **User Locked Out** - Oops - What to do now?
Expand Down
5 changes: 4 additions & 1 deletion resources/views/email/parts/_head.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
/* ANDROID CENTER FIX */
div[style*="margin: 16px 0;"] { margin: 0 !important; }
/* Long URL Fix */
a {word-break: break-all;}
</style>
</head>
<body style="background-color: {{$data['theme']["header_bg_color"]}}; margin: 0 !important; padding: 0 !important;">
<body style="background-color: {{$data['theme']["body_bg_color"]}}; margin: 0 !important; padding: 0 !important;">

4 changes: 2 additions & 2 deletions src/EmailTemplatesAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Visualbuilder\EmailTemplates;

use App\Models\User;

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Visualbuilder\EmailTemplates\Mail\UserVerifyEmail;
Expand All @@ -20,7 +20,7 @@ public function boot()

if(config('filament-email-templates.send_emails.verification')) {
//Override default Laravel VerifyEmail notification toMail function
VerifyEmail::toMailUsing(function (User $user, string $verificationUrl) {
VerifyEmail::toMailUsing(function ($user, string $verificationUrl) {
return (new UserVerifyEmail($user, $verificationUrl));
});
}
Expand Down
4 changes: 1 addition & 3 deletions src/EmailTemplatesFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Illuminate\Support\Facades\Facade;

/**
* @see \Visualbuilder\EmailTemplates\Skeleton\SkeletonClass
*/

class EmailTemplatesFacade extends Facade
{
/**
Expand Down
16 changes: 16 additions & 0 deletions src/EmailTemplatesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Visualbuilder\EmailTemplates\Helpers\CreateMailableHelper;
use Visualbuilder\EmailTemplates\Helpers\FormHelper;


class EmailTemplatesServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
Expand All @@ -35,6 +36,21 @@ public function packageRegistered(): void
$this->app->singleton(CreateMailableInterface::class, CreateMailableHelper::class);
$this->app->singleton(FormHelperInterface::class, FormHelper::class);
$this->app->register(EmailTemplatesEventServiceProvider::class);

//Override Filament send email verification Listener
if(config('filament-email-templates.send_emails.verification')) {
$this->app->bind(
\Filament\Listeners\Auth\SendEmailVerificationNotification::class,
\Visualbuilder\EmailTemplates\Listeners\SendEmailVerificationListener::class,

);
$this->app->bind(
\Filament\Notifications\Auth\VerifyEmail::class,
\Visualbuilder\EmailTemplates\Listeners\SendEmailVerificationListener::class,

);
}

}

public function packageBooted(): void
Expand Down
34 changes: 34 additions & 0 deletions src/Listeners/SendEmailVerificationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Visualbuilder\EmailTemplates\Listeners;

use Exception;
use Filament\Facades\Filament;
use Visualbuilder\EmailTemplates\Notifications\UserVerifyEmailNotification;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification as BaseListener;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class SendEmailVerificationListener extends BaseListener
{
public function handle(Registered $event): void
{
if (! $event->user instanceof MustVerifyEmail) {
return;
}

if ($event->user->hasVerifiedEmail()) {
return;
}

if (! method_exists($event->user, 'notify')) {
$userClass = $event->user::class;

throw new Exception("Model [{$userClass}] does not have a [notify()] method.");
}

$notification = new UserVerifyEmailNotification(Filament::getVerifyEmailUrl($event->user));

$event->user->notify($notification);
}
}
4 changes: 2 additions & 2 deletions src/Mail/UserVerifyEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class UserVerifyEmail extends Mailable
*
* @return void
*/
public function __construct($user, $token)
public function __construct($user, $url)
{
$this->user = $user;
$this->verificationUrl = $token;
$this->verificationUrl = $url;
$this->sendTo = $user->email;
}
}
63 changes: 63 additions & 0 deletions src/Notifications/UserVerifyEmailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Visualbuilder\EmailTemplates\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Visualbuilder\EmailTemplates\Mail\UserVerifiedEmail;
use Visualbuilder\EmailTemplates\Mail\UserVerifyEmail;

class UserVerifyEmailNotification extends Notification
{
use Queueable;

public string $url;


/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($url)
{
$this->url = $url;
}

/**
* 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)
{
if(config('filament-email-templates.send_emails.verification')) {
return app(UserVerifyEmail::class, ['user' => $notifiable,'url'=>$this->url]);
}
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [

];
}
}

0 comments on commit 8976a22

Please sign in to comment.