Encoding - Aspose.Email for Python

Hi Aspose Team,

I’m currently using Aspose.Email for Python to save email messages as HTML. However, I’ve run into an issue where I cannot set the encoding of the resulting HTML output.

In the .NET version of Aspose.Email, there is no encoding property in the HtmlSaveOptions class for Python. It would be extremely helpful if you could add the ability to specify the encoding for HTML output directly in HtmlSaveOptions (for example, like encoding in Aspose.Words).

This would save me from needing to manually patch the HTML after saving, which is currently the only workaround I see.

Thank you for considering this request.

Best regards,
BP

@bpt

Currently, the Aspose.Email for Python library does not provide an encoding property within the HtmlSaveOptions class, as found in the .NET version. This means you cannot directly set the encoding for the HTML output when saving email messages, which can be inconvenient if you prefer a specific encoding format.

As a workaround, you may need to manually adjust the encoding of the HTML output after it has been generated. This involves saving the email as HTML and then modifying the resulting file to include your desired encoding declaration.

Here’s a basic example of how you might approach saving an email as HTML:

import aspose.email as ae

# Create an instance of MailMessage
email = ae.MailMessage()
email.subject = "New MailMessage created with Aspose.Email for Python"
email.html_body = "<b>This line is in bold</b> while this is normal text"
email.from_address = "from@example.com"
email.to.append(ae.MailAddress("to@example.com", "Recipient"))

# Save the email as HTML
email.save("output.html", ae.SaveOptions.default_html)

# After saving, you may need to manually change the encoding in the output.html file.

This code snippet creates a simple email message and saves it as an HTML file. To set the encoding, you would need to open the generated HTML file and manually insert a meta tag for the desired encoding, such as:

<meta charset="UTF-8">

I recommend submitting a feature request to the Aspose team to consider adding an encoding property to the HtmlSaveOptions class in future releases, as this would streamline the process significantly.

For more information, you can refer to the HtmlSaveOptions documentation for additional properties and usage details.

Sources:
[1]: HtmlSaveOptions | Aspose.Email for Python via .NET API

Thank you for the suggested workaround. However, I find this approach quite time-consuming and inefficient—especially when dealing with multiple email exports.

It would be much more practical if the HtmlSaveOptions class in the Python version included an encoding property.

I hope the Aspose team considers this enhancement in a future release.

Hello @bpt,

Thank you for bringing this to our attention. We’re reviewing the issue and will get back to you shortly.

@bpt
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): EMAILNET-41589

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Any updates on this matter? Thank You!

Hello @bpt,

In the latest Aspose.Email ver. 25.7, you can control the encoding of the resulting HTML output by setting the preferred_text_encoding property on the MailMessage instance before saving.

Here’s a sample:

import aspose.email as ae

email = ae.MailMessage()
email.subject = "New MailMessage created with Aspose.Email for Python"
email.html_body = "<b>This line is in bold</b> while this is normal text"
email.from_address = ae.MailAddress("from@example.com")
email.to.append(ae.MailAddress("to@example.com", "Recipient"))

# Set the preferred text encoding (e.g., UTF-8)
email.preferred_text_encoding = "utf-8"

# Save the email as HTML
email.save("output.html", ae.SaveOptions.default_html)

This will ensure the output HTML is saved using the specified encoding.
Thank you.