-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify_message.py
43 lines (37 loc) · 1.86 KB
/
notify_message.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
import os
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def message(subject="Python Notification", text="", img=None, attachment=None):
# build message contents
msg = MIMEMultipart()
msg['Subject'] = subject #add in the subject
msg.attach(MIMEText(text)) # add text contents
# check if we have anyhing given in the img parameter
if img is not None:
# if we do, we want to iterate through the images , so let's check that
# what we have is actually a list
if type(img) is not list:
img = [img] #if it isn't a list, make it one
#now iterate through our list
for one_img in img:
img_data = open(one_img, 'rb').read() # read the image binary data
# attach the image data to MIMEMultipart using MIMEImage, we add
# the given filename use os.basename
msg.attach(MIMEImage(img_data, name=os.path.basename(one_img)))
# we do the same for attachments as we did for images
if attachment is not None:
if type(attachment) is not list:
attachment = [attachment] #if it isn't a list make it one
for one_attachment in attachment:
with open(one_attachment, 'rb' ) as f:
#read in the attacment using MIMEApplication
file = MIMEApplication(
f.read(),
name=os.path.basename(one_attachment)
)
# here we edit the attachment file metadata
file['Content-Disposition'] = f'attachment; filename="{os.path.basename(one_attachment)}"'
msg.attach(file) #finally, add the attchment to our message object
return msg