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.
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:
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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembleremail_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.")
"<<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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembleremail_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.")
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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblercustomers=[{"Email":"orders@quaysidebistro.example","Name":"Quayside Bistro","Subject":"Personalized Message",},{"Email":"orders@saltmarshdeli.example","Name":"Saltmarsh Deli","Subject":"Personalized Message",},]# Generate email for each customerassembler=DocumentAssembler()forcustomerincustomers: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 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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembleremail_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.")
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: