【问题标题】:Sending email with multiple language versions recognized on the basis of the language in the receiver's email client发送具有根据接收者电子邮件客户端中的语言识别的多种语言版本的电子邮件
【发布时间】:2021-01-14 19:40:23
【问题描述】:

在主题中,我正在尝试实现发送电子邮件,该电子邮件具有根据接收者浏览器中的语言识别的多种语言版本。

例如,我想用英语和西班牙语发送相同的电子邮件,但收件人只会看到他首选(通过电子邮件提供商设置)语言版本的一个选项。

我正在尝试在https://tools.ietf.org/id/draft-ietf-slim-multilangcontent-14.html#Examples 的基础上构建它,但我不确定它是否可能,因为我没有找到此示例的任何有效实现。 我正在使用 Python 和 Pyramid Mailer。

我无法在一封电子邮件中正确设置 2 种不同的内容语言。

如果您知道是否可以发送此类电子邮件,请帮助我。

【问题讨论】:

  • 您认为收件人为什么会使用浏览器来阅读他们的电子邮件?
  • @tripleee 我的意思是电子邮件提供商,例如 gmail 或 Thunderbird,很抱歉误导了您。我会改变的。

标签: python email multilingual content-type mailer


【解决方案1】:

就像您链接到的示例一样,您必须有多个 MIME 部分,每个部分都有一个单独的 Content-Language: 标头。

这是重新创建示例的尝试。不幸的是,如果您使用suben['Content-Language'] = 'en-GB' 添加它并使用add_header 将其移动到附件的标题中,Python 似乎会清除Content-Language: 标题,所以我不确定这是否会起作用。在我添加的Content-Disposition: 和 Python 添加的之间也存在令人讨厌的冲突。如果我使用msg.add_attachment()suben, 'rfc822', 'inline'),我会收到一条愤怒的错误消息,指出message/rfc822 部件不支持'inline'。 (也许您需要为这种类型创建一个新的内容管理器?)

from email.message import EmailMessage


msg = EmailMessage()
msg['From'] = 'Nik@example.com'
msg['To'] = 'Nathaniel@example.com'
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')

这大体上是基于 https://docs.python.org/3/library/email.examples.html 中的示例,并对这种相当不寻常的多部分类型进行了一些调整。

Demo: https://ideone.com/T0AonQ 显示第一个set_content 的内容被删除;如果你想支持它,以不同的方式添加它(我猜是另一个附件)应该很简单。

如演示所示,您可以使用msg.as_string() 检查生成消息的来源,并最终使用smtplib.send_message(msg) 发送。

我不知道电子邮件客户端在实践中对这种结构的支持程度(如果有的话)。

这使用了 Python 3.6 中经过大修的 EmailMessage 类。如果您使用的是旧版本,则在 3.3 中引入了此界面 - 但未记录在案;旧版本仍然必须使用旧的 email.message.Message() 类,但实际上,您会想要升级。

【讨论】:

  • 作为一个模糊的作弊,如果我输入 msg.add_attachment(suben, 'RFC822', 'inline') 我会得到 Content-disposition: where I want it。猜猜客户是否接受message/RFC822 作为小写message/rfc822 的同义词。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 2010-11-09
  • 2016-08-18
  • 2022-01-13
  • 1970-01-01
  • 2013-09-10
相关资源
最近更新 更多