246 Commits

Author SHA1 Message Date
medmunds
09bec9e463 Improve error when reply_to isn't list.
Issue a better error message if message.reply_to
is set to a single string.

(Would also like to do this for to, cc, and bcc,
but Django core EmailMessage.recipients is called
and stumbles over thoses cases before Anymail's
backend gets involved.)

Fixes #57
2017-04-04 11:57:11 -07:00
medmunds
3dd05ae882 Mailgun: update integration tests
Track change to Mailgun's events API, which
no longer includes message recipients.

(Only affected check for successful send
in the integration tests; Anymail doesn't
use the events API outside test code.)

Fixes #58
2017-04-04 10:33:03 -07:00
medmunds
23800657b8 Support Python 3.6 with Django 1.11-alpha
Only real problem is in json serialization tests:
Python 3.6 [changed][1] the json serialization
error message to use the object's class name
rather than its repr. E.g.:
  "Decimal('19.99') is not JSON serializable"
becomes:
  "Object of type 'Decimal' is not JSON serializable"

Update tests that looked for specific serialization
error message to just look for the word "Decimal"
instead. (Works with all Python versions.)

[1]: https://bugs.python.org/issue26623
2017-01-22 11:34:57 -08:00
medmunds
56d2b53c2b Use specific ESP name in error messages.
Change  (e.g.,) "ESP API response 400"
to "Mailgun API response 400".
2017-01-22 10:21:19 -08:00
medmunds
79288603fb Rename EmailBackends for Django consistency
* **Future breaking change:**
  Rename all Anymail backends to just `EmailBackend`,
  matching Django's naming convention.
  (E.g., switch to "anymail.backends.mailgun.EmailBackend"
  rather than "anymail.backends.mailgun.MailgunBackend".)

  The old names still work, but will issue a DeprecationWarning
  and will be removed in some future release.

  (Apologies for this change; the old naming convention was
  a holdover from Djrill, and I wanted consistency with
  other Django EmailBackends before hitting 1.0.)

Fixes #49.
2017-01-20 15:47:37 -08:00
medmunds
bff01b440a Fix webhook tests for Django 1.11-alpha
Django 1.11 appears to enforce ALLOWED_HOSTS
(more strictly) during tests.
2017-01-19 19:22:16 -08:00
medmunds
0ba5d1d4ad Mandrill: include auth in webhook signature calc
Mandrill's webhook signature calculation uses the
*exact url* Mandrill is posting to. If HTTP basic
auth is also used, that auth is included in the url.

Anymail was using Django's request.build_absolute_uri,
which doesn't include HTTP basic auth. Anymail now
includes the auth in the calculation, if it was present
in the request.

This should eliminate the need to use the
ANYMAIL_MANDRILL_WEBHOOK_URL override,
if Django's SECURE_PROXY_SSL_HEADER and
USE_X_FORWARDED_HOST (and/or
USE_X_FORWARDED_PROTO) settings are correct
for your server.

(The calculated url is now also included in
the validation failure error message, to aid
debugging.)

Fixes #48
2017-01-19 19:01:36 -08:00
medmunds
0eab2172d2 Tests: add Django 1.11-alpha
(including on Python 3.6)

