django/django — pull request #2467 by DasIch (2014-03-23 16:37:06+00:00)
Title: Turn *EmailBackend into contextmanager

This pull request turns `django.core.mail.backends.BaseEmailBackend` into a contextmanager that can be used in a with statement. This is useful, if you are sending multiple messages as described in [https://docs.djangoproject.com/en/dev/topics/email/#sending-multiple-emails]. The example mentioned there basically turns into

with mail.get_connection() as conn:
# ...
mail.EmailMessage(..., connection=conn).send()
# ...

which makes it less likely to forget the calls to `.open()` and `.close()`.

I've added a test but no documentation, yet. For documentation I considered adding a `.. versionadded:: ...` directive but I was unsure on which version to use and would appreciate any pointers you could give me regarding that problem.