Created
April 9, 2026 11:20
-
-
Save blog-aspose-cloud/8426b4050752b9b5140628f1757bd113 to your computer and use it in GitHub Desktop.
Create EML File in Python Programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asposeemailcloud | |
| from asposeemailcloud import ApiClient, EmailApi, EmailDto, Attachment, MailAddress | |
| # Initialize API client | |
| client_id = "YOUR_CLIENT_ID" | |
| client_secret = "YOUR_CLIENT_SECRET" | |
| api_client = ApiClient(client_id, client_secret) | |
| email_api = EmailApi(api_client) | |
| # Build the email message | |
| email = EmailDto( | |
| from_address=MailAddress(address="sender@example.com", display_name="Sender"), | |
| to=[MailAddress(address="recipient@example.com", display_name="Recipient")], | |
| subject="Sample EML with Attachments", | |
| body="This email contains an attachment and an inline image.", | |
| body_type="PlainText" | |
| ) | |
| # Add a plain text attachment | |
| attachment = Attachment( | |
| name="notes.txt", | |
| data=b"These are the attached notes." | |
| ) | |
| email.attachments.append(attachment) | |
| # Add an inline image | |
| inline_image = Attachment( | |
| name="image.png", | |
| content_id="image1", | |
| data=open("image.png", "rb").read(), | |
| is_inline=True | |
| ) | |
| email.attachments.append(inline_image) | |
| # Save the message as EML | |
| response = email_api.create(email, format="EML") | |
| with open("output.eml", "wb") as f: | |
| f.write(response) | |
| print("EML file generated successfully.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment