diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index 6a95248..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":1099:{a:2:{s:7:"defects";a:1:{s:89:"ProtoneMedia\LaravelVerifyNewEmail\Tests\VerifyNewEmailTest::it_can_generate_a_signed_url";i:4;}s:5:"times";a:7:{s:125:"ProtoneMedia\LaravelVerifyNewEmail\Tests\MustVerifyNewEmailTest::it_can_generate_a_token_and_mail_it_to_the_new_email_address";d:0.146;s:102:"ProtoneMedia\LaravelVerifyNewEmail\Tests\MustVerifyNewEmailTest::it_can_regenerate_a_token_and_mail_it";d:0.011;s:134:"ProtoneMedia\LaravelVerifyNewEmail\Tests\MustVerifyNewEmailTest::it_deletes_previous_attempts_of_the_user_trying_to_verify_a_new_email";d:0.01;s:126:"ProtoneMedia\LaravelVerifyNewEmail\Tests\VerifyNewEmailControllerTest::it_updates_the_user_email_and_deletes_the_pending_email";d:0.022;s:145:"ProtoneMedia\LaravelVerifyNewEmail\Tests\VerifyNewEmailControllerTest::it_removes_both_pending_models_if_two_users_try_to_verify_the_same_address";d:0.01;s:117:"ProtoneMedia\LaravelVerifyNewEmail\Tests\VerifyNewEmailControllerTest::it_throws_an_exception_if_the_token_is_invalid";d:0.012;s:89:"ProtoneMedia\LaravelVerifyNewEmail\Tests\VerifyNewEmailTest::it_can_generate_a_signed_url";d:0.086;}}} \ No newline at end of file diff --git a/config/config.php b/config/config.php index e0b1d1a..26c7bd0 100644 --- a/config/config.php +++ b/config/config.php @@ -38,4 +38,9 @@ * update its email address. */ 'mailable_for_new_email' => \ProtoneMedia\LaravelVerifyNewEmail\Mail\VerifyNewEmail::class, + + /** + * The Mailable that will be sent when the User's email address has been updated. + */ + 'mailable_for_new_email_notification' => \ProtoneMedia\LaravelVerifyNewEmail\Mail\NotifyOldEmail::class, ]; diff --git a/resources/views/notifyOldEmail.blade.php b/resources/views/notifyOldEmail.blade.php new file mode 100644 index 0000000..d2625b9 --- /dev/null +++ b/resources/views/notifyOldEmail.blade.php @@ -0,0 +1,8 @@ +@component('mail::message') +# Your email address has been changed + +Your email address has been changed. + +Thanks,
+{{ config('app.name') }} +@endcomponent \ No newline at end of file diff --git a/src/Mail/NotifyOldEmail.php b/src/Mail/NotifyOldEmail.php new file mode 100644 index 0000000..f24d858 --- /dev/null +++ b/src/Mail/NotifyOldEmail.php @@ -0,0 +1,34 @@ +subject(__('Your email address has been changed')); + + return $this->markdown('verify-new-email::notifyOldEmail'); + } +} diff --git a/src/PendingUserEmail.php b/src/PendingUserEmail.php index d333f0f..4cc9e51 100644 --- a/src/PendingUserEmail.php +++ b/src/PendingUserEmail.php @@ -5,6 +5,7 @@ use Illuminate\Auth\Events\Verified; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\URL; use Illuminate\Support\Traits\Tappable; @@ -58,6 +59,8 @@ public function activate() $dispatchEvent = !$user->hasVerifiedEmail() || $user->email !== $this->email; + $originalEmail = $user->email; + $user->email = $this->email; $user->save(); $user->markEmailAsVerified(); @@ -65,6 +68,8 @@ public function activate() static::whereEmail($this->email)->get()->each->delete(); $dispatchEvent ? event(new Verified($user)) : null; + + $this->sendNotificationToOldEmail($originalEmail); } /** @@ -80,4 +85,19 @@ public function verificationUrl(): string ['token' => $this->token] ); } + + /** + * Send a notification to original email address regarding change of email address. + * + * @param string $originalEmail + * @return void + */ + public function sendNotificationToOldEmail(string $originalEmail) + { + $mailableClass = config('verify-new-email.mailable_for_new_email_notification'); + + $mailable = new $mailableClass(); + + return Mail::to($originalEmail)->send($mailable); + } } diff --git a/tests/MustVerifyNewEmailTest.php b/tests/MustVerifyNewEmailTest.php index 2576f8c..fd78b07 100644 --- a/tests/MustVerifyNewEmailTest.php +++ b/tests/MustVerifyNewEmailTest.php @@ -5,6 +5,7 @@ use Illuminate\Mail\Mailable; use Illuminate\Support\Facades\Mail; use ProtoneMedia\LaravelVerifyNewEmail\InvalidEmailVerificationModelException; +use ProtoneMedia\LaravelVerifyNewEmail\Mail\NotifyOldEmail; use ProtoneMedia\LaravelVerifyNewEmail\Mail\VerifyFirstEmail; use ProtoneMedia\LaravelVerifyNewEmail\Mail\VerifyNewEmail; use ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail; @@ -148,4 +149,21 @@ public function it_deletes_previous_attempts_of_the_user_trying_to_verify_a_new_ 'email' => 'new@example.com', ]); } + + /** @test */ + public function it_notifies_old_email_address_after_email_change() + { + Mail::fake(); + + $user = $this->user(); + + $user->email_verified_at = now(); + $user->save(); + + $model = $user->newEmail('new@example.com'); + + $model->activate(); + + Mail::assertQueued(NotifyOldEmail::class); + } }