send email
This commit is contained in:
0
apps/vinyl/src/utils/__init__.py
Normal file
0
apps/vinyl/src/utils/__init__.py
Normal file
57
apps/vinyl/src/utils/email.py
Normal file
57
apps/vinyl/src/utils/email.py
Normal file
@@ -0,0 +1,57 @@
|
||||
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="Aanbieding op plato (new)!") -> 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)
|
||||
38
apps/vinyl/src/utils/parse.py
Normal file
38
apps/vinyl/src/utils/parse.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from datetime import date, datetime
|
||||
|
||||
|
||||
def parse_date(dutch_date: str) -> date:
|
||||
"""
|
||||
Parse a date string in Dutch format (e.g., "1 januari 2023") and return a date object.
|
||||
|
||||
Args:
|
||||
- dutch_date (str): The date string in Dutch format.
|
||||
|
||||
Returns:
|
||||
- date: A date object representing the parsed date.
|
||||
"""
|
||||
# Create a dictionary to map Dutch month names to English
|
||||
dutch_to_english_months = {
|
||||
"januari": "January",
|
||||
"februari": "February",
|
||||
"maart": "March",
|
||||
"april": "April",
|
||||
"mei": "May",
|
||||
"juni": "June",
|
||||
"juli": "July",
|
||||
"augustus": "August",
|
||||
"september": "September",
|
||||
"oktober": "October",
|
||||
"november": "November",
|
||||
"december": "December",
|
||||
}
|
||||
|
||||
# Split the date and replace the Dutch month with its English equivalent
|
||||
day, dutch_month, year = dutch_date.split()
|
||||
english_month = dutch_to_english_months[dutch_month]
|
||||
|
||||
# Rebuild the date string in English format
|
||||
english_date = f"{day} {english_month} {year}"
|
||||
|
||||
# Parse the date
|
||||
return datetime.strptime(english_date, "%d %B %Y").date()
|
||||
Reference in New Issue
Block a user