From cbd2edf9328f22bf80ddf6d5d03614845f954603 Mon Sep 17 00:00:00 2001 From: Timo Stollenwerk Date: Thu, 4 Nov 2010 15:56:12 +0000 Subject: [PATCH] Catch SMTP exceptions when sending email notifications. svn path=/plone.app.discussion/trunk/; revision=45199 --- CHANGES.txt | 7 ++++ plone/app/discussion/comment.py | 67 ++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 75504b5..5de27bf 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,13 @@ Changelog ========= +1.0RC1 (unreleased) +------------------- + +- Catch SMTP exceptions when sending email notifications. + [timo] + + 1.0b12 (2010-11-04) ------------------- diff --git a/plone/app/discussion/comment.py b/plone/app/discussion/comment.py index 0809c90..f3fb0a1 100644 --- a/plone/app/discussion/comment.py +++ b/plone/app/discussion/comment.py @@ -1,8 +1,13 @@ # -*- coding: utf-8 -*- """The default comment class and factory. """ + +import logging + from datetime import datetime +from smtplib import SMTPException + from zope.annotation.interfaces import IAnnotatable from zope.component.factory import Factory @@ -52,6 +57,8 @@ MAIL_NOTIFICATION_MESSAGE = _(u"mail_notification_message", default=u"A comment on '${title}' " "has been posted here: ${link}") +logger = logging.getLogger("plone.app.discussion") + class Comment(CatalogAware, WorkflowAware, DynamicType, Traversable, RoleManager, Owned, Implicit, Persistent): @@ -223,17 +230,29 @@ def notify_user(obj, event): # Send email if PLONE_4: - mail_host.send(message, - comment.author_email, - sender, - subject, - charset='utf-8') - else: - mail_host.secureSend(message, - comment.author_email, - sender, - subject=subject, - charset='utf-8') # pragma: no cover + try: + mail_host.send(message, + comment.author_email, + sender, + subject, + charset='utf-8') + except SMTPException: + logger.error('SMTP exception while trying to send an ' + + 'email from %s to %s', + sender, + comment.author_email) + else: # pragma: no cover + try: + mail_host.secureSend(message, + comment.author_email, + sender, + subject=subject, + charset='utf-8') + except SMTPException: + logger.error('SMTP exception while trying to send an ' + + 'email from %s to %s', + sender, + comment.author_email) def notify_moderator(obj, event): """Tell the moderator when a comment needs attention. @@ -284,10 +303,22 @@ def notify_moderator(obj, event): # Send email if PLONE_4: - mail_host.send(message, mto, sender, subject, charset='utf-8') - else: - mail_host.secureSend(message, - mto, - sender, - subject=subject, - charset='utf-8') # pragma: no cover + try: + mail_host.send(message, mto, sender, subject, charset='utf-8') + except SMTPException: + logger.error('SMTP exception while trying to send an ' + + 'email from %s to %s', + sender, + comment.author_email) + else: # pragma: no cover + try: + mail_host.secureSend(message, + mto, + sender, + subject=subject, + charset='utf-8') + except SMTPException: + logger.error('SMTP exception while trying to send an ' + + 'email from %s to %s', + sender, + comment.author_email)