EmailClient.forge_message: add attachment_payloads parameter

This commit is contained in:
Benjamin Renard 2021-04-19 14:48:06 +02:00
parent 49ed5d7a82
commit 4d91675d7a

View file

@ -63,7 +63,8 @@ class EmailClient(object): # pylint: disable=useless-object-inheritance,too-man
self.encoding = encoding self.encoding = encoding
def forge_message(self, rcpt_to, subject=None, html_body=None, text_body=None, attachment_files=None, def forge_message(self, rcpt_to, subject=None, html_body=None, text_body=None, attachment_files=None,
sender_name=None, sender_email=None, encoding=None, template=None, **template_vars): # pylint: disable=too-many-arguments,too-many-locals attachment_payloads=None, sender_name=None, sender_email=None, encoding=None,
template=None, **template_vars): # pylint: disable=too-many-arguments,too-many-locals
""" """
Forge a message Forge a message
@ -72,6 +73,7 @@ class EmailClient(object): # pylint: disable=useless-object-inheritance,too-man
:param html_body: The HTML body of the email :param html_body: The HTML body of the email
:param text_body: The plain text body of the email :param text_body: The plain text body of the email
:param attachment_files: List of filepaths to attach :param attachment_files: List of filepaths to attach
:param attachment_payloads: List of tuples with filename and payload to attach
:param sender_name: Custom sender name (default: as defined on initialization) :param sender_name: Custom sender name (default: as defined on initialization)
:param sender_email: Custom sender email (default: as defined on initialization) :param sender_email: Custom sender email (default: as defined on initialization)
:param encoding: Email content encoding (default: as defined on initialization) :param encoding: Email content encoding (default: as defined on initialization)
@ -122,6 +124,13 @@ class EmailClient(object): # pylint: disable=useless-object-inheritance,too-man
encode_base64(part) encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filepath)) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(filepath))
msg.attach(part) msg.attach(part)
if attachment_payloads:
for filename, payload in attachment_payloads:
part = MIMEBase('application', "octet-stream")
part.set_payload(payload)
encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename)
msg.attach(part)
return msg return msg
def send(self, rcpt_to, msg=None, subject=None, just_try=False, **forge_args): def send(self, rcpt_to, msg=None, subject=None, just_try=False, **forge_args):