Files
dagster/apps/vinyl/src/utils/email.py

58 lines
1.6 KiB
Python

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import dagster as dg
class EmailService(dg.ConfigurableResource):
"""
Service for sending emails using SMTP.
Attributes:
smtp_server (str): SMTP server address.
smtp_port (int): SMTP server port.
smtp_username (str): SMTP username for authentication.
smtp_password (str): SMTP password for authentication.
sender_email (str): Email address from which the email will be sent.
receiver_email (str): Email address to which the email will be sent.
"""
smtp_server: str = "email-smtp.eu-west-1.amazonaws.com"
smtp_port: int = 587
smtp_username: str
smtp_password: str
sender_email: str
receiver_email: str
def send_email(self, body: str, subject="Vinyl aanbiedingen!") -> None:
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = self.sender_email
msg["To"] = self.receiver_email
# Add HTML content
msg.attach(
MIMEText(
f"""
<html>
<head></head>
<body>
{body}
</body>
</html>
""",
"html",
)
)
# Send
try:
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
server.starttls()
server.login(self.smtp_username, self.smtp_password)
server.send_message(msg)
except Exception as e:
print("Failed to send email:", e)