forked from x4nth055/pythoncode-tutorials
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_emails.py
More file actions
Latest commit
76 lines (69 loc) · 2.86 KB
/
Copy pathsend_emails.py
File metadata and controls
76 lines (69 loc) · 2.86 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# for getting full paths to attachements
importos
# for encoding messages in base64
frombase64importurlsafe_b64encode
# for dealing with attachement MIME types
fromemail.mime.textimportMIMEText
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.imageimportMIMEImage
fromemail.mime.audioimportMIMEAudio
fromemail.mime.baseimportMIMEBase
fromemail.mime.multipartimportMIMEMultipart
frommimetypesimportguess_typeasguess_mime_type
fromcommonimportour_email, gmail_authenticate
# Adds the attachment with the given filename to the given message
defadd_attachment(message, filename):
content_type, encoding=guess_mime_type(filename)
ifcontent_typeisNoneorencodingisnotNone:
content_type='application/octet-stream'
main_type, sub_type=content_type.split('/', 1)
ifmain_type=='text':
fp=open(filename, 'rb')
msg=MIMEText(fp.read().decode(), _subtype=sub_type)
fp.close()
elifmain_type=='image':
fp=open(filename, 'rb')
msg=MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elifmain_type=='audio':
fp=open(filename, 'rb')
msg=MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
else:
fp=open(filename, 'rb')
msg=MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename=os.path.basename(filename)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
defbuild_message(destination, obj, body, attachments=[]):
ifnotattachments: # no attachments given
message=MIMEText(body)
message['to'] =destination
message['from'] =our_email
message['subject'] =obj
else:
message=MIMEMultipart()
message['to'] =destination
message['from'] =our_email
message['subject'] =obj
message.attach(MIMEText(body))
forfilenameinattachments:
add_attachment(message, filename)
return {'raw': urlsafe_b64encode(message.as_bytes()).decode()}
defsend_message(service, destination, obj, body, attachments=[]):
returnservice.users().messages().send(
userId="me",
body=build_message(destination, obj, body, attachments)
).execute()
if__name__=="__main__":
importargparse
parser=argparse.ArgumentParser(description="Email Sender using Gmail API")
parser.add_argument('destination', type=str, help='The destination email address')
parser.add_argument('subject', type=str, help='The subject of the email')
parser.add_argument('body', type=str, help='The body of the email')
parser.add_argument('-f', '--files', type=str, help='email attachments', nargs='+')
args=parser.parse_args()
service=gmail_authenticate()
send_message(service, args.destination, args.subject, args.body, args.files)