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