-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessenger.py
More file actions
49 lines (41 loc) · 1.54 KB
/
messenger.py
File metadata and controls
49 lines (41 loc) · 1.54 KB
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
import smtplib
from datetime import datetime
from smtplib import SMTPAuthenticationError, SMTPException
from email.mime.text import MIMEText
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# List the sender and recepient emails
EMAIL1 = os.getenv("EMAIL1")
EMAIL2 = os.getenv("EMAIL2")
PASSWORD = os.getenv("PASSWORD")
def send_messenge(msg):
"""Form email and send it via gmail server"""
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server:
smtp_server.login(EMAIL2, PASSWORD)
smtp_server.sendmail(EMAIL2, EMAIL1, msg.as_string())
except SMTPAuthenticationError:
print('Error: Authentication failed. Check your passwords')
except ConnectionRefusedError:
print('Error: The server refused connection. Check your internet and SMTP settings')
except SMTPException as e:
print('Error')
except Exception as e:
print('Error')
else:
print('Email sent successfully')
finally:
try:
# Ensure that the server connection is closed
smtp_server.quit()
except:
pass
def build_email(symbol, price, level):
msg = MIMEText(f"{symbol} has reached your {level} level at {datetime.now()} and is now ${float(price):,.2f}")
msg['Subject'] = f'Important {symbol} Price Update: {level} Price Reached'
msg['From'] = EMAIL2
msg['To'] = EMAIL1
print(f"{symbol} has reached your {level} level price and is now ${float(price):,.2f}")
send_messenge(msg)