AltScore
Guides

Sending Emails Using AltScore Python SDK

The SDK provides a user-friendly interface for drafting and sending emails with attachments. The following code illustrates how to do this.

from altscore import AltScoreAsync
from altscore.borrower_central.schemas.communications import MailBody
import base64
import io
import pandas as pd
 
async def send_email(pdf_data: dict, excel_data: dict):
    # First, we create the email structure
    mail_body = MailBody(
        to=[{ "email": "client@enterprise.com" }],
        cc=[{ "email": "other.client@enterprise.com" }],
        subject="Workflow Results",
        content="<h2>Here are the results of the workflow</h2>",
    )
 
    # Now let's create attachments
    pdf_content = create_pdf(**pdf_data)
 
    # We need a way to store the file content in binary
    # because this is the value that needs to be included in the attachment in base64 format
    buffer = io.BytesIO()
    pdf_content.write(buffer)
    buffer.seek(0)
 
    # It's important to include the extension, as this method infers the mimetype based on it
    mail_body.add_attachment("report.pdf", base64.b64encode(buffer.read()).decode("utf-8"))
 
    # Now let's create an Excel file
    example_df = pd.DataFrame.from_dict(excel_data)
    buffer = io.BytesIO()
    example_df.to_excel(buffer)
    buffer.seek(0)
 
    byte_content = buffer.read()
    base64_content = base64.b64encode(byte_content).decode("utf-8")
 
    mail_body.add_attachment("excel.xlsx", base64_content)
 
    # And we're ready to send it
    altscore = AltScoreAsync(**config)
    await altscore.borrower_central.communications.send_mail(mail_body)

Note that the sender of this email will always be workflows@altscore.ai

On this page