envelopeEmail

Setting Up Email Notifications

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

circle-exclamation

Notification Implementation

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="[email protected]",
        subject=subject,
        body=body,
        html=True
    )
    
    return pl.LazyFrame(response)

Example Email Templates

chevron-rightHTML formatting and exampleshashtag

Email supports rich HTML formatting for better presentation:

Basic HTML Structure:

Styled Alert Example:

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

Last updated