/
mailsender.py
39 lines (30 loc) · 1.05 KB
/
mailsender.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
import logging
import smtplib
import yaml
class MailSender:
hostname = 'smtp.gmail.com'
port = 465
configfile = 'config.yml'
togglesfile = 'toggles.yaml'
def check_send_toggle(self):
with open(self.togglesfile, "r") as toggles:
toggles = yaml.load(toggles, Loader=yaml.FullLoader)
return toggles["SENDMAIL"]
def send_mail(self, URL, title, price):
with open(self.configfile, "r") as config:
cfg = yaml.load(config, Loader=yaml.FullLoader)
server = smtplib.SMTP(self.hostname, self.port)
server.ehlo()
server.starttls()
server.ehlo()
server.login(cfg["username"], cfg["password"])
subject = title + ' price fell'
body = 'The price of ' + title + ' fell down to ' + price + '\n\n'+ '\n\n Check it out on: ' + URL
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
cfg["mailfrom"],
cfg["mailto"],
msg
)
logging.info('Mail has been sent successfully.')
server.quit()