trap errors in mailer
This commit is contained in:
@@ -41,6 +41,21 @@ class NotificationEventListener
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $to
|
||||||
|
* @param $email
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function sendEmail($to, $email)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return Mail::to($to)->send($email);
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
Log::error('Error sending email!');
|
||||||
|
Log::error($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send an email when the user registered
|
* Send an email when the user registered
|
||||||
* @param UserRegistered $event
|
* @param UserRegistered $event
|
||||||
@@ -62,20 +77,17 @@ class NotificationEventListener
|
|||||||
|
|
||||||
if (!empty($admin_email)) {
|
if (!empty($admin_email)) {
|
||||||
$email = new \App\Mail\Admin\UserRegistered($event->user);
|
$email = new \App\Mail\Admin\UserRegistered($event->user);
|
||||||
Mail::to($admin_email)->send($email);
|
$this->sendEmail($admin_email, $email);
|
||||||
}
|
}
|
||||||
|
|
||||||
# Then notify the user
|
# Then notify the user
|
||||||
if($event->user->state === UserState::ACTIVE) {
|
if($event->user->state === UserState::ACTIVE) {
|
||||||
$email = new \App\Mail\UserRegistered(
|
$email = new \App\Mail\UserRegistered($event->user);
|
||||||
$event->user,
|
|
||||||
'Welcome to ' . config('app.name') . '!'
|
|
||||||
);
|
|
||||||
|
|
||||||
Mail::to($event->user->email)->send($email);
|
|
||||||
} else if($event->user->state === UserState::PENDING) {
|
} else if($event->user->state === UserState::PENDING) {
|
||||||
Mail::to($event->user->email)->send(new \App\Mail\UserPending($event->user));
|
$email = new \App\Mail\UserPending($event->user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->sendEmail($event->user->email, $email);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -89,21 +101,13 @@ class NotificationEventListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($event->old_state === UserState::PENDING) {
|
if ($event->old_state === UserState::PENDING) {
|
||||||
if ($event->user->state === UserState::ACTIVE)
|
if ($event->user->state === UserState::ACTIVE) {
|
||||||
{
|
$email = new \App\Mail\UserRegistered($event->user,
|
||||||
$email = new \App\Mail\UserRegistered(
|
'Your registration has been accepted!');
|
||||||
$event->user,
|
} else if ($event->user->state === UserState::REJECTED) {
|
||||||
'Your registration has been accepted!'
|
|
||||||
);
|
|
||||||
|
|
||||||
Mail::to($event->user->email)->send($email);
|
|
||||||
}
|
|
||||||
|
|
||||||
else if ($event->user->state === UserState::REJECTED)
|
|
||||||
{
|
|
||||||
$email = new \App\Mail\UserRejected($event->user);
|
$email = new \App\Mail\UserRejected($event->user);
|
||||||
Mail::to($event->user->email)->send($email);
|
|
||||||
}
|
}
|
||||||
|
$this->sendEmail($event->user->email, $email);
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: Other state transitions
|
# TODO: Other state transitions
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ use App\Models\User;
|
|||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Mail\Mailable;
|
use Illuminate\Mail\Mailable;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
|
|
||||||
class UserRegistered extends Mailable
|
class UserRegistered extends Mailable
|
||||||
{
|
{
|
||||||
@@ -16,7 +15,7 @@ class UserRegistered extends Mailable
|
|||||||
|
|
||||||
public function __construct(User $user, $subject=null)
|
public function __construct(User $user, $subject=null)
|
||||||
{
|
{
|
||||||
$this->subject = $subject;
|
$this->subject = $subject ?: 'Welcome to '.config('app.name').'!';
|
||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user