-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_sender.py
44 lines (36 loc) · 1.66 KB
/
email_sender.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
from datetime import datetime
import smtplib
class SendEmail:
"""
A class to send an email with the given message and flight details.
"""
def __init__(self, message):
"""
Initializes the SendEmail object with the message to be sent.
:param message: The content of the email (message body)
"""
self.message = message
def send_email(self, email, password, to_add):
"""
Sends an email using the provided Gmail credentials to the specified recipient.
:param email: Sender's email address
:param password: Sender's email password (or app-specific password)
:param to_add: Recipient's email address
:return: Status message confirming the email was sent
"""
try:
# Establishing connection to the Gmail SMTP server
with smtplib.SMTP(host="smtp.gmail.com", port=587) as connection:
connection.starttls() # Start TLS encryption for security
connection.login(user=email, password=password) # Login with provided credentials
# Creating the email content
subject = "These are the cheapest price flights today"
date_today = datetime.today().date()
email_message = f"Subject: {subject}\n\nDate: {date_today}\n{self.message}"
# Sending the email
connection.sendmail(from_addr=email,
to_addrs=to_add,
msg=email_message.encode("utf-8"))
return "Email sent successfully"
except Exception as e:
return f"Failed to send email: {e}"