Skip to content

Commit 6ef74dc

Browse files
committed
Interact with Mailable
1 parent 763b646 commit 6ef74dc

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-verify-new-email` will be documented in this file
44

5+
## 1.6.0 - 2022-02-24
6+
7+
- Interact with the Mailable before sending
8+
59
## 1.5.0 - 2022-02-04
610

711
- Support for Laravel 9

src/MustVerifyNewEmail.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ trait MustVerifyNewEmail
1414
* to the new email address.
1515
*
1616
* @param string $email
17+
* @param callable $withMailable
1718
* @return \Illuminate\Database\Eloquent\Model|null
1819
*/
19-
public function newEmail(string $email): ?Model
20+
public function newEmail(string $email, callable $withMailable = null): ?Model
2021
{
2122
if ($this->getEmailForVerification() === $email && $this->hasVerifiedEmail()) {
2223
return null;
2324
}
2425

25-
return $this->createPendingUserEmailModel($email)->tap(function ($model) {
26-
$this->sendPendingEmailVerificationMail($model);
26+
return $this->createPendingUserEmailModel($email)->tap(function ($model) use ($withMailable) {
27+
$this->sendPendingEmailVerificationMail($model, $withMailable);
2728
});
2829
}
2930

@@ -80,9 +81,10 @@ public function clearPendingEmail()
8081
* Sends the VerifyNewEmail Mailable to the new email address.
8182
*
8283
* @param \Illuminate\Database\Eloquent\Model $pendingUserEmail
84+
* @param callable $withMailable
8385
* @return mixed
8486
*/
85-
public function sendPendingEmailVerificationMail(Model $pendingUserEmail)
87+
public function sendPendingEmailVerificationMail(Model $pendingUserEmail, callable $withMailable = null)
8688
{
8789
$mailableClass = config('verify-new-email.mailable_for_first_verification');
8890

@@ -92,6 +94,10 @@ public function sendPendingEmailVerificationMail(Model $pendingUserEmail)
9294

9395
$mailable = new $mailableClass($pendingUserEmail);
9496

97+
if ($withMailable) {
98+
$withMailable($mailable, $pendingUserEmail);
99+
}
100+
95101
return Mail::to($pendingUserEmail->email)->send($mailable);
96102
}
97103

tests/MustVerifyNewEmailTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ public function it_can_regenerate_a_token_and_mail_it()
131131
});
132132
}
133133

134+
/** @test */
135+
public function it_can_interact_with_the_mailable()
136+
{
137+
Mail::fake();
138+
139+
$user = $this->user();
140+
141+
$user->email_verified_at = now();
142+
$user->save();
143+
144+
$pendingUserEmail = $user->newEmail('new@example.com', function (VerifyNewEmail $mailable, PendingUserEmail $model) {
145+
$mailable->bcc('test@test.com');
146+
$this->assertTrue($model->exists);
147+
});
148+
149+
Mail::assertQueued(VerifyNewEmail::class, function (Mailable $mailable) use ($pendingUserEmail) {
150+
$this->assertTrue($mailable->pendingUserEmail->is($pendingUserEmail));
151+
152+
$this->assertTrue($mailable->hasTo('new@example.com'));
153+
154+
$this->assertFalse($mailable->hasTo('old@example.com'));
155+
$this->assertFalse($mailable->hasCc('old@example.com'));
156+
$this->assertFalse($mailable->hasBcc('old@example.com'));
157+
158+
$this->assertTrue($mailable->hasBcc('test@test.com'));
159+
160+
return true;
161+
});
162+
}
163+
134164
/** @test */
135165
public function it_deletes_previous_attempts_of_the_user_trying_to_verify_a_new_email()
136166
{

0 commit comments

Comments
 (0)