As in the subject I am trying to implement sending e-mail with multiple language versions recognized on the basis of the language in the receiver browser.
I would like to send the same email message in English and Spanish for example, but the receiver will see only one option in his preferred (by email provider settings) language version.
I am trying to build it on the basis of https://tools.ietf.org/id/draft-ietf-slim-multilangcontent-14.html#Examples, but I am not sure if it is even possible because I didn't find any working implementation of this example. I am using Python and Pyramid Mailer.
I could not set properly 2 different Content-Language in one email message.
Please help me if you have any idea if it is possible to send this type of emails.
Like the example you link to shows, you have to have multiple MIME parts, each with a separate Content-Language:
header.
Here's an attempt at recreating the sample. Unfortunately, Python seems to scrub the Content-Language:
header if you add it with suben['Content-Language'] = 'en-GB'
and move it to the headers of the attachment with add_header
, so I'm not sure this is going to work at all. There is also a pesky conflict between the Content-Disposition:
I add and the one added by Python. If I use msg.add_attachment()suben, 'rfc822', 'inline')
I get an angry error message saying 'inline'
is not supported for message/rfc822
parts. (Maybe you'd need to create a new content manager for this type?)
from email.message import EmailMessage
msg = EmailMessage()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'Example of a message in Spanish and English'
msg['Content-Disposition'] = 'inline' # redundant?
msg.set_content = """\
This is a message in multiple languages. It says the
same thing in each language. If you can read it in one language,
you can ignore the other translations. The other translations may be
presented as attachments or grouped together.
Este es un mensaje en varios idiomas. Dice lo mismo en
cada idioma. Si puede leerlo en un idioma, puede ignorar las otras
traducciones. Las otras traducciones pueden presentarse como archivos
adjuntos o agrupados.
"""
suben = EmailMessage()
# suben['Content-Language'] = 'en-GB'
suben['Content-Translation-Type'] = 'original'
# suben['Content-Disposition'] = 'inline' # redundant?
suben['Subject'] = 'Example of a message in Spanish and English'
suben.set_content("Hello, this message content is provided in your language.")
suben.add.header('Content-Language', 'en-GB')
suben.add_header('Content-Disposition', 'inline')
subes = EmailMessage()
# subes['Content-Language'] = 'es-ES'
subes['Content-Translation-Type'] = 'human'
# subes['Content-Disposition'] = 'inline' # redundant?
subes['Subject'] = 'Ejemplo práctico de mensaje en español e inglés'
subes.set_content("Hola, el contenido de este mensaje esta disponible en su idioma.")
subes.add_header('Content-Language', 'es-ES')
subes.add_header('Content-Disposition', 'inline')
msg.add_attachment(suben)
msg.add_attachment(subes)
msg.replace_header('Content-type', 'multipart/multilingual')
This is by and large based on the examples from https://docs.python.org/3/library/email.examples.html with some adaptations for this rather unusual multipart type.
Demo: https://ideone.com/T0AonQ reveals that the content from the first set_content
is removed; if you want to support that, adding it in a different fashion (as another attachment, I guess) should be straightforward.
Like the demo shows, you can examine the generated message's source with msg.as_string()
, and eventually send it with smtplib.send_message(msg)
.
I have no idea how well - if at all - this construct is supported by email clients in practice.
This uses the overhauled EmailMessage
class from Python 3.6. If you are on an older version, this interface was introduced - but undocumented - in 3.3; older versions still will have to use the legacy email.message.Message()
class, but really, you will want to upgrade.