Support for multiple attachments and binary application attachments in GMail
This commit is contained in:
parent
55eb689121
commit
6bfb6d934d
1 changed files with 25 additions and 18 deletions
|
@ -5,8 +5,10 @@ import os
|
||||||
|
|
||||||
from apiclient import discovery
|
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.audio import MIMEAudio
|
||||||
from email.mime.base import MIMEBase
|
# from email.mime.base import MIMEBase
|
||||||
from email.mime.image import MIMEImage
|
from email.mime.image import MIMEImage
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
@ -22,35 +24,40 @@ class GoogleMailPlugin(GooglePlugin):
|
||||||
super().__init__(scopes=self.scopes, *args, **kwargs)
|
super().__init__(scopes=self.scopes, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def compose(self, sender, to, subject, body, file=None):
|
def compose(self, sender, to, subject, body, files=None):
|
||||||
message = MIMEMultipart() if file else MIMEText(body)
|
message = MIMEMultipart() if files else MIMEText(body)
|
||||||
message['to'] = to
|
message['to'] = to
|
||||||
message['from'] = sender
|
message['from'] = sender
|
||||||
message['subject'] = subject
|
message['subject'] = subject
|
||||||
|
|
||||||
if file:
|
if files:
|
||||||
msg = MIMEText(body)
|
for file in files:
|
||||||
message.attach(msg)
|
msg = MIMEText(body)
|
||||||
content_type, encoding = mimetypes.guess_type(file)
|
message.attach(msg)
|
||||||
|
content_type, encoding = mimetypes.guess_type(file)
|
||||||
|
|
||||||
if content_type is None or encoding is not None:
|
if content_type is None or encoding is not None:
|
||||||
content_type = 'application/octet-stream'
|
content_type = 'application/octet-stream'
|
||||||
main_type, sub_type = content_type.split('/', 1)
|
|
||||||
|
main_type, sub_type = content_type.split('/', 1)
|
||||||
|
with open(file, 'rb') as fp: content = fp.read()
|
||||||
|
|
||||||
with open(file, 'rb') as fp:
|
|
||||||
if main_type == 'text':
|
if main_type == 'text':
|
||||||
msg = mimetypes.MIMEText(fp.read(), _subtype=sub_type)
|
msg = mimetypes.MIMEText(content, _subtype=sub_type)
|
||||||
elif main_type == 'image':
|
elif main_type == 'image':
|
||||||
msg = MIMEImage(fp.read(), _subtype=sub_type)
|
msg = MIMEImage(content, _subtype=sub_type)
|
||||||
elif main_type == 'audio':
|
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:
|
else:
|
||||||
msg = MIMEBase(main_type, sub_type)
|
msg = MIMEBase(main_type, sub_type)
|
||||||
msg.set_payload(fp.read())
|
msg.set_payload(content)
|
||||||
|
|
||||||
filename = os.path.basename(file)
|
filename = os.path.basename(file)
|
||||||
msg.add_header('Content-Disposition', 'attachment', filename=filename)
|
msg.add_header('Content-Disposition', 'attachment', filename=filename)
|
||||||
message.attach(msg)
|
message.attach(msg)
|
||||||
|
|
||||||
service = self._get_service()
|
service = self._get_service()
|
||||||
body = { 'raw': base64.urlsafe_b64encode(message.as_bytes()).decode() }
|
body = { 'raw': base64.urlsafe_b64encode(message.as_bytes()).decode() }
|
||||||
|
|
Loading…
Reference in a new issue