Add better exception handling for emails

This commit is contained in:
Romain Neutron
2013-01-29 12:06:02 +01:00
parent 309a0a9472
commit 8325c35f91
4 changed files with 55 additions and 16 deletions

View File

@@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Controller\Admin;
use Alchemy\Phrasea\Notification\Receiver; use Alchemy\Phrasea\Notification\Receiver;
use Alchemy\Phrasea\Notification\Mail\MailTest; use Alchemy\Phrasea\Notification\Mail\MailTest;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Silex\Application; use Silex\Application;
use Silex\ControllerProviderInterface; use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -187,7 +188,12 @@ class Dashboard implements ControllerProviderInterface
$app->abort(400, 'Bad request missing email parameter'); $app->abort(400, 'Bad request missing email parameter');
}; };
try {
$receiver = new Receiver(null, $mail); $receiver = new Receiver(null, $mail);
} catch (InvalidArgumentException $e) {
return $app->redirect('/admin/dashboard/?email=not-sent');
}
$mail = MailTest::create($app, $receiver); $mail = MailTest::create($app, $receiver);
$app['notification.deliverer']->deliver($mail); $app['notification.deliverer']->deliver($mail);

View File

@@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Controller\Prod;
use Silex\Application; use Silex\Application;
use Silex\ControllerProviderInterface; use Silex\ControllerProviderInterface;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Notification\Emitter; use Alchemy\Phrasea\Notification\Emitter;
use Alchemy\Phrasea\Notification\Receiver; use Alchemy\Phrasea\Notification\Receiver;
use Alchemy\Phrasea\Notification\Mail\MailRecordsExport; use Alchemy\Phrasea\Notification\Mail\MailRecordsExport;
@@ -285,7 +286,11 @@ class Export implements ControllerProviderInterface
$emitter = new Emitter($app['phraseanet.user']->get_display_name(), $app['phraseanet.user']->get_email()); $emitter = new Emitter($app['phraseanet.user']->get_display_name(), $app['phraseanet.user']->get_email());
foreach ($destMails as $key => $mail) { foreach ($destMails as $key => $mail) {
try {
$receiver = new Receiver(null, trim($mail)); $receiver = new Receiver(null, trim($mail));
} catch (InvalidArgumentException $e) {
continue;
}
$mail = MailRecordsExport::create($app, $receiver, $emitter, $request->request->get('textmail')); $mail = MailRecordsExport::create($app, $receiver, $emitter, $request->request->get('textmail'));
$mail->setButtonUrl($url); $mail->setButtonUrl($url);

View File

@@ -12,6 +12,7 @@
namespace Alchemy\Phrasea\Helper\User; namespace Alchemy\Phrasea\Helper\User;
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Notification\Mail\MailSuccessEmailUpdate; use Alchemy\Phrasea\Notification\Mail\MailSuccessEmailUpdate;
use Alchemy\Phrasea\Notification\Receiver; use Alchemy\Phrasea\Notification\Receiver;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -539,16 +540,28 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
$new_email = $user->get_email(); $new_email = $user->get_email();
if ($old_email != $new_email) { if ($old_email != $new_email) {
try {
$newReceiver = new Receiver(null, $new_email);
$oldReceiver = new Receiver(null, $old_email); $oldReceiver = new Receiver(null, $old_email);
} catch (InvalidArgumentException $e) {
}
if ($oldReceiver) {
$mailOldAddress = MailSuccessEmailUpdate::create($this->app, $oldReceiver, null, sprintf(_('You will now receive notifications at %s'), $new_email)); $mailOldAddress = MailSuccessEmailUpdate::create($this->app, $oldReceiver, null, sprintf(_('You will now receive notifications at %s'), $new_email));
$mailNewAddress = MailSuccessEmailUpdate::create($this->app, $newReceiver, null, sprintf(_('You will no longer receive notifications at %s'), $old_email));
$this->app['notification.deliverer']->deliver($mailOldAddress); $this->app['notification.deliverer']->deliver($mailOldAddress);
}
try {
$newReceiver = new Receiver(null, $new_email);
} catch (InvalidArgumentException $e) {
}
if ($newReceiver) {
$mailNewAddress = MailSuccessEmailUpdate::create($this->app, $newReceiver, null, sprintf(_('You will no longer receive notifications at %s'), $old_email));
$this->app['notification.deliverer']->deliver($mailNewAddress); $this->app['notification.deliverer']->deliver($mailNewAddress);
} }
}
return $this; return $this;
} }

View File

@@ -8,6 +8,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Notification\Mail\MailSuccessFTP; use Alchemy\Phrasea\Notification\Mail\MailSuccessFTP;
use Alchemy\Phrasea\Notification\Receiver; use Alchemy\Phrasea\Notification\Receiver;
@@ -658,18 +659,32 @@ class task_period_ftp extends task_appboxAbstract
$sender_message = $text_mail_sender . $message; $sender_message = $text_mail_sender . $message;
$receiver_message = $text_mail_receiver . $message; $receiver_message = $text_mail_receiver . $message;
$receiver = null;
try {
$receiver = new Receiver(null, $sendermail); $receiver = new Receiver(null, $sendermail);
} catch (InvalidArgumentException $e) {
}
if ($receiver) {
$mail = MailSuccessFTP::create($this->dependencyContainer, $receiver, null, $sender_message); $mail = MailSuccessFTP::create($this->dependencyContainer, $receiver, null, $sender_message);
$mail->setServer($ftp_server); $mail->setServer($ftp_server);
$this->dependencyContainer['notification.deliverer']->deliver($mail); $this->dependencyContainer['notification.deliverer']->deliver($mail);
}
$receiver = null;
try {
$receiver = new Receiver(null, $mail); $receiver = new Receiver(null, $mail);
} catch (InvalidArgumentException $e) {
}
if ($receiver) {
$mail = MailSuccessFTP::create($this->dependencyContainer, $receiver, null, $receiver_message); $mail = MailSuccessFTP::create($this->dependencyContainer, $receiver, null, $receiver_message);
$mail->setServer($ftp_server); $mail->setServer($ftp_server);
$this->dependencyContainer['notification.deliverer']->deliver($mail); $this->dependencyContainer['notification.deliverer']->deliver($mail);
} }
}
public function logexport(record_adapter $record, $obj, $ftpLog) public function logexport(record_adapter $record, $obj, $ftpLog)
{ {