Monday, 9 September 2013

CakePHP Catch Invalid Email

CakePHP Catch Invalid Email

I am trying to send email invitations with CakePHP (2.3.6) where a User
enters a comma separated list of email addresses into an input field.
Currently, I can send emails without an issue as long as there is no
invalid email address. However, I tried adding a Try/Catch to catch errors
if there was a bad email, but my code is never hitting the catch.
Here is what I have
try {
if($Email->send()) {
$this->Session->setFlash(__('Email successfully sent'),
'flash/success');
} else {
$this->Session->setFlash(__('Could not invite guests.
Please, try again.'), 'flash/error');
$this->redirect($this->referer());
}
} catch(Exception $e) {
$this->Session->setFlash(__('Could not invite guests. Probably
a bad email. Please, try again.'), 'flash/error');
$this->redirect($this->referer());
}
$this->redirect($this->referer());
When I enter an invalid email with debugging on, I get the following error:
Invalid email: "foo" Error: An Internal Error Has Occurred.
And when debugging is off:
An Internal Error Has Occurred.
I assumed there is something wrong with my Try / Catch, but it looks right
to me. Is there some other method I should be going through to catch
CakePHP errors?
Thanks in advance!!

No comments:

Post a Comment