diff --git a/django/core/mail/backends/console.py b/django/core/mail/backends/console.py index 705497520a..fa71f3816f 100644 --- a/django/core/mail/backends/console.py +++ b/django/core/mail/backends/console.py @@ -18,17 +18,20 @@ class EmailBackend(BaseEmailBackend): return self._lock.acquire() try: - stream_created = self.open() - for message in email_messages: - self.stream.write('%s\n' % message.message().as_string()) - self.stream.write('-'*79) - self.stream.write('\n') - self.stream.flush() # flush after each message - if stream_created: - self.close() - except: - if not self.fail_silently: - raise + # The try-except is nested to allow for + # Python 2.4 support (Refs #12147) + try: + stream_created = self.open() + for message in email_messages: + self.stream.write('%s\n' % message.message().as_string()) + self.stream.write('-'*79) + self.stream.write('\n') + self.stream.flush() # flush after each message + if stream_created: + self.close() + except: + if not self.fail_silently: + raise finally: self._lock.release() return len(email_messages)