-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_sender.py
50 lines (37 loc) · 1.29 KB
/
email_sender.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
46
47
48
#!/usr/bin/env python3
#
# Class to send email.
# @author Tomasz Zymni <[email protected]>
#
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
import yaml
# Load configuration from yaml file.
def LoadConfig():
global config
with open("config.yaml", "r") as yamlfile:
config = yaml.load(yamlfile, Loader=yaml.FullLoader)
class EmailSender:
def __init__(self):
LoadConfig()
# Send email based on configuration with file attached in parameter.
def SendMail(self, ImgFileName):
img_data = open(ImgFileName, 'rb').read()
msg = MIMEMultipart()
msg['Subject'] = config["email"]['subject']
msg['From'] = config["email"]['from']
msg['To'] = config["email"]['to']
text = MIMEText(config["email"]['message'])
msg.attach(text)
image = MIMEImage(img_data, name=os.path.basename(ImgFileName))
msg.attach(image)
s = smtplib.SMTP(config["smtp"]['host'], config["smtp"]['port'])
s.ehlo()
s.starttls()
s.ehlo()
s.login(config["smtp"]['user'], config["smtp"]['password'])
s.sendmail(config["email"]['from'], config["email"]['to'], msg.as_string())
s.quit()