Support optional Source, parse from header
The Email ``from`` header is either formatted as ``name <address>`` or ``address``. This commit will use `parseaddr` to extract a ``(name, address)`` tuple, which we will use the ``address`` to check if it's verified. Also support the case where ``Source`` is omitted (which AWS requires the ``from`` header to be set).
This commit is contained in:
parent
cb364eedc6
commit
d21c387eb6
3 changed files with 79 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import email
|
||||
from email.utils import parseaddr
|
||||
|
||||
from moto.core import BaseBackend, BaseModel
|
||||
from .exceptions import MessageRejectedError
|
||||
|
|
@ -84,13 +85,27 @@ class SESBackend(BaseBackend):
|
|||
return message
|
||||
|
||||
def send_raw_email(self, source, destinations, raw_data):
|
||||
if source not in self.addresses:
|
||||
raise MessageRejectedError(
|
||||
"Did not have authority to send from email %s" % source
|
||||
)
|
||||
if source is not None:
|
||||
_, source_email_address = parseaddr(source)
|
||||
if source_email_address not in self.addresses:
|
||||
raise MessageRejectedError(
|
||||
"Did not have authority to send from email %s" % source_email_address
|
||||
)
|
||||
|
||||
recipient_count = len(destinations)
|
||||
message = email.message_from_string(raw_data)
|
||||
if source is None:
|
||||
if message['from'] is None:
|
||||
raise MessageRejectedError(
|
||||
"Source not specified"
|
||||
)
|
||||
|
||||
_, source_email_address = parseaddr(message['from'])
|
||||
if source_email_address not in self.addresses:
|
||||
raise MessageRejectedError(
|
||||
"Did not have authority to send from email %s" % source_email_address
|
||||
)
|
||||
|
||||
for header in 'TO', 'CC', 'BCC':
|
||||
recipient_count += sum(
|
||||
d.strip() and 1 or 0
|
||||
|
|
|
|||
|
|
@ -75,7 +75,10 @@ class EmailResponse(BaseResponse):
|
|||
return template.render(message=message)
|
||||
|
||||
def send_raw_email(self):
|
||||
source = self.querystring.get('Source')[0]
|
||||
source = self.querystring.get('Source')
|
||||
if source is not None:
|
||||
source, = source
|
||||
|
||||
raw_data = self.querystring.get('RawMessage.Data')[0]
|
||||
raw_data = base64.b64decode(raw_data)
|
||||
if six.PY3:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue