# Error handling for Mailer

**URL:** <https://discourse.getcockpit.com/t/error-handling-for-mailer/380>\
**Category:** Feature Request\
**Created:** [November 3, 2018, 12:26am UTC](https://discourse.getcockpit.com/t/error-handling-for-mailer/380 "2018-11-03T00:26:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 3, 2018, 12:26am UTC](https://discourse.getcockpit.com/t/error-handling-for-mailer/380/1 "2018-11-03T00:26:13Z")

</div>

Trusting a mailer without error handling means hoping, that nothing breaks. Especially if the form data is not saved in the database (for reasons).

I digged a bit in the code and it looks like enabling error handling for the mailer is quite easy.

Changing `$mail = new PHPMailer();` to `$mail = new PHPMailer(true);` in [Mailer.php#L37](https://github.com/agentejo/cockpit/blob/next/lib/Mailer.php#L37) enables throwing Exceptions.

After changing [Forms/bootstrap.php#L297](https://github.com/agentejo/cockpit/blob/next/modules/Forms/bootstrap.php#L297) from

```auto
$this->app->mailer->mail($frm['email_forward'], $options['subject'] ?? "New form data for: {$formname}", $body, $options);

```

to

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

```

I get a useful response: `{"error":"SMTP Error: Could not authenticate."}`

That was easy. Now comes the hard part:

Changing the few places in core, where Mailer is called could be fixed easily. I think, the only mailings are password reset and form submission.

Throwing Exceptions could break all places where the Mailer was called without catching them. What if custom bootstraps or addons use the Mailer class?

The default for form submission is, to return `$data`. Should it return `['error' => $response, 'data' => $data]` instead? This is the way, I handle form validation responses in FormValidation addon.

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [November 3, 2018, 1:45am UTC](https://discourse.getcockpit.com/t/error-handling-for-mailer/380/2 "2018-11-03T01:45:52Z")

</div>

I’m waiting for the Pull Request 😉

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 5, 2018, 7:05pm UTC](https://discourse.getcockpit.com/t/error-handling-for-mailer/380/3 "2018-11-05T19:05:12Z")

</div>

I’ll write it soon. I have a few things on my to do list with a higher priority:

1. [x] Custom renderers in entries view (set, maybe repeater, maybe some more)
2. [] eplore new virtual assets folders to build an image gallery system
3. [] change default tab in entry view to main
4. [x] **Mailer Exceptions**
5. [x] publish simple PHP frontend (using cockpit as a library)
6. … 😉

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 22, 2018, 12:08am UTC](https://discourse.getcockpit.com/t/error-handling-for-mailer/380/4 "2018-11-22T00:08:09Z")

</div>

> [@artur](#):
>
> I’m waiting for the Pull Request

I changed my priorities, so you don’t have to wait anymore:

> <https://github.com/agentejo/cockpit/pull/935>
>
> Now the mailer throws errors and they are catched in forms/submit and in passwor…d reset.
> 
> I had some struggle with i18n of error messages on the password reset page with the mixup of LimeExtra and riot.js, so I translated the messages in Auth.php before sending the json string.
