-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_notifications.py
More file actions
57 lines (49 loc) · 1.9 KB
/
email_notifications.py
File metadata and controls
57 lines (49 loc) · 1.9 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
50
51
52
53
54
55
56
57
import os
from dotenv import load_dotenv
import smtplib
import logging
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Load environment variables from the .env file
load_dotenv()
# Get email credentials from .env file
sender_email = os.getenv('SENDER_EMAIL')
recipient_email = os.getenv('RECIPIENT_EMAIL')
smtp_server = os.getenv('SMTP_SERVER')
smtp_port = int(os.getenv('SMTP_PORT'))
smtp_username = os.getenv('SMTP_USERNAME')
smtp_password = os.getenv('SMTP_PASSWORD')
# Common SMTP server settings
# smtp_server = "smtp.gmail.com"
# smtp_server = "smtp.office365.com"
# smtp_server = "smtp.mail.yahoo.com"
# smtp_server = "You can obtain this from your domain or email service provider."
# Common SMTP port numbers
# smtp_port = 587 # For TLS (Transport Layer Security)
# smtp_port = 465 # For SSL (Secure Sockets Layer)
# smtp_port = 25 # For non-secure connections
def send_email_notification(subject, body, to_email):
"""Send an email notification with the given subject and body to the specified email address."""
# Prepare the email content
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_username, smtp_password)
server.send_message(msg)
print(f"Email sent to {to_email}")
except Exception as e:
logging.error(f"Failed to send email: {e}")
print(f"Failed to send email: {e}")
# Example usage
if __name__ == "__main__":
new_listings = "Property at 123 Main St.\nProperty at 456 Oak St."
send_email_notification(
subject='New Foreclosure Auction Listings',
body=f"New auction listings found:\n{new_listings}",
to_email=recipient_email
)