Allow kwargs overrides for (nearly) all settings

* Update utils.get_anymail_setting to support
  kwargs override of django.conf.settings values
* Use the updated version everywhere
* Switch from ImproperlyConfigured to
  AnymailConfigurationError exception
  (anticipates feature_wehooks change)

Closes #12
This commit is contained in:
medmunds
2016-04-29 14:34:34 -07:00
parent 6b415eeaae
commit df881fdb75
10 changed files with 124 additions and 38 deletions

View File

@@ -122,6 +122,14 @@ if you are using other Django apps that work with the same ESP.)
# nor ANYMAIL_MAILGUN_API_KEY have been set
Finally, for complex use cases, you can override most settings on a per-instance
basis by providing keyword args where the instance is initialized (e.g., in a
:func:`~django.core.mail.get_connection` call to create an email backend instance,
or in `View.as_view()` call to set up webhooks in a custom urls.py). To get the kwargs
parameter for a setting, drop "ANYMAIL" and the ESP name, and lowercase the rest:
e.g., you can override ANYMAIL_MAILGUN_API_KEY by passing `api_key="abc"` to
:func:`~django.core.mail.get_connection`. See :ref:`multiple-backends` for an example.
There are specific Anymail settings for each ESP (like API keys and urls).
See the :ref:`supported ESPs <supported-esps>` section for details.
Here are the other settings Anymail supports:

View File

@@ -13,7 +13,7 @@ This could be useful, for example, to deliver customer emails with the ESP,
but send admin emails directly through an SMTP server:
.. code-block:: python
:emphasize-lines: 8,10,13,15
:emphasize-lines: 8,10,13,15,19-20,22
from django.core.mail import send_mail, get_connection
@@ -28,9 +28,17 @@ but send admin emails directly through an SMTP server:
# You can even use multiple Anymail backends in the same app:
sendgrid_backend = get_connection('anymail.backends.sendgrid.SendGridBackend')
send_mail("Password reset", "Here you go", "user@example.com", ["noreply@example.com"],
send_mail("Password reset", "Here you go", "noreply@example.com", ["user@example.com"],
connection=sendgrid_backend)
# You can override settings.py settings with kwargs to get_connection.
# This example supplies credentials to use a SendGrid subuser acccount:
alt_sendgrid_backend = get_connection('anymail.backends.sendgrid.SendGridBackend',
username='marketing_subuser', password='abc123')
send_mail("Here's that info", "you wanted", "marketing@example.com", ["prospect@example.com"],
connection=alt_sendgrid_backend)
You can supply a different connection to Django's
:func:`~django.core.mail.send_mail` and :func:`~django.core.mail.send_mass_mail` helpers,
and in the constructor for an
@@ -39,6 +47,3 @@ and in the constructor for an
(See the :class:`django.utils.log.AdminEmailHandler` docs for more information
on Django's admin error logging.)
.. _django.utils.log.AdminEmailHandler:
https://docs.djangoproject.com/en/stable/topics/logging/#django.utils.log.AdminEmailHandler