Verified Commit 0df14c44 authored by Baptiste Beauplat's avatar Baptiste Beauplat
Browse files

Catch socket/SMTP exception when sending emails

parent 0130c063
...@@ -170,15 +170,21 @@ class Email(object): ...@@ -170,15 +170,21 @@ class Email(object):
pylons.url._pop_object() pylons.url._pop_object()
if 'debexpo.testsmtp' in pylons.config: if 'debexpo.testsmtp' in pylons.config:
self._save_as_file(recipients, message) return self._save_as_file(recipients, message)
else: else:
self._send_as_mail(recipients, message) try:
return self._send_as_mail(recipients, message)
except (IOError, smtplib.SMTPException) as e:
log.critical('Failed to send email: {}'.format(e))
return False
def _save_as_file(self, recipients, message): def _save_as_file(self, recipients, message):
log.debug('Save email as file to %s' % self.server) log.debug('Save email as file to %s' % self.server)
with open(pylons.config['debexpo.testsmtp'], 'a') as email: with open(pylons.config['debexpo.testsmtp'], 'a') as email:
email.write(message) email.write(message)
return True
def _send_as_mail(self, recipients, message): def _send_as_mail(self, recipients, message):
log.debug('Starting SMTP session to %s:%s' % (self.server, self.port)) log.debug('Starting SMTP session to %s:%s' % (self.server, self.port))
session = smtplib.SMTP(self.server, self.port) session = smtplib.SMTP(self.server, self.port)
...@@ -197,8 +203,10 @@ class Email(object): ...@@ -197,8 +203,10 @@ class Email(object):
log.critical('Failed sending to %s: %s, %s' % log.critical('Failed sending to %s: %s, %s' %
(recipient, result[recipient][0], (recipient, result[recipient][0],
result[recipient][1])) result[recipient][1]))
else: return False
log.debug('Successfully sent')
log.debug('Successfully sent')
return True
def _check_error(self, msg, err, data=None): def _check_error(self, msg, err, data=None):
if err != 'OK': if err != 'OK':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment