Skip to content

Commit

Permalink
Merge pull request #935 from raffaelj/mailer-error-handling
Browse files Browse the repository at this point in the history
Mailer error handling
  • Loading branch information
aheinze committed Nov 22, 2018
2 parents 718babf + 083dadd commit f1afb43
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/Mailer.php
Expand Up @@ -34,7 +34,7 @@ public function mail($to, $subject, $message, $options = []) {

public function createMessage($to, $subject, $message, $options=[]) {

$mail = new PHPMailer();
$mail = new PHPMailer(true);

if ($this->transport == 'smtp') {

Expand Down
28 changes: 18 additions & 10 deletions modules/Cockpit/Controller/Auth.php
Expand Up @@ -67,7 +67,7 @@ public function requestreset() {
$user = $this->app->storage->findOne('cockpit/accounts', $query);

if (!$user) {
return $this->stop('{"error": "User not found"}', 404);
return $this->stop(['error' => $this('i18n')->get('User does not exist')], 404);
}

$token = uniqid('rp-').'-'.time();
Expand All @@ -77,16 +77,24 @@ public function requestreset() {
$this->app->storage->save('cockpit/accounts', $data);
$message = $this->app->view('cockpit:emails/recover.php', compact('user','token','target'));

$this->app->mailer->mail(
$user['email'],
$this->param('subject', $this->app->getSiteUrl().' - Pasword Recovery'),
$message
);
try {
$response = $this->app->mailer->mail(
$user['email'],
$this->param('subject', $this->app->getSiteUrl().' - '.$this('i18n')->get('Password Recovery')),
$message
);
} catch (\Exception $e) {
$response = $e->getMessage();
}

if ($response !== true) {
return $this->stop(['error' => $this('i18n')->get($response)], 404);
}

return ['message' => 'Recovery email sent'];
return ['message' => $this('i18n')->get('Recovery email sent')];
}

return $this->stop('{"error": "User required"}', 412);
return $this->stop(['error' => $this('i18n')->get('User required')], 412);
}

public function newpassword() {
Expand Down Expand Up @@ -123,9 +131,9 @@ public function resetpassword() {

$this->app->storage->save('cockpit/accounts', $data);

return ['success' => true, 'message' => 'Password updated'];
return ['success' => true, 'message' => $this('i18n')->get('Password updated')];
}

return $this->stop('{"error": "Token required"}', 412);
return $this->stop(['error' => $this('i18n')->get('Token required')], 412);
}
}
8 changes: 7 additions & 1 deletion modules/Cockpit/views/layouts/forgotpassword.php
Expand Up @@ -53,6 +53,10 @@
<div class="uk-animation-shake uk-margin-top" if="{ error }">
<strong>{ error }</strong>
</div>
<div class="uk-animation-shake uk-margin-top" if="{ message }">
<strong>{ message }</strong>
</div>
</div>
<div class="uk-alert uk-alert-success uk-text-center uk-animation-slide-bottom" if="{ reset }">
Expand All @@ -77,6 +81,7 @@
this.error = false;
this.reset = false;
this.message = false;
submit(e) {
Expand All @@ -88,11 +93,12 @@
App.request('/auth/requestreset', {user:this.refs.user.value}).then(function(data){
this.reset = true;
this.message = data.message;
this.update();
}.bind(this)).catch(function(data) {
this.error = '@lang("User does not exist")';
this.error = typeof data.error === 'string' ? data.error : '@lang("Something went wrong")';
App.$('#reset-dialog').removeClass('uk-animation-shake');
Expand Down
8 changes: 6 additions & 2 deletions modules/Forms/bootstrap.php
Expand Up @@ -296,7 +296,11 @@

$formname = isset($frm['label']) && trim($frm['label']) ? $frm['label'] : $form;

$this->app->mailer->mail($frm['email_forward'], $options['subject'] ?? "New form data for: {$formname}", $body, $options);
try {
$response = $this->app->mailer->mail($frm['email_forward'], $options['subject'] ?? "New form data for: {$formname}", $body, $options);
} catch (\Exception $e) {
$response = $e->getMessage();
}
}
}

Expand All @@ -307,7 +311,7 @@

$this->app->trigger('forms.submit.after', [$form, &$data, $frm]);

return $data;
return (isset($response) && $response !== true) ? ['error' => $response, 'data' => $data] : $data;
}
]);

Expand Down

0 comments on commit f1afb43

Please sign in to comment.