|
2 | 2 | """ |
3 | 3 |
|
4 | 4 | import base64 |
| 5 | +import mimetypes |
| 6 | +import os |
5 | 7 | from email.mime.audio import MIMEAudio |
6 | 8 | from email.mime.base import MIMEBase |
7 | 9 | from email.mime.image import MIMEImage |
8 | 10 | from email.mime.multipart import MIMEMultipart |
9 | 11 | from email.mime.text import MIMEText |
10 | | -import mimetypes |
11 | | -import os |
12 | 12 |
|
13 | 13 | from apiclient import errors |
14 | 14 |
|
15 | 15 |
|
16 | 16 | # [START create_draft] |
17 | 17 | def create_draft(service, user_id, message_body): |
18 | | - """Create and insert a draft email. Print the returned draft's message and id. |
19 | | -
|
20 | | - Args: |
21 | | - service: Authorized Gmail API service instance. |
22 | | - user_id: User's email address. The special value "me" |
23 | | - can be used to indicate the authenticated user. |
24 | | - message_body: The body of the email message, including headers. |
25 | | -
|
26 | | - Returns: |
27 | | - Draft object, including draft id and message meta data. |
28 | | - """ |
29 | | - try: |
30 | | - message = {'message': message_body} |
31 | | - draft = service.users().drafts().create(userId=user_id, body=message).execute() |
32 | | - |
33 | | - print('Draft id: %s\nDraft message: %s' % (draft['id'], draft['message'])) |
34 | | - |
35 | | - return draft |
36 | | - except errors.HttpError as error: |
37 | | - print('An error occurred: %s' % error) |
38 | | - return None |
| 18 | + """Create and insert a draft email. Print the returned draft's message and id. |
| 19 | +
|
| 20 | + Args: |
| 21 | + service: Authorized Gmail API service instance. |
| 22 | + user_id: User's email address. The special value "me" |
| 23 | + can be used to indicate the authenticated user. |
| 24 | + message_body: The body of the email message, including headers. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + Draft object, including draft id and message meta data. |
| 28 | + """ |
| 29 | + try: |
| 30 | + message = {'message': message_body} |
| 31 | + draft = service.users().drafts().create(userId=user_id, body=message).execute() |
| 32 | + |
| 33 | + print('Draft id: %s\nDraft message: %s' % (draft['id'], draft['message'])) |
| 34 | + |
| 35 | + return draft |
| 36 | + except errors.HttpError as error: |
| 37 | + print('An error occurred: %s' % error) |
| 38 | + return None |
| 39 | + |
| 40 | + |
39 | 41 | # [END create_draft] |
40 | 42 |
|
41 | 43 |
|
42 | 44 | # [START send_email] |
43 | 45 | def send_message(service, user_id, message): |
44 | | - """Send an email message. |
45 | | -
|
46 | | - Args: |
47 | | - service: Authorized Gmail API service instance. |
48 | | - user_id: User's email address. The special value "me" |
49 | | - can be used to indicate the authenticated user. |
50 | | - message: Message to be sent. |
51 | | -
|
52 | | - Returns: |
53 | | - Sent Message. |
54 | | - """ |
55 | | - try: |
56 | | - message = (service.users().messages().send(userId=user_id, body=message) |
57 | | - .execute()) |
58 | | - print('Message Id: %s' % message['id']) |
59 | | - return message |
60 | | - except errors.HttpError as error: |
61 | | - print('An error occurred: %s' % error) |
| 46 | + """Send an email message. |
| 47 | +
|
| 48 | + Args: |
| 49 | + service: Authorized Gmail API service instance. |
| 50 | + user_id: User's email address. The special value "me" |
| 51 | + can be used to indicate the authenticated user. |
| 52 | + message: Message to be sent. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + Sent Message. |
| 56 | + """ |
| 57 | + try: |
| 58 | + message = (service.users().messages().send(userId=user_id, body=message) |
| 59 | + .execute()) |
| 60 | + print('Message Id: %s' % message['id']) |
| 61 | + return message |
| 62 | + except errors.HttpError as error: |
| 63 | + print('An error occurred: %s' % error) |
| 64 | + |
| 65 | + |
62 | 66 | # [END send_email] |
63 | 67 |
|
64 | 68 |
|
65 | 69 | # [START create_message] |
66 | 70 | def create_message(sender, to, subject, message_text): |
67 | | - """Create a message for an email. |
68 | | -
|
69 | | - Args: |
70 | | - sender: Email address of the sender. |
71 | | - to: Email address of the receiver. |
72 | | - subject: The subject of the email message. |
73 | | - message_text: The text of the email message. |
74 | | -
|
75 | | - Returns: |
76 | | - An object containing a base64url encoded email object. |
77 | | - """ |
78 | | - message = MIMEText(message_text) |
79 | | - message['to'] = to |
80 | | - message['from'] = sender |
81 | | - message['subject'] = subject |
82 | | - return {'raw': base64.urlsafe_b64encode(message.as_string())} |
| 71 | + """Create a message for an email. |
| 72 | +
|
| 73 | + Args: |
| 74 | + sender: Email address of the sender. |
| 75 | + to: Email address of the receiver. |
| 76 | + subject: The subject of the email message. |
| 77 | + message_text: The text of the email message. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + An object containing a base64url encoded email object. |
| 81 | + """ |
| 82 | + message = MIMEText(message_text) |
| 83 | + message['to'] = to |
| 84 | + message['from'] = sender |
| 85 | + message['subject'] = subject |
| 86 | + return {'raw': base64.urlsafe_b64encode(message.as_string())} |
| 87 | + |
| 88 | + |
83 | 89 | # [END create_message] |
84 | 90 |
|
85 | 91 |
|
86 | 92 | # [START create_message_attachment] |
87 | 93 | def create_message_with_attachment( |
88 | | - sender, to, subject, message_text, file): |
89 | | - """Create a message for an email. |
90 | | -
|
91 | | - Args: |
92 | | - sender: Email address of the sender. |
93 | | - to: Email address of the receiver. |
94 | | - subject: The subject of the email message. |
95 | | - message_text: The text of the email message. |
96 | | - file: The path to the file to be attached. |
97 | | -
|
98 | | - Returns: |
99 | | - An object containing a base64url encoded email object. |
100 | | - """ |
101 | | - message = MIMEMultipart() |
102 | | - message['to'] = to |
103 | | - message['from'] = sender |
104 | | - message['subject'] = subject |
105 | | - |
106 | | - msg = MIMEText(message_text) |
107 | | - message.attach(msg) |
108 | | - |
109 | | - content_type, encoding = mimetypes.guess_type(file) |
110 | | - |
111 | | - if content_type is None or encoding is not None: |
112 | | - content_type = 'application/octet-stream' |
113 | | - main_type, sub_type = content_type.split('/', 1) |
114 | | - if main_type == 'text': |
115 | | - fp = open(file, 'rb') |
116 | | - msg = MIMEText(fp.read(), _subtype=sub_type) |
117 | | - fp.close() |
118 | | - elif main_type == 'image': |
119 | | - fp = open(file, 'rb') |
120 | | - msg = MIMEImage(fp.read(), _subtype=sub_type) |
121 | | - fp.close() |
122 | | - elif main_type == 'audio': |
123 | | - fp = open(file, 'rb') |
124 | | - msg = MIMEAudio(fp.read(), _subtype=sub_type) |
125 | | - fp.close() |
126 | | - else: |
127 | | - fp = open(file, 'rb') |
128 | | - msg = MIMEBase(main_type, sub_type) |
129 | | - msg.set_payload(fp.read()) |
130 | | - fp.close() |
131 | | - filename = os.path.basename(file) |
132 | | - msg.add_header('Content-Disposition', 'attachment', filename=filename) |
133 | | - message.attach(msg) |
134 | | - |
135 | | - return {'raw': base64.urlsafe_b64encode(message.as_string())} |
| 94 | + sender, to, subject, message_text, file): |
| 95 | + """Create a message for an email. |
| 96 | +
|
| 97 | + Args: |
| 98 | + sender: Email address of the sender. |
| 99 | + to: Email address of the receiver. |
| 100 | + subject: The subject of the email message. |
| 101 | + message_text: The text of the email message. |
| 102 | + file: The path to the file to be attached. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + An object containing a base64url encoded email object. |
| 106 | + """ |
| 107 | + message = MIMEMultipart() |
| 108 | + message['to'] = to |
| 109 | + message['from'] = sender |
| 110 | + message['subject'] = subject |
| 111 | + |
| 112 | + msg = MIMEText(message_text) |
| 113 | + message.attach(msg) |
| 114 | + |
| 115 | + content_type, encoding = mimetypes.guess_type(file) |
| 116 | + |
| 117 | + if content_type is None or encoding is not None: |
| 118 | + content_type = 'application/octet-stream' |
| 119 | + main_type, sub_type = content_type.split('/', 1) |
| 120 | + if main_type == 'text': |
| 121 | + fp = open(file, 'rb') |
| 122 | + msg = MIMEText(fp.read(), _subtype=sub_type) |
| 123 | + fp.close() |
| 124 | + elif main_type == 'image': |
| 125 | + fp = open(file, 'rb') |
| 126 | + msg = MIMEImage(fp.read(), _subtype=sub_type) |
| 127 | + fp.close() |
| 128 | + elif main_type == 'audio': |
| 129 | + fp = open(file, 'rb') |
| 130 | + msg = MIMEAudio(fp.read(), _subtype=sub_type) |
| 131 | + fp.close() |
| 132 | + else: |
| 133 | + fp = open(file, 'rb') |
| 134 | + msg = MIMEBase(main_type, sub_type) |
| 135 | + msg.set_payload(fp.read()) |
| 136 | + fp.close() |
| 137 | + filename = os.path.basename(file) |
| 138 | + msg.add_header('Content-Disposition', 'attachment', filename=filename) |
| 139 | + message.attach(msg) |
| 140 | + |
| 141 | + return {'raw': base64.urlsafe_b64encode(message.as_string())} |
136 | 142 | # [END create_message_attachment] |
0 commit comments