trap errors in mailer

This commit is contained in:
Nabeel Shahzad
2017-12-31 23:26:10 -06:00
parent 2f56baf7ff
commit 820f5dbbd6
2 changed files with 26 additions and 23 deletions

View File

@@ -41,6 +41,21 @@ class NotificationEventListener
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
* @param UserRegistered $event
@@ -62,20 +77,17 @@ class NotificationEventListener
if (!empty($admin_email)) {
$email = new \App\Mail\Admin\UserRegistered($event->user);
Mail::to($admin_email)->send($email);
$this->sendEmail($admin_email, $email);
}
# Then notify the user
if($event->user->state === UserState::ACTIVE) {
$email = new \App\Mail\UserRegistered(
$event->user,
'Welcome to ' . config('app.name') . '!'
);
Mail::to($event->user->email)->send($email);
$email = new \App\Mail\UserRegistered($event->user);
} 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->user->state === UserState::ACTIVE)
{
$email = new \App\Mail\UserRegistered(
$event->user,
'Your registration has been accepted!'
);
Mail::to($event->user->email)->send($email);
}
else if ($event->user->state === UserState::REJECTED)
{
if ($event->user->state === UserState::ACTIVE) {
$email = new \App\Mail\UserRegistered($event->user,
'Your registration has been accepted!');
} else if ($event->user->state === UserState::REJECTED) {
$email = new \App\Mail\UserRejected($event->user);
Mail::to($event->user->email)->send($email);
}
$this->sendEmail($event->user->email, $email);
}
# TODO: Other state transitions

View File

@@ -6,7 +6,6 @@ use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserRegistered extends Mailable
{
@@ -16,7 +15,7 @@ class UserRegistered extends Mailable
public function __construct(User $user, $subject=null)
{
$this->subject = $subject;
$this->subject = $subject ?: 'Welcome to '.config('app.name').'!';
$this->user = $user;
}