mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
Mailgun, SparkPost: support multiple from_email addresses
[RFC-5322 allows](https://tools.ietf.org/html/rfc5322#section-3.6.2) multiple addresses in the From header. Django's SMTP backend supports this, as a single comma-separated string (*not* a list of strings like the recipient params): from_email='one@example.com, two@example.com' to=['one@example.com', 'two@example.com'] Both Mailgun and SparkPost support multiple From addresses (and Postmark accepts them, though truncates to the first one on their end). For compatibility with Django -- and because Anymail attempts to support all ESP features -- Anymail now allows multiple From addresses, too, for ESPs that support it. Note: as a practical matter, deliverability with multiple From addresses is pretty bad. (Google outright rejects them.) This change also reworks Anymail's internal ParsedEmail object, and approach to parsing addresses, for better consistency with Django's SMTP backend and improved error messaging. In particular, Django (and now Anymail) allows multiple email addresses in a single recipient string: to=['one@example.com', 'two@example.com, three@example.com'] len(to) == 2 # but there will be three recipients Fixes #60
This commit is contained in:
@@ -15,7 +15,7 @@ from mock import patch
|
||||
from sparkpost.exceptions import SparkPostAPIException
|
||||
|
||||
from anymail.exceptions import (AnymailAPIError, AnymailUnsupportedFeature, AnymailRecipientsRefused,
|
||||
AnymailConfigurationError)
|
||||
AnymailConfigurationError, AnymailInvalidAddress)
|
||||
from anymail.message import attach_inline_image_file
|
||||
|
||||
from .utils import AnymailTestMixin, decode_att, SAMPLE_IMAGE_FILENAME, sample_image_path, sample_image_content
|
||||
@@ -288,6 +288,20 @@ class SparkPostBackendStandardEmailTests(SparkPostBackendMockAPITestCase):
|
||||
params = self.get_send_params()
|
||||
self.assertNotIn('recipients', params)
|
||||
|
||||
def test_multiple_from_emails(self):
|
||||
"""SparkPost supports multiple addresses in from_email"""
|
||||
self.message.from_email = 'first@example.com, "From, also" <second@example.com>'
|
||||
self.message.send()
|
||||
params = self.get_send_params()
|
||||
self.assertEqual(params['from_email'],
|
||||
'first@example.com, "From, also" <second@example.com>')
|
||||
|
||||
# Make sure the far-more-likely scenario of a single from_email
|
||||
# with an unquoted display-name issues a reasonable error:
|
||||
self.message.from_email = 'Unquoted, display-name <from@example.com>'
|
||||
with self.assertRaises(AnymailInvalidAddress):
|
||||
self.message.send()
|
||||
|
||||
def test_api_failure(self):
|
||||
self.set_mock_failure(status_code=400)
|
||||
with self.assertRaisesMessage(AnymailAPIError, "SparkPost API response 400"):
|
||||
|
||||
Reference in New Issue
Block a user