Allow failures on prerelease Django tests.
2017-01-19 14:54:59 -08:00
Mike Edmunds
e568e50d0c SendGrid: update to new v3 send API (#50)
SendGrid: update to v3 send API

**SendGrid:** **[possibly-breaking]** Update SendGrid backend to newer Web API v3. This should be a transparent change for most projects. Exceptions: if you use SendGrid username/password auth, esp_extra with "x-smtpapi", or multiple Reply-To addresses, please review the [porting notes](http://anymail.readthedocs.io/en/latest/esps/sendgrid/#sendgrid-v3-upgrade).

Closes #28
2017-01-19 14:29:15 -08:00
Mike Edmunds
edf2a3ddcf Handle Django lazy strings.
In BasePayload, ensure any Django ugettext_lazy
(or similar) are converted to real strings before
handing off to ESP code. This resolves problems where
calling code expects it can use lazy strings "anywhere",
but non-Django code (requests, ESP packages) don't
always handle them correctly.

* Add utils helpers for lazy objects (is_lazy, force_non_lazy*)
* Add lazy object handling to utils.Attachment
* Add lazy object handling converters to BasePayload attr
  processing where appropriate. (This ends up varying by
  the expected attribute type.)

Fixes #34.
2016-12-30 15:48:08 -05:00
medmunds
146afbaf3b Simplify Mandrill webhook validation handshake.
Anymail was requiring Mandrill's webhook authentication key for the initial webhook url validation request from Mandrill, but Mandrill doesn't issue the key until that validation request succeeds.

* Defer complaining about missing Mandrill webhook key until actual event post.
* Document the double-deploy process required to set up Mandrill webhooks.

Fixes #46.
2016-12-16 14:24:46 -08:00
medmunds
52596394cc Support Postmark delivery event webhook.
Add support for Postmark's recently-released [delivery tracking webhook] to Anymail's normailized status event handling. The existing Anymail tracking webhook URL can be copied to "Delivery webhook" in your Postmark outbound server settings.

Closes #45.
2016-12-15 14:24:10 -08:00
medmunds
d0596d100b Raise error for invalidly-formatted email addresses.
A message's `from_email` and each address in its `to`, `cc`, and `bcc` lists must contain exactly one email address. Previous code would silently ignore additional addresses, leading to unusual behavior. Now, raises new `AnymailInvalidAddress` exception.

Example: `from_email='Widgets, Inc. <widgets@example.com>'` is invalid: it needs double-quotes around the "Widgets, Inc." display-name portion. In earlier versions, this probably would have sent the message from something like "From: Widgets <@localhost>". Now, it will raise an exception.

**Potentially-breaking change:** If your code is using an unquoted display-name containing a comma in an email address, it will now raise an error. In earlier versions, this may have appeared to succeed, but was almost certainly not doing what you intended.

Fixes #44.
2016-12-15 13:57:49 -08:00
medmunds
4ca39a976f Postmark: handle Reply-To in EmailMessage headers
Move 'Reply-To' header into dedicated Postmark API param

Fixes #39
2016-11-01 12:12:21 -07:00
Seb Bacon
f0589e3338 Compatibility with earlier Python 2.7 versions
Compatibility with Python 2.7 versions older than 2.7.7

* Use Django's constant_time_compare method
* Include sparkpost in test requirements
* Don't use non-public `EnvironmentVarGuard` in tests

Fixes #41
2016-11-01 11:24:51 -07:00
medmunds
7248c3441e Postmark: add track_clicks support
Closes #38
2016-10-23 10:54:59 -07:00
medmunds
a1380b82f3 SendGrid: force empty text and html to " " with template_id
Work around an unexpected limitation in SendGrid template
rendering, where template text or html bodies are omitted
if the supplied message text or html is "". Changing empty
string to " " works around the issue.

https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/smtpapi.html#-Text-or-HTML-Templates

Closes #32
2016-10-13 15:15:37 -07:00
medmunds
771ed513b2 Tests: switch to explicit u"unicode" literals
Drop `from __future__ import unicode_literals`;
it was there for Python 3.2 compatibility (which
Anymail doesn't support). Ensures tests use normal
strs in Python 2.x.
2016-10-13 14:37:08 -07:00
medmunds
fb21c0d25b Mailgun: Add MAILGUN_SENDER_DOMAIN setting
Allow custom MAILGUN_SENDER_DOMAIN in Anymail
settings. (Replaces need to use global esp_extra.)

Improve docs to cover cases where this is needed.

(esp_extra sender_domain is still supported for
overriding individual messages.)

Fixes #26.
2016-08-03 14:19:35 -07:00
medmunds
f95be248ec SparkPost: remove empty content params with template_id
When using a stored template, SparkPost disallows
subject, text, and html. Django's EmailMessage default
empty strings are enough to provoke "Both content
object and template_id are specified" from SparkPost,
so remove them (if empty) when using stored templates.

Update docs and tests; add integration test for template_id.

Fixes #24
2016-06-24 12:13:32 -07:00
medmunds
0c5911ca34 Tests: limit live API integration tests in Travis runs
To conserve our ESP test accounts' send quotas, don't run
the live API integration tests 13 times in every Travis run.
Instead, just run them twice, on a representative set
of Python/Django combinations:

* Once on Python 2.7 (currently with Django 1.8)
* Once on Python 3.x (currently 3.5 with Django 1.9)

(Prep for running weekly tests on Travis cron.)

The *non*-integration tests still run on all combos.

* Introduce RUN_LIVE_TESTS environment var to control
  whether live API integration test cases should run.
  Default True, except in Travis-CI runs default False.
* Enable RUN_LIVE_TESTS in .travis.yml matrix for the
  Python/Django combos listed above.
2016-06-23 15:21:40 -07:00
medmunds
b664ee3dbc SparkPost: work around json recipients problem
python-sparkpost generates a transmissions.send
payload which is now considered invalid by the API,
if you try to use both `cc` (or `bcc`) and the
`recipients` dict structure required for merge_data.

[Anymail had been generating that recipients dict
structure in all cases, for simplicity. Sometime
between 2016-06-07 and 2016-06-22, SparkPost
began rejecting that if it appeared in the `header_to`
constructed by python-sparkpost.]
2016-06-22 16:58:56 -07:00
Mike Edmunds
db101bf6b9 Add SparkPost support (#20)
Implement SparkPost backend and tracking webhooks.

Closes #11.
2016-06-22 15:31:30 -07:00
Adam Chainz
731670427c Tests: Convert readthedocs links to .io
Convert readthedocs links for their .org -> .io migration for hosted projects

As per [their blog post of the 27th April](https://blog.readthedocs.com/securing-subdomains/) ‘Securing subdomains’:

> Starting today, Read the Docs will start hosting projects from subdomains on the domain readthedocs.io, instead of on readthedocs.org. This change addresses some security concerns around site cookies while hosting user generated data on the same domain as our dashboard.

Test Plan: Manually visited all the links I’ve modified.
2016-06-22 14:57:58 -07:00
medmunds
1ea9ab6fee Upgrade requests exceptions to AnymailRequestsAPIError
Catch and re-raise requests.RequestException in
AnymailRequestsBackend.post_to_esp.

* AnymailRequestsAPIError is needed for proper
  fail_silently handling.
* Retain original requests exception type, to avoid
  breaking existing code that might look for specific
  requests exceptions.

Closes #16
2016-06-01 10:18:27 -07:00
medmunds
5d2bc66190 Clean up leftover references to "Djrill" 2016-06-01 08:11:47 -07:00
medmunds
5d417080ee SendGrid: set to to from in batch send.
Set ignored (but required-valid) `to` field to
the from_email for batch send with merge_data.
See https://github.com/anymail/django-anymail/pull/14#issuecomment-220231250
2016-06-01 08:05:51 -07:00
medmunds
af0e36ab65 Webhooks: fix 403 Forbidden errors (csrf check)
* csrf_exempt must be applied to View.dispatch,
  not View.post.

* In base WebhookTestCase, enable Django test Client
  enforce_csrf_checks. (Test Client by default disables
  CSRF protection.)

Closes #19
2016-05-31 11:57:48 -07:00
medmunds
34af81aee6 Tests: Simplify Django version compatibility
* Restructure runtests.py as suggested in
  https://docs.djangoproject.com/en/1.9/topics/testing/advanced/#using-the-django-test-runner-to-test-reusable-applications

* Load version-specific Django settings modules
  (rather than trying to make a single settings.configure()
  work with all supported versions).

* Use `django-admin startproject` defaults for all
  settings modules. (Tests compatibility with typical
  apps, middleware, and other settings.)

* Set up tests-specific url config; switch to literal
  urls in test cases. (Eliminates url `reverse` from
  tests.)

* Make runtests.py executable

Closes #18
2016-05-31 11:57:19 -07:00
medmunds
296f6cab50 Test Django 1.10
Also includes:
* Change AnymailTestMixin.assertDoesNotWarn
  to filter specific warning classes.
* Look specifically for AnymailInsecureWebhookWarning
  in WebhookBasicAuthTestsMixin.test_warns_if_no_auth
  (because we don't care *in that test case* about
  DeprecatedInDjango10 warnings).
2016-05-29 17:34:51 -07:00
medmunds
995617a130 SendGrid: improve to handling with merge_data
* Add merge_data integration test
* Simplify backend handling for this case
  (also fixes duplicated to's in all_recipients
  from previous fix)
2016-05-18 11:23:01 -07:00
Lewis Taylor
72d899460c SendGrid: Set to field when using merge_data (#14)
* Set to field when using merge_data

The `to` field is required even if providing recipient addresses in x-smtpapi. See https://sendgrid.com/docs/API_Reference/Web_API/mail.html#-send.

* Check data['to'] contains expected emails

* Add space for toname check

* Make `to` expected data contain email only
2016-05-18 11:01:54 -07:00
medmunds
f8eafba0df Add pre_send and post_send signals
Closes #8
2016-05-12 21:18:04 -07:00
medmunds
c60790fb52 Use generic TestBackend for base functionality tests
* Create generic TestBackend that simply collects
  send parameters
* Change BackendSettingsTests to TestBackend,
  and add some missing cases
* Add UnsupportedFeatureTests
* Replace repetitive per-backend SEND_DEFAULTS
  test cases with single (and more comprehensive)
  SendDefaultsTests
2016-05-12 17:18:48 -07:00
medmunds
a0b92bee7a Mandrill: support esp_extra
* Merge esp_extra with Mandrill send payload
* Handle pythonic forms of `recipient_metadata`
  and `template_content` in esp_extra
* DeprecationWarning for Mandrill EmailMessage
  attributes inherited from Djrill
2016-05-11 15:08:57 -07:00
medmunds
75730e8219 Add ESP templates, batch send and merge
* message.template_id to use ESP stored templates
* message.merge_data and merge_global_data
  to supply per-recipient/global merge variables
  (with or without an ESP stored template)
* When using per-recipient merge_data, tell ESP to use
  batch send: individual message per "to" address.
  (Mailgun does this automatically; SendGrid requires
  using a different "to" field; Mandrill requires
  `preserve_recipients=False`; Postmark doesn't
  support *this type* of batch sending with merge data.)
* Allow message.from_email=None (must be set after
  init) and message.subject=None to suppress those
  fields in API calls (for ESPs that allow "From" and
  "Subject" in their template definitions).

Mailgun:
* Emulate merge_global_data by copying to
  recipient-variables for each recipient.

SendGrid:
* Add delimiters to merge field names via
  esp_extra['merge_field_format'] or
  ANYMAIL_SENDGRID_MERGE_FIELD_FORMAT setting.

Mandrill:
* Remove Djrill versions of these features;
  update migration notes.

Closes #5.
2016-05-06 12:27:11 -07:00
medmunds
c7a8c81e32 Remove pre-Django 1.8 compatibility code 2016-04-30 10:33:51 -07:00
medmunds
1372ef21eb SendGrid: merge 'filters' in esp_extra
Previously, setting esp_extra['x-smtpapi']['filters']
would override the entire filters setting, potentially
undoing other Anymail options that use SendGrid
filters (like track_opens).

Now, 'filters' is special-cased, and merged with
any other Anymail filter options.

(We don't do a fully deep merge, because otherwise
there would be no way to use esp_extra to *clear*
Anymail settings.)
2016-04-30 10:21:11 -07:00
medmunds
d3f914be12 Event-tracking webhooks
Closes #3
2016-04-29 18:10:50 -07:00
medmunds
36461e57b9 Keep angle brackets on SendGrid smtp-id
SendGrid seems to consistently use <angle brackets>
on Message-ID and smtp-id.
2016-04-29 15:05:58 -07:00
medmunds
df881fdb75 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
2016-04-29 14:34:34 -07:00
medmunds
ebe6710326 Get rid of magic JSON serialization for Mailgun metadata.
Treat Mailgun metadata like all other ESPs: simple
key-value dict, where values are strings. If you want
to store JSON in metadata, you should serialize and
deserialize it yourself.
2016-04-29 09:46:13 -07:00
medmunds
8e43f29944 Workaround missing smtp-id in SendGrid tracking.
* Add smtp-id in unique_args (metadata), to ensure
  it shows up in click and open events.
* Add SENDGRID_GENERATE_MESSAGE_ID setting,
  default True, to control auto-Message-ID behavior.
* Document it.
2016-04-29 09:44:31 -07:00
medmunds
1e4abada8c Update Postmark integration test
Error message changed for invalid API token
2016-04-20 17:21:03 -07:00
medmunds
f50908c975 Mailgun integration tests tolerate server errors
Make Mailgun integration tests resilient to 403
and 5xx errors from events API.
2016-03-22 11:17:02 -07:00
medmunds
385d76b53a Move tests out of app module
(Directory structure as suggested in
[Django testing docs][1].)

[1]: https://docs.djangoproject.com/en/1.9/topics/testing/advanced/#using-the-django-test-runner-to-test-reusable-applications
2016-03-21 11:38:58 -07:00