-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistribution.py
45 lines (33 loc) · 1.49 KB
/
distribution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import print_function
import base64
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders
from apiclient import errors
from googleapiclient.discovery import build
from settings import OUTPUT_PDF_PATH, EMAIL_BODY, EMAIL_SUBJECT
def send_email_pdf(creds, destination, path_to_pdf=OUTPUT_PDF_PATH, body=EMAIL_BODY, subject=EMAIL_SUBJECT):
try:
email = MIMEMultipart()
email['To'] = destination
email['Subject'] = subject
body = MIMEText(body)
email.attach(body)
with open(path_to_pdf, "rb") as f:
attach = MIMEApplication(f.read(), _subtype="pdf")
attach.add_header('Content-Disposition', 'attachment', filename=path_to_pdf)
attach.add_header('Content-Type', "application", name=path_to_pdf)
# Encode file in ASCII characters to send by email
encoders.encode_base64(attach)
# Add attachment to message and convert message to string
email.attach(attach)
text = {'raw': base64.urlsafe_b64encode(email.as_bytes()).decode()}
# Call the Gmail API
service = build('gmail', 'v1', credentials=creds)
message = (service.users().messages().send(userId="me", body=text)
.execute())
print('Message Id: %s' % message['id'])
return message
except errors.HttpError as error:
print('An error occurred: %s' % error)