|
1 | 1 | from __future__ import print_function |
2 | 2 |
|
3 | 3 | import base64 |
| 4 | +import mimetypes |
4 | 5 | import os |
| 6 | +from email.mime.audio import MIMEAudio |
| 7 | +from email.mime.base import MIMEBase |
| 8 | +from email.mime.image import MIMEImage |
5 | 9 | from email.mime.multipart import MIMEMultipart |
6 | 10 | from email.mime.text import MIMEText |
7 | 11 |
|
@@ -58,66 +62,64 @@ def SendMessageInternal(service, user_id, message): |
58 | 62 | return "Error" |
59 | 63 | return "OK" |
60 | 64 |
|
61 | | - # Create Mail With AttAchment Remove hash to make the code readable |
62 | | - # def createMessageWithAttachment( |
63 | | - # sender, to, subject, msgHtml, msgPlain, attachmentFile): |
| 65 | + |
| 66 | +def createMessageWithAttachment( |
| 67 | + sender, to, subject, msgHtml, msgPlain, attachmentFile): |
64 | 68 | """Create a message for an email. |
65 | 69 |
|
66 | 70 | Args: |
67 | | - sender: Email address of the sender. |
68 | | - to: Email address of the receiver. |
69 | | - subject: The subject of the email message. |
70 | | - msgHtml: Html message to be sent |
71 | | - msgPlain: Alternative plain text message for older email clients |
72 | | - attachmentFile: The path to the file to be attached. |
| 71 | + sender: Email address of the sender. |
| 72 | + to: Email address of the receiver. |
| 73 | + subject: The subject of the email message. |
| 74 | + msgHtml: Html message to be sent |
| 75 | + msgPlain: Alternative plain text message for older email clients |
| 76 | + attachmentFile: The path to the file to be attached. |
73 | 77 |
|
74 | 78 | Returns: |
75 | | - An object containing a base64url encoded email object. |
| 79 | + An object containing a base64url encoded email object. |
76 | 80 | """ |
77 | | - |
78 | | - |
79 | | -# message = MIMEMultipart('mixed') |
80 | | -# message['to'] = to |
81 | | -# message['from'] = sender |
82 | | -# message['subject'] = subject |
83 | | - |
84 | | -# messageA = MIMEMultipart('alternative') |
85 | | -# messageR = MIMEMultipart('related') |
86 | | - |
87 | | -# messageR.attach(MIMEText(msgHtml, 'html')) |
88 | | -# messageA.attach(MIMEText(msgPlain, 'plain')) |
89 | | -# messageA.attach(messageR) |
90 | | - |
91 | | -# message.attach(messageA) |
92 | | - |
93 | | -# print "create_message_with_attachment: file:", attachmentFile |
94 | | -# content_type, encoding = mimetypes.guess_type(attachmentFile) |
95 | | - |
96 | | -# if content_type is None or encoding is not None: |
97 | | -# content_type = 'application/octet-stream' |
98 | | -# main_type, sub_type = content_type.split('/', 1) |
99 | | -# if main_type == 'text': |
100 | | -# fp = open(attachmentFile, 'rb') |
101 | | -# msg = MIMEText(fp.read(), _subtype=sub_type) |
102 | | -# fp.close() |
103 | | -# elif main_type == 'image': |
104 | | -# fp = open(attachmentFile, 'rb') |
105 | | -# msg = MIMEImage(fp.read(), _subtype=sub_type) |
106 | | -# fp.close() |
107 | | -# elif main_type == 'audio': |
108 | | -# fp = open(attachmentFile, 'rb') |
109 | | -# msg = MIMEAudio(fp.read(), _subtype=sub_type) |
110 | | -# fp.close() |
111 | | -# else: |
112 | | -# fp = open(attachmentFile, 'rb') |
113 | | -# msg = MIMEBase(main_type, sub_type) |
114 | | -# msg.set_payload(fp.read()) |
115 | | -# fp.close() |
116 | | -# filename = os.path.basename(attachmentFile) |
117 | | -# msg.add_header('Content-Disposition', 'attachment', filename=filename) |
118 | | -# message.attach(msg) |
119 | | - |
120 | | -# return {'raw': base64.urlsafe_b64encode(message.as_string())} |
| 81 | + message = MIMEMultipart('mixed') |
| 82 | + message['to'] = to |
| 83 | + message['from'] = sender |
| 84 | + message['subject'] = subject |
| 85 | + |
| 86 | + messageA = MIMEMultipart('alternative') |
| 87 | + messageR = MIMEMultipart('related') |
| 88 | + |
| 89 | + messageR.attach(MIMEText(msgHtml, 'html')) |
| 90 | + messageA.attach(MIMEText(msgPlain, 'plain')) |
| 91 | + messageA.attach(messageR) |
| 92 | + |
| 93 | + message.attach(messageA) |
| 94 | + |
| 95 | + print("create_message_with_attachment: file:", attachmentFile) |
| 96 | + content_type, encoding = mimetypes.guess_type(attachmentFile) |
| 97 | + |
| 98 | + if content_type is None or encoding is not None: |
| 99 | + content_type = 'application/octet-stream' |
| 100 | + main_type, sub_type = content_type.split('/', 1) |
| 101 | + if main_type == 'text': |
| 102 | + fp = open(attachmentFile, 'rb') |
| 103 | + msg = MIMEText(fp.read(), _subtype=sub_type) |
| 104 | + fp.close() |
| 105 | + elif main_type == 'image': |
| 106 | + fp = open(attachmentFile, 'rb') |
| 107 | + msg = MIMEImage(fp.read(), _subtype=sub_type) |
| 108 | + fp.close() |
| 109 | + elif main_type == 'audio': |
| 110 | + fp = open(attachmentFile, 'rb') |
| 111 | + msg = MIMEAudio(fp.read(), _subtype=sub_type) |
| 112 | + fp.close() |
| 113 | + else: |
| 114 | + fp = open(attachmentFile, 'rb') |
| 115 | + msg = MIMEBase(main_type, sub_type) |
| 116 | + msg.set_payload(fp.read()) |
| 117 | + fp.close() |
| 118 | + filename = os.path.basename(attachmentFile) |
| 119 | + msg.add_header('Content-Disposition', 'attachment', filename=filename) |
| 120 | + message.attach(msg) |
| 121 | + |
| 122 | + return {'raw': base64.urlsafe_b64encode(message.as_string())} |
121 | 123 |
|
122 | 124 |
|
123 | 125 | def CreateMessageHtml(sender, to, subject, msgHtml, msgPlain): |
|
0 commit comments