-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmail.py
50 lines (40 loc) · 1.55 KB
/
mail.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
49
50
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
import os
timeout = 15 * 60
last_run = [time.time()]
sender_address = 'mghenrichsen@gmail.com'
sender_pass = os.environ['MY_PASS']
print(sender_pass)
def send_email(in_stock):
print(last_run)
if timeout + last_run[-1] > time.time():
print('We sent out emails less than 15 minutes ago')
else:
with open("emails.txt") as f:
emails = f.readlines()
for email in emails:
session = smtplib.SMTP('smtp.gmail.com', 587)
session.starttls() # enable security
session.login(sender_address, sender_pass)
message = MIMEMultipart()
message['To'] = email
message['From'] = 'PS5 lagerstatus'
content = ''
for i in in_stock:
store = i['store']
product = i['name']
url = i['url']
content = content + 'Playstation ' + product + ' er på lager hos ' + store + '. ' + url + '\n'
message['Subject'] = 'Playstation er på lager!'
print(content)
mail_content = content
message.attach(MIMEText(mail_content, 'plain'))
text = message.as_string()
session.sendmail(sender_address, email, text)
print('Mail Sent to ', email)
session.quit()
last_run.clear()
last_run.append(time.time())