|
|
@ -55,10 +55,10 @@ |
|
|
|
# API Documentation: https://developers.sparkpost.com/api/ |
|
|
|
# API Documentation: https://developers.sparkpost.com/api/ |
|
|
|
# Specifically: https://developers.sparkpost.com/api/transmissions/ |
|
|
|
# Specifically: https://developers.sparkpost.com/api/transmissions/ |
|
|
|
import requests |
|
|
|
import requests |
|
|
|
import base64 |
|
|
|
|
|
|
|
from json import loads |
|
|
|
from json import loads |
|
|
|
from json import dumps |
|
|
|
from json import dumps |
|
|
|
from .base import NotifyBase |
|
|
|
from .base import NotifyBase |
|
|
|
|
|
|
|
from .. import exception |
|
|
|
from ..common import NotifyType |
|
|
|
from ..common import NotifyType |
|
|
|
from ..common import NotifyFormat |
|
|
|
from ..common import NotifyFormat |
|
|
|
from ..utils import is_email |
|
|
|
from ..utils import is_email |
|
|
@ -500,7 +500,7 @@ class NotifySparkPost(NotifyBase): |
|
|
|
if not self.targets: |
|
|
|
if not self.targets: |
|
|
|
# There is no one to email; we're done |
|
|
|
# There is no one to email; we're done |
|
|
|
self.logger.warning( |
|
|
|
self.logger.warning( |
|
|
|
'There are no Email recipients to notify') |
|
|
|
'There are no SparkPost Email recipients to notify') |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
# Initialize our has_error flag |
|
|
|
# Initialize our has_error flag |
|
|
@ -546,35 +546,35 @@ class NotifySparkPost(NotifyBase): |
|
|
|
# Prepare ourselves an attachment object |
|
|
|
# Prepare ourselves an attachment object |
|
|
|
payload['content']['attachments'] = [] |
|
|
|
payload['content']['attachments'] = [] |
|
|
|
|
|
|
|
|
|
|
|
for attachment in attach: |
|
|
|
for no, attachment in enumerate(attach, start=1): |
|
|
|
# Perform some simple error checking |
|
|
|
# Perform some simple error checking |
|
|
|
if not attachment: |
|
|
|
if not attachment: |
|
|
|
# We could not access the attachment |
|
|
|
# We could not access the attachment |
|
|
|
self.logger.error( |
|
|
|
self.logger.error( |
|
|
|
'Could not access attachment {}.'.format( |
|
|
|
'Could not access SparkPost attachment {}.'.format( |
|
|
|
attachment.url(privacy=True))) |
|
|
|
attachment.url(privacy=True))) |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
self.logger.debug( |
|
|
|
|
|
|
|
'Preparing SparkPost attachment {}'.format( |
|
|
|
|
|
|
|
attachment.url(privacy=True))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
with open(attachment.path, 'rb') as fp: |
|
|
|
# Prepare API Upload Payload |
|
|
|
# Prepare API Upload Payload |
|
|
|
payload['content']['attachments'].append({ |
|
|
|
payload['content']['attachments'].append({ |
|
|
|
'name': attachment.name |
|
|
|
'name': attachment.name, |
|
|
|
if attachment.name else f'file{no:03}.dat', |
|
|
|
'type': attachment.mimetype, |
|
|
|
'type': attachment.mimetype, |
|
|
|
'data': base64.b64encode(fp.read()).decode("ascii") |
|
|
|
'data': attachment.base64(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
except (OSError, IOError) as e: |
|
|
|
except exception.AppriseException: |
|
|
|
self.logger.warning( |
|
|
|
# We could not access the attachment |
|
|
|
'An I/O error occurred while reading {}.'.format( |
|
|
|
self.logger.error( |
|
|
|
attachment.name if attachment else 'attachment')) |
|
|
|
'Could not access SparkPost attachment {}.'.format( |
|
|
|
self.logger.debug('I/O Exception: %s' % str(e)) |
|
|
|
attachment.url(privacy=True))) |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.logger.debug( |
|
|
|
|
|
|
|
'Appending SparkPost attachment {}'.format( |
|
|
|
|
|
|
|
attachment.url(privacy=True))) |
|
|
|
|
|
|
|
|
|
|
|
# Take a copy of our token dictionary |
|
|
|
# Take a copy of our token dictionary |
|
|
|
tokens = self.tokens.copy() |
|
|
|
tokens = self.tokens.copy() |
|
|
|
|
|
|
|
|
|
|
|