Skip to content

Commit f0ea098

Browse files
committed
Extend e-mail sending with queues
1 parent 007efa2 commit f0ea098

File tree

2 files changed

+207
-4
lines changed

2 files changed

+207
-4
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ For Laravel 5.0.* | 5.1.* | 5.2.*, use branch
1616
## About
1717

1818
- [x] Generate and store a verification token for a registered user
19-
- [x] Send an e-mail with the verification token link
19+
- [x] Send or queue an e-mail with the verification token link
2020
- [x] Handle the token verification
2121
- [x] Set the user as verified
2222
- [x] Relaunch the process anytime
@@ -233,7 +233,7 @@ Do something if the verification fails.
233233

234234
### API
235235

236-
The package public API offers three (3) methods.
236+
The package public API offers height (8) methods.
237237

238238
* `generate(AuthenticatableContract $user)`
239239

@@ -243,18 +243,42 @@ Generate and save a verification token for the given user.
243243

244244
Send by e-mail a link containing the verification token.
245245

246+
* `sendQueue(AuthenticatableContract $user, $subject = null, $from = null, $name = null)`
247+
248+
Queue and send by e-mail a link containing the verification token.
249+
250+
* `sendQueueOn($queue, AuthenticatableContract $user, $subject = null, $from = null, $name = null)`
251+
252+
Queue on the given queue and send by e-mail a link containing the verification token.
253+
254+
* `sendLater($seconds, AuthenticatableContract $user, $subject = null, $from = null, $name = null)`
255+
256+
Send later by e-mail a link containing the verification token.
257+
258+
* `sendLaterOn($queue, $seconds, AuthenticatableContract $user, $subject = null, $from = null, $name = null)`
259+
260+
Send later on the given queue by e-mail a link containing the verification token.
261+
246262
* `process($email, $token, $userTable)`
247263

248264
Process the token verification for the given e-mail and token.
249265

266+
* `emailView($name)`
267+
268+
Set the e-mail view name.
269+
270+
For the `sendQueue`, `sendQueueOn`, `sendLater` and
271+
`sendLaterOn` methods, you must [configure your queues](https://laravel.com/docs/)
272+
before using this feature.
273+
250274
### Facade
251275

252276
The package offers a facade `UserVerification::`.
253277

254278
### Attributes/Properties
255279

256280
To customize the package behaviour and the redirects you can implement and
257-
customize six (5) attributes/properties:
281+
customize six (6) attributes/properties:
258282

259283
* `$redirectIfVerified = '/';`
260284

@@ -272,6 +296,10 @@ Where to redirect after a failling token verification.
272296

273297
Name of the view returned by the getVerificationError method.
274298

299+
* `$verificationEmailView = 'emails.user-verification';`
300+
301+
Name of the default e-mail view.
302+
275303
* `$userTable = 'users';`
276304

277305
Name of the default table used for managing users.

src/UserVerification.php

Lines changed: 176 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,94 @@ public function send(AuthenticatableContract $user, $subject = null, $from = nul
116116
return (boolean) $this->emailVerificationLink($user, $subject, $from, $name);
117117
}
118118

119+
/**
120+
* Queue and send by e-mail a link containing the verification token.
121+
*
122+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
123+
* @param string $subject
124+
* @param string $from
125+
* @param string $name
126+
* @return bool
127+
*
128+
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
129+
*/
130+
public function sendQueue(AuthenticatableContract $user, $subject = null, $from = null, $name = null)
131+
{
132+
if (! $this->isCompliant($user)) {
133+
throw new ModelNotCompliantException();
134+
}
135+
136+
return (boolean) $this->emailQueueVerificationLink($user, $subject, $from, $name);
137+
}
138+
139+
/**
140+
* Queue on the given queue and send by e-mail a link containing the verification token.
141+
*
142+
* @param string $queue
143+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
144+
* @param string $subject
145+
* @param string $from
146+
* @param string $name
147+
* @return bool
148+
*
149+
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
150+
*/
151+
public function sendQueueOn($queue, AuthenticatableContract $user, $subject = null, $from = null, $name = null)
152+
{
153+
if (! $this->isCompliant($user)) {
154+
throw new ModelNotCompliantException();
155+
}
156+
157+
return (boolean) $this->emailQueueOnVerificationLink($queue, $user, $subject, $from, $name);
158+
}
159+
160+
/**
161+
* Send later by e-mail a link containing the verification token.
162+
*
163+
* @param int $seconds
164+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
165+
* @param string $subject
166+
* @param string $from
167+
* @param string $name
168+
* @return bool
169+
*
170+
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
171+
*/
172+
public function sendLater($seconds, AuthenticatableContract $user, $subject = null, $from = null, $name = null)
173+
{
174+
if (! $this->isCompliant($user)) {
175+
throw new ModelNotCompliantException();
176+
}
177+
178+
return (boolean) $this->emailLaterVerificationLink($seconds, $user, $subject, $from, $name);
179+
}
180+
181+
/**
182+
* Send later on the given queue by e-mail a link containing the verification token.
183+
*
184+
* @param string $queue
185+
* @param int $seconds
186+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
187+
* @param string $subject
188+
* @param string $from
189+
* @param string $name
190+
* @return bool
191+
*
192+
* @throws \Jrean\UserVerification\Exceptions\ModelNotCompliantException
193+
*/
194+
public function sendLaterOn($queue, $seconds, AuthenticatableContract $user, $subject = null, $from = null, $name = null)
195+
{
196+
if (! $this->isCompliant($user)) {
197+
throw new ModelNotCompliantException();
198+
}
199+
200+
return (boolean) $this->emailLaterOnVerificationLink($queue, $seconds, $user, $subject, $from, $name);
201+
}
202+
119203
/**
120204
* Set the e-mail view name.
121205
*
122-
* @param mixed $name
206+
* @param string $name
123207
* @return \Jrean\UserVerification
124208
*/
125209
public function emailView($name)
@@ -256,6 +340,97 @@ protected function emailVerificationLink(AuthenticatableContract $user, $subject
256340
});
257341
}
258342

343+
/**
344+
* Prepare and push a job onto the queue to send the e-mail with the verification token link.
345+
*
346+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
347+
* @param string $subject
348+
* @param string $from
349+
* @param string $name
350+
* @return mixed
351+
*/
352+
protected function emailQueueVerificationLink(AuthenticatableContract $user, $subject, $from = null, $name = null)
353+
{
354+
return $this->mailer->queue($this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
355+
if (! empty($from)) {
356+
$m->from($from, $name);
357+
}
358+
359+
$m->to($user->email);
360+
361+
$m->subject(is_null($subject) ? 'Your Account Verification Link' : $subject);
362+
});
363+
}
364+
365+
/**
366+
* Prepare and push a job onto the given queue to send the e-mail with the verification token link.
367+
*
368+
* @param string $queue
369+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
370+
* @param string $subject
371+
* @param string $from
372+
* @param string $name
373+
* @return mixed
374+
*/
375+
protected function emailQueueOnVerificationLink($queue, AuthenticatableContract $user, $subject, $from = null, $name = null)
376+
{
377+
return $this->mailer->queueOn($queue, $this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
378+
if (! empty($from)) {
379+
$m->from($from, $name);
380+
}
381+
382+
$m->to($user->email);
383+
384+
$m->subject(is_null($subject) ? 'Your Account Verification Link' : $subject);
385+
});
386+
}
387+
388+
/**
389+
* Prepare and send later the e-mail with the verification token link.
390+
*
391+
* @param int $seconds
392+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
393+
* @param string $subject
394+
* @param string $from
395+
* @param string $name
396+
* @return mixed
397+
*/
398+
protected function emailLaterVerificationLink($seconds, AuthenticatableContract $user, $subject, $from = null, $name = null)
399+
{
400+
return $this->mailer->later($seconds, $this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
401+
if (! empty($from)) {
402+
$m->from($from, $name);
403+
}
404+
405+
$m->to($user->email);
406+
407+
$m->subject(is_null($subject) ? 'Your Account Verification Link' : $subject);
408+
});
409+
}
410+
411+
/**
412+
* Prepare and send later on the given queue the e-mail with the verification token link.
413+
*
414+
* @param int $seconds
415+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
416+
* @param string $subject
417+
* @param string $from
418+
* @param string $name
419+
* @return mixed
420+
*/
421+
protected function emailLaterOnVerificationLink($queue, $seconds, AuthenticatableContract $user, $subject, $from = null, $name = null)
422+
{
423+
return $this->mailer->laterOn($queue, $seconds, $this->emailView, compact('user'), function ($m) use ($user, $subject, $from, $name) {
424+
if (! empty($from)) {
425+
$m->from($from, $name);
426+
}
427+
428+
$m->to($user->email);
429+
430+
$m->subject(is_null($subject) ? 'Your Account Verification Link' : $subject);
431+
});
432+
}
433+
259434
/**
260435
* Determine if the given model table has the verified and verification_token
261436
* columns.

0 commit comments

Comments
 (0)