Support for multiple attachments and binary application attachments in GMail

This commit is contained in:
Fabio Manganiello 2018-01-28 02:59:19 +01:00
parent 55eb689121
commit 6bfb6d934d

View file

@ -5,8 +5,10 @@ import os
from apiclient import discovery
from email.encoders import encode_base64
from email.mime.application import MIMEApplication
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
# from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
@ -22,31 +24,36 @@ class GoogleMailPlugin(GooglePlugin):
super().__init__(scopes=self.scopes, *args, **kwargs)
def compose(self, sender, to, subject, body, file=None):
message = MIMEMultipart() if file else MIMEText(body)
def compose(self, sender, to, subject, body, files=None):
message = MIMEMultipart() if files else MIMEText(body)
message['to'] = to
message['from'] = sender
message['subject'] = subject
if file:
if files:
for file in files:
msg = MIMEText(body)
message.attach(msg)
content_type, encoding = mimetypes.guess_type(file)
if content_type is None or encoding is not None:
content_type = 'application/octet-stream'
main_type, sub_type = content_type.split('/', 1)
with open(file, 'rb') as fp:
main_type, sub_type = content_type.split('/', 1)
with open(file, 'rb') as fp: content = fp.read()
if main_type == 'text':
msg = mimetypes.MIMEText(fp.read(), _subtype=sub_type)
msg = mimetypes.MIMEText(content, _subtype=sub_type)
elif main_type == 'image':
msg = MIMEImage(fp.read(), _subtype=sub_type)
msg = MIMEImage(content, _subtype=sub_type)
elif main_type == 'audio':
msg = MIMEAudio(fp.read(), _subtype=sub_type)
msg = MIMEAudio(content, _subtype=sub_type)
elif main_type == 'application':
msg = MIMEApplication(content, _subtype=sub_type,
_encoder=encode_base64)
else:
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
msg.set_payload(content)
filename = os.path.basename(file)
msg.add_header('Content-Disposition', 'attachment', filename=filename)