Created
April 14, 2026 08:09
-
-
Save blog-aspose-cloud/3004c18bccf5be8df640d743d757b757 to your computer and use it in GitHub Desktop.
Convert EML to MSG in Python
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 os | |
| from asposeemailcloud import EmailApi, Configuration, ApiException | |
| # ==== Configuration ==== | |
| client_id = "YOUR_CLIENT_ID" | |
| client_secret = "YOUR_CLIENT_SECRET" | |
| config = Configuration() | |
| config.client_id = client_id | |
| config.client_secret = client_secret | |
| config.base_url = "https://api.aspose.cloud" | |
| email_api = EmailApi(configuration=config) | |
| def convert_eml_to_msg(input_path: str, output_path: str): | |
| try: | |
| # Upload EML file to cloud storage | |
| remote_path = f"Temp/{os.path.basename(input_path)}" | |
| with open(input_path, "rb") as file_stream: | |
| email_api.upload_file(remote_path, file_stream) | |
| # Convert to MSG | |
| conversion_result = email_api.convert( | |
| format="msg", | |
| input_file=remote_path, | |
| output_file=output_path, | |
| storage="Default" | |
| ) | |
| # Download the MSG file | |
| with open(output_path, "wb") as out_file: | |
| out_file.write(conversion_result) | |
| print(f"Converted '{input_path}' to '{output_path}' successfully.") | |
| except ApiException as e: | |
| print(f"Error during conversion: {e}") | |
| # ---- Single file conversion ---- | |
| convert_eml_to_msg("samples/email1.eml", "output/email1.msg") | |
| # ---- Batch conversion ---- | |
| source_folder = "samples/batch_eml" | |
| output_folder = "output/batch_msg" | |
| os.makedirs(output_folder, exist_ok=True) | |
| for filename in os.listdir(source_folder): | |
| if filename.lower().endswith(".eml"): | |
| input_file = os.path.join(source_folder, filename) | |
| output_file = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.msg") | |
| convert_eml_to_msg(input_file, output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment