-
Notifications
You must be signed in to change notification settings - Fork 1
/
messaging_system.py
60 lines (51 loc) · 1.99 KB
/
messaging_system.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
51
52
53
54
55
56
57
58
59
60
import smtplib
from email.message import EmailMessage
import ssl
import sqlite3
EMAIL_PASSWORD = 'The App Password To Your Email'
def get_client_info(name):
conn = sqlite3.connect('clients.db')
c = conn.cursor()
c.execute("SELECT * FROM clients WHERE name=?", (name,))
data = c.fetchone()
conn.close()
if data:
return {
"name": data[0],
"owe_amount": data[1],
"last_activity": data[2],
"booked_activity": data[3]
}
else:
return None
def send1_email(client_name):
client_info = get_client_info(client_name)
if not client_info: # If no client info found for the client_name, exit the function
print(f"No client info found for {client_name}. Email not sent.")
return
# Construct email details using client_info
email_sender = 'theemailitsendsfrom@gmail.com'
email_recipient = 'example123@gmail.com' # Change this to the recipient's email address
subject = f"Alert: {client_info['name']} detected"
body = f"""{client_info['name']} detected!
Owe Amount: ${client_info['owe_amount']}.
Last Activity: {client_info['last_activity']}.
Booked for: {client_info['booked_activity']}."""
# Print email details
print(f"Sending Email to: {email_recipient}")
print(f"Subject: {subject}")
print(f"Body:\n{body}")
try:
# Below is your code for sending the email.
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_recipient
em['Subject'] = subject
em.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(email_sender, EMAIL_PASSWORD)
smtp.sendmail(email_sender, email_recipient, em.as_string())
print("Email sent successfully")
except Exception as e:
print(f"Email sending failed with error: {e}")