Generate Email Messages from Data

Overview

GroupDocs.Assembly for Python via .NET enables you to generate email messages dynamically from templates. You can set email attributes like subject, body, and recipients (To, CC, BCC) using template syntax, making it easy to create personalized email messages from data sources.

The main classes involved are:

Here are the steps to generate email messages:

  • Create an email template (MSG or EML format)
  • Set email attributes (subject, To, CC, BCC) using template syntax
  • Create email body content with data binding
  • Assemble the email message with your data source
  • Save the assembled email
Note
Email templates support the same template syntax as Word documents, including expression tags, data bands, conditional blocks, and formatting.

The templates on this page are EML files: MIME messages in plain text, which any text editor can write. In an EML template, a tag in an address header (From, To, Cc, Bcc) sits in a quoted display name, followed by any placeholder address: To: "<<[email.Recipient]>>" <recipient@harborlane.example>. The assembled header holds the addresses the tag gives, and the placeholder is dropped. Without the quotes, the header is read as an address list before the tag is evaluated, and the tag is lost. An MSG template made in Outlook takes the same tags in its subject, recipient fields and body.

Generate Email with Subject and Body

Create a simple email with dynamic subject and body:

from decimal import Decimal

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

email_data = {
    "Recipient": "orders@quaysidebistro.example",
    "Subject": "Invoice #1001",
    "CustomerName": "Quayside Bistro",
    "InvoiceAmount": Decimal("1500.00"),
}

assembler = DocumentAssembler()
assembler.assemble_document("invoice-notification.eml", "invoice-notification-report.eml",
                            DataSourceInfo(email_data, "email"))

print("Email generated successfully.")

The following sample file is used in this example: invoice-notification.eml

Binary file (EML, 786 bytes)

Download full output

Email Template Structure

In your email template (invoice-notification.eml), set attributes and body:

Subject field:

<<[email.Subject]>>

To field:

"<<[email.Recipient]>>" <recipient@harborlane.example>

Body:

Dear <<[email.CustomerName]>>,

Please find your invoice below:

Amount: <<[email.InvoiceAmount]:"C">>

Thank you for your business.

Generate Email with Multiple Recipients

Set multiple recipients using foreach:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

email_data = {
    "Recipients": [
        "orders@quaysidebistro.example",
        "orders@lanternstreetcafe.example",
    ],
    "Subject": "Important Update",
    "Message": "Our warehouse at 18 Quayside Road closes at noon on Friday, 16 January 2015.",
}

assembler = DocumentAssembler()
assembler.assemble_document("warehouse-update.eml", "warehouse-update-report.eml",
                            DataSourceInfo(email_data, "email"))

print("Email with multiple recipients generated successfully.")

The following sample file is used in this example: warehouse-update.eml

Binary file (EML, 763 bytes)

Download full output

Template for multiple recipients:

To field:

"<<foreach [r in email.Recipients]>><<[r]>>; <</foreach>>" <recipients@harborlane.example>

Body:

<<[email.Message]>>

The data band writes the addresses separated by semicolons, and the message gets one recipient per address. The list can also be a data source of its own, DataSourceInfo(addresses, "recipients"), read by <<foreach [r in recipients]>>; an expression tag alone, <<[recipients]>>, would write the name of the list type instead of the addresses.

Generate Email with CC and BCC

Set CC and BCC recipients:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

email_data = {
    "To": "orders@quaysidebistro.example",
    "Cc": "mei.tanaka@harborlane.example",
    "Bcc": "archive@harborlane.example",
    "Subject": "Invoice",
    "Body": "Please find attached invoice.",
}

assembler = DocumentAssembler()
assembler.assemble_document("invoice-cc-bcc.eml", "invoice-cc-bcc-report.eml",
                            DataSourceInfo(email_data, "email"))

print("Email with CC and BCC generated successfully.")

The following sample file is used in this example: invoice-cc-bcc.eml

Binary file (EML, 742 bytes)

Download full output

Template:

To field:

"<<[email.To]>>" <recipient@harborlane.example>

CC field:

"<<[email.Cc]>>" <copy@harborlane.example>

BCC field:

"<<[email.Bcc]>>" <blind-copy@harborlane.example>

Body:

<<[email.Body]>>

Generate Email with Rich Content

Create emails with lists, totals and formatted content:

from decimal import Decimal

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

order_email = {
    "CustomerName": "Quayside Bistro",
    "Email": "orders@quaysidebistro.example",
    "Subject": "Order Confirmation",
    "Items": [
        {"ProductName": "Harbor Blend whole bean coffee", "Price": Decimal("18.40")},
        {"ProductName": "Aged harbour cheddar", "Price": Decimal("16.50")},
    ],
}

assembler = DocumentAssembler()
assembler.assemble_document("order-confirmation.eml", "order-confirmation-report.eml",
                            DataSourceInfo(order_email, "order"))

print("Rich email generated successfully.")

The following sample file is used in this example: order-confirmation.eml

Binary file (EML, 870 bytes)

Download full output

Template body:

Dear <<[order.CustomerName]>>,

Thank you for your order. Here are your items:

<<foreach [in order.Items]>>• <<[ProductName]>> - <<[Price]:"C">>
<</foreach>>
Total: <<[order.Items.Sum(i => i.Price)]:"C">>

Generate Email from Collection

Generate multiple emails from a collection, one assemble_document call per item:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

customers = [
    {
        "Email": "orders@quaysidebistro.example",
        "Name": "Quayside Bistro",
        "Subject": "Personalized Message",
    },
    {
        "Email": "orders@saltmarshdeli.example",
        "Name": "Saltmarsh Deli",
        "Subject": "Personalized Message",
    },
]

# Generate email for each customer
assembler = DocumentAssembler()
for customer in customers:
    output_path = "personal-greeting-{}.eml".format(customer["Name"].lower().replace(" ", "-"))
    assembler.assemble_document("personal-greeting.eml", output_path,
                                DataSourceInfo(customer, "customer"))

print("Emails generated from collection successfully.")

The following sample file is used in this example: personal-greeting.eml

personal-greeting-quayside-bistro.eml (897 bytes)
personal-greeting-saltmarsh-deli.eml (895 bytes)

Download full output

The template addresses the message to "<<[customer.Email]>>", puts <<[customer.Subject]>> in the subject and greets <<[customer.Name]>> in the body. The example writes personal-greeting-quayside-bistro.eml and personal-greeting-saltmarsh-deli.eml.

Set Email From Address

Set the sender (From) address:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

email_data = {
    "From": "accounts@harborlane.example",
    "To": "orders@quaysidebistro.example",
    "Subject": "Statement for January 2015",
}

assembler = DocumentAssembler()
assembler.assemble_document("sender-address.eml", "sender-address-report.eml",
                            DataSourceInfo(email_data, "email"))

print("Email with From address generated successfully.")

The following sample file is used in this example: sender-address.eml

Binary file (EML, 725 bytes)

Download full output

Template:

From field:

"<<[email.From]>>" <sender@harborlane.example>
Warning
Email templates must be in MSG or EML format. The template structure depends on the email client used to create it. Ensure proper formatting for HTML or RTF email bodies.

Advanced Usage Topics

To learn more about email attachments, HTML email bodies, email headers, and advanced email generation scenarios, please refer to the advanced usage section.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured library, we provide simple but powerful free online apps.

To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.