Skip to content

Commit

Permalink
Send a notification to the old email address #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bradyrenting committed Jan 26, 2022
1 parent 2a8c9a6 commit de6c009
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
1 change: 0 additions & 1 deletion .phpunit.result.cache

This file was deleted.

5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
8 changes: 8 additions & 0 deletions resources/views/notifyOldEmail.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@component('mail::message')
# Your email address has been changed

Your email address has been changed.

Thanks,<br>
{{ config('app.name') }}
@endcomponent
34 changes: 34 additions & 0 deletions src/Mail/NotifyOldEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace ProtoneMedia\LaravelVerifyNewEmail\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class NotifyOldEmail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
$this->subject(__('Your email address has been changed'));

return $this->markdown('verify-new-email::notifyOldEmail');
}
}
20 changes: 20 additions & 0 deletions src/PendingUserEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,13 +59,17 @@ public function activate()

$dispatchEvent = !$user->hasVerifiedEmail() || $user->email !== $this->email;

$originalEmail = $user->email;

$user->email = $this->email;
$user->save();
$user->markEmailAsVerified();

static::whereEmail($this->email)->get()->each->delete();

$dispatchEvent ? event(new Verified($user)) : null;

$this->sendNotificationToOldEmail($originalEmail);
}

/**
Expand All @@ -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);
}
}
18 changes: 18 additions & 0 deletions tests/MustVerifyNewEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit de6c009

Please sign in to comment.