Skip to content

Commit

Permalink
#78 - Customize mail - Backend (#83)
Browse files Browse the repository at this point in the history
* Add custom mail

* fix code style

* Add custom mail for forgot password

* Fix test

* Improve mail context

* Apply suggestions from code review

Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com>

* Refactor callback function

* Update resources/views/emails/auth/verify.blade.php

Co-authored-by: Aleksandra Kozubal <104600942+AleksandraKozubal@users.noreply.github.com>

---------

Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com>
Co-authored-by: Aleksandra Kozubal <104600942+AleksandraKozubal@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent 143579e commit 51f1262
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 19 deletions.
5 changes: 3 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Models;

use App\Notifications\SendResetPasswordEmail;
use App\Notifications\ResetPasswordNotification;
use App\Notifications\SendVerificationEmail;
use Carbon\Carbon;
use Illuminate\Contracts\Auth\CanResetPassword;
Expand All @@ -18,6 +18,7 @@
use Spatie\Permission\Traits\HasRoles;

/**
* @param string $token
* @property int $id
* @property string $name
* @property string $surname
Expand Down Expand Up @@ -61,7 +62,7 @@ public function sendEmailVerificationNotification(): void

public function sendPasswordResetNotification($token): void
{
$this->notify(new SendResetPasswordEmail($token));
$this->notify(new ResetPasswordNotification($token));
}

public function quizSubmissions(): HasMany
Expand Down
35 changes: 35 additions & 0 deletions app/Notifications/ResetPasswordNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ResetPasswordNotification extends Notification
{
use Queueable;

public function __construct(
protected string $token,
) {}

public function via(object $notifiable): array
{
return ["mail"];
}

public function toMail(object $notifiable): MailMessage
{
$resetUrl = url("/auth/password/reset/" . $this->token . "&email=" . urlencode($notifiable->email));

return (new MailMessage())
->subject("Resetowanie hasła")
->view("emails.auth.reset-password", [
"user" => $notifiable,
"url" => $resetUrl,
]);
}
}
14 changes: 0 additions & 14 deletions app/Notifications/SendResetPasswordEmail.php

This file was deleted.

10 changes: 10 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace App\Providers;

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -16,5 +18,13 @@ public function register(): void
public function boot(): void
{
JsonResource::withoutWrapping();
VerifyEmail::toMailUsing(
fn(object $notifiable, string $url): MailMessage => (new MailMessage())
->subject("Weryfikacja adresu e-mail")
->view("emails.auth.verify", [
"user" => $notifiable,
"url" => $url,
]),
);
}
}
27 changes: 27 additions & 0 deletions resources/views/emails/auth/reset-password.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zresetuj hasło</title>
</head>

<header>
<img src="favicon.png" alt="{{ config('app.name') }} Logo" width="150" height="50">
<h1>{{ config('app.name') }}</h1>
</header>

<body>
<h1>Cześć {{ $user->name }},</h1>

<p>Otrzymaliśmy prośbę o zresetowanie hasła dla Twojego konta.</p>

<p>Aby zresetować swoje hasło, kliknij poniższy link:</p>

<a href="{{ $url }}">Zresetuj hasło</a>


<p>Pozdrawiamy,<br>{{ config('app.name') }}</p>
</body>
</html>
27 changes: 27 additions & 0 deletions resources/views/emails/auth/verify.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="pl">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> {{ config('app.name') }}</title>
</head>

<header>
<img src="" alt="{{ config('app.name') }} Logo" width="150" height="50">
<h1>{{ config('app.name') }}</h1>
</header>

<body>
<p>Cześć {{ $user->name }},</p>

<p>Aby zweryfikować swoje konto, kliknij poniższy link:</p>

<a href="{{ $url }}">Zweryfikuj e-mail </a>

<p>Jeśli to nie Ty wysłałeś/aś tę prośbę, po prostu zignoruj tę wiadomość.</p>

<p>Pozdrawiamy,<br>{{ config('app.name') }}</p>
</body>

</html>
1 change: 0 additions & 1 deletion resources/views/emails/verify-email.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Feature/ForgotPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\Feature;

use App\Models\User;
use App\Notifications\SendResetPasswordEmail;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use JsonException;
Expand All @@ -30,7 +30,7 @@ public function testUserCanSendForgotPasswordRequest(): void

Notification::assertSentTo(
[$user],
SendResetPasswordEmail::class,
ResetPasswordNotification::class,
);
}

Expand Down

0 comments on commit 51f1262

Please sign in to comment.