forked from cheezwinkle/Online-Store-Price-Checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendemail.py
40 lines (35 loc) · 1.38 KB
/
sendemail.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
#! python3
import smtplib, logging, configparser
from email.mime.text import MIMEText
config = configparser.ConfigParser()
config.read('config.ini')
emailsettings = config['Email Settings']
def sendemail(newPrice):
logging.info('Begin sending email')
smtp_server = emailsettings['smtp_server']
smtp_port = emailsettings['smtp_port']
smtp_login = emailsettings['login']
smtp_pw = emailsettings['password']
sender = emailsettings['sender']
recipients = emailsettings['recipients'].split(',')
msg = MIMEText('A price change was detected!\nNew price: $' + str(newPrice) + '\n' + config['Webpage Settings']['url'])
msg['Subject'] = "Online Store Price Check Alert"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s = smtplib.SMTP(smtp_server, int(smtp_port))
s.ehlo()
s.starttls()
s.ehlo()
logging.info(msg.as_string())
try:
s.login(smtp_login, smtp_pw)
except Exception as exc:
logging.error('There was an error: %s' % (exc))
try:
s.sendmail(msg['From'], recipients, msg.as_string())
except Exception as exc:
logging.error('There was an error: %s' % (exc))
else:
logging.info('Email sent')
finally:
s.quit()