Skip to content

Email

To send email notifications from DataSpace, you need SMTP credentials from your email service provider.

Once you have your SMTP credentials configured as environment variables, you can send email notifications using Python’s built-in smtplib library.

import polars as pl
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
SMTP_SERVER = os.environ["SMTP_SERVER"]
SMTP_PORT = int(os.environ.get("SMTP_PORT", "587"))
SMTP_USERNAME = os.environ["SMTP_USERNAME"]
SMTP_PASSWORD = os.environ["SMTP_PASSWORD"]
FROM_EMAIL = os.environ["FROM_EMAIL"]
def send_email(to_email, subject, body, html=False):
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = FROM_EMAIL
message["To"] = to_email
# Add body to email
content_type = "html" if html else "plain"
message.attach(MIMEText(body, content_type))
try:
# Connect to SMTP server
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls() # Secure the connection
server.login(SMTP_USERNAME, SMTP_PASSWORD)
server.send_message(message)
print(f"Email sent successfully to {to_email}")
return {"status": "success", "recipient": to_email}
except Exception as e:
print(f"Failed to send email: {str(e)}")
return {"status": "error", "message": str(e)}
def transform():
subject = "⚠️ New Alert from DataSpace"
body = """
<html>
<body>
<h2>Alert Notification</h2>
<p>This is an automated notification from your DataSpace transformation.</p>
<p><strong>Status:</strong> Action Required</p>
</body>
</html>
"""
response = send_email(
to_email="recipient@example.com",
subject=subject,
body=body,
html=True
)
return pl.LazyFrame(response)
HTML formatting and examples

Email supports rich HTML formatting for better presentation:

Basic HTML Structure:

<html>
<body>
<h1>Main Heading</h1>
<p>Paragraph text with <strong>bold</strong> and <em>italic</em>.</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
<a href="https://example.com">Link</a>
</body>
</html>

Styled Alert Example:

<html>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<div style="background-color: #f8d7da; border: 1px solid #f5c6cb;
border-radius: 4px; padding: 15px; color: #721c24;">
<h3 style="margin-top: 0;">⚠️ Alert</h3>
<p>Your data pipeline has detected an anomaly.</p>
<p><strong>Time:</strong> 2025-02-28 14:30:00</p>
</div>
</body>
</html>

You can also include tables, images, and custom styling for professional-looking notifications.