在Mac OS上使用html打开默认的Mail应用程序

时间:2022-08-11 02:39:42

I'm trying to start the default mail app on Mac OS (and the other OS, but I have problems only with Mac), with an email already written. The body of the email contains HTML.

我正在尝试在Mac OS上启动默认邮件应用程序(和其他操作系统,但我只在Mac上遇到问题),已经写了一封电子邮件。电子邮件正文包含HTML。

body = "<span style='font-weight:bold'>Title : </span>{}</br> \
        <span style='font-weight:bold'>Journal : </span>{}</br></br> \
        <span style='font-weight:bold'>Abstract : </span></br></br>{}"

body = body.format(title, journal, abstract)
url = "mailto:?subject={}&body={}"
url = url.format(title, body)

if sys.platform=='win32':
    os.startfile(url)

elif sys.platform=='darwin':
    subprocess.Popen(['open', url])

else:
    # Create an url to be opened with a mail client
    try:
        subprocess.Popen(['xdg-email', url])
    except OSError:
        self.l.error("shareByEmail: OSError")

On windows and linux, this code simply opens the default mail app, and fills all the fields. The email is ready to be sent, and the html tags are not visible (the tags are correctly translated into formatted text).

在Windows和Linux上,此代码只打开默认邮件应用程序,并填充所有字段。电子邮件已准备好发送,html标签不可见(标签被正确翻译成格式化文本)。

But on Mac OS, the body of the email is a simple text, with the html tags completely visible (text not formatted).

但在Mac OS上,电子邮件的正文是一个简单的文本,html标签完全可见(文本未格式化)。

Is it a settings problem of the mail client ? Is it supposed to happen ? Can we write HTML in the body of an email on Mac OS ?

这是邮件客户端的设置问题吗?这应该发生吗?我们可以在Mac OS的电子邮件正文中编写HTML吗?

I have to mention a few things:

我必须提到一些事情:

  • I'm not testing this code on my computer (I don't own a Mac), so I didn't set the mail app myself

    我没有在我的电脑上测试这个代码(我没有Mac),所以我没有自己设置邮件应用程序

  • The Mac OS version I'm testing the code on is Yosemite, the mail App is the default one (not thunderbird)

    我正在测试代码的Mac OS版本是Yosemite,邮件App是默认的(不是雷鸟)

  • I can't use the webbrowser module

    我无法使用webbrowser模块

1 个解决方案

#1


What you're trying to do shouldn't work anywhere.

你想做什么不应该在任何地方工作。

The mailto: URL scheme is defined in RFC 6068, which says:

mailto:URL方案在RFC 6068中定义,其中说:

The special "body" indicates that the associated is the body of the message. The "body" field value is intended to contain the content for the first text/plain body part of the message. The "body" pseudo header field is primarily intended for the generation of short text messages for automatic processing (such as "subscribe" messages for mailing lists), not for general MIME bodies. Except for the encoding of characters based on UTF-8 and percent-encoding, no additional encoding (such as e.g., base64 or quoted-printable; see [RFC2045]) is used for the "body" field value. As a consequence, header fields related to message encoding (e.g., Content-Transfer-Encoding) in a 'mailto' URI are irrelevant and MUST be ignored. The "body" pseudo header field name has been registered with IANA for this special purpose (see Section 8.2).

特殊的“正文”表示关联的是消息的正文。 “body”字段值旨在包含消息的第一个text / plain正文部分的内容。 “body”伪头字段主要用于生成用于自动处理的短文本消息(例如邮件列表的“订阅”消息),而不是用于一般MIME主体。除了基于UTF-8和百分比编码的字符编码之外,没有其他编码(例如,base64或quoted-printable;参见[RFC2045])用于“body”字段值。结果,与'mailto'URI中的消息编码(例如,内容传输编码)相关的头字段是无关紧要的并且必须被忽略。出于此特殊目的,已在IANA注册了“body”伪标头字段名称(请参阅第8.2节)。

The fact that it happens to work on whatever default mail programs you have set up on Windows and Linux means those mail programs are doing something non-standard, and you shouldn't be relying on that.

事实上它恰好适用于您在Windows和Linux上设置的任何默认邮件程序,这意味着这些邮件程序正在做一些非标准的事情,您不应该依赖它。

Also, passing the entire body as a command-line argument (as you're doing on all three platforms) can run into the limit on command-line length, and many mailers will truncate a too-long mailto even if it gets to them untouched.

此外,将整个主体作为命令行参数传递(正如您在所有三个平台上所做的那样)可能会遇到命令行长度的限制,并且许多邮件程序将截断一个太长的邮件,即使它到达它们不变。

Meanwhile:

Can we write HTML in the body of an email on Mac OS ?

我们可以在Mac OS的电子邮件正文中编写HTML吗?

Yes, but not with a mailto: URL. For example, you can use AppleEvents to script any mailer that handles the "standard mail suite" (which of course includes Mail.app). See SBSendEmail for some sample code (in ObjC, but using ScriptingBridge, which you can similarly access from Python) that does it that way.

是的,但没有mailto:URL。例如,您可以使用AppleEvents为任何处理“标准邮件套件”的邮件程序(当然包括Mail.app)编写脚本。请参阅SBSendEmail以获取一些示例代码(在ObjC中,但使用ScriptingBridge,您可以类似地从Python访问)以这种方式执行。

#1


What you're trying to do shouldn't work anywhere.

你想做什么不应该在任何地方工作。

The mailto: URL scheme is defined in RFC 6068, which says:

mailto:URL方案在RFC 6068中定义,其中说:

The special "body" indicates that the associated is the body of the message. The "body" field value is intended to contain the content for the first text/plain body part of the message. The "body" pseudo header field is primarily intended for the generation of short text messages for automatic processing (such as "subscribe" messages for mailing lists), not for general MIME bodies. Except for the encoding of characters based on UTF-8 and percent-encoding, no additional encoding (such as e.g., base64 or quoted-printable; see [RFC2045]) is used for the "body" field value. As a consequence, header fields related to message encoding (e.g., Content-Transfer-Encoding) in a 'mailto' URI are irrelevant and MUST be ignored. The "body" pseudo header field name has been registered with IANA for this special purpose (see Section 8.2).

特殊的“正文”表示关联的是消息的正文。 “body”字段值旨在包含消息的第一个text / plain正文部分的内容。 “body”伪头字段主要用于生成用于自动处理的短文本消息(例如邮件列表的“订阅”消息),而不是用于一般MIME主体。除了基于UTF-8和百分比编码的字符编码之外,没有其他编码(例如,base64或quoted-printable;参见[RFC2045])用于“body”字段值。结果,与'mailto'URI中的消息编码(例如,内容传输编码)相关的头字段是无关紧要的并且必须被忽略。出于此特殊目的,已在IANA注册了“body”伪标头字段名称(请参阅第8.2节)。

The fact that it happens to work on whatever default mail programs you have set up on Windows and Linux means those mail programs are doing something non-standard, and you shouldn't be relying on that.

事实上它恰好适用于您在Windows和Linux上设置的任何默认邮件程序,这意味着这些邮件程序正在做一些非标准的事情,您不应该依赖它。

Also, passing the entire body as a command-line argument (as you're doing on all three platforms) can run into the limit on command-line length, and many mailers will truncate a too-long mailto even if it gets to them untouched.

此外,将整个主体作为命令行参数传递(正如您在所有三个平台上所做的那样)可能会遇到命令行长度的限制,并且许多邮件程序将截断一个太长的邮件,即使它到达它们不变。

Meanwhile:

Can we write HTML in the body of an email on Mac OS ?

我们可以在Mac OS的电子邮件正文中编写HTML吗?

Yes, but not with a mailto: URL. For example, you can use AppleEvents to script any mailer that handles the "standard mail suite" (which of course includes Mail.app). See SBSendEmail for some sample code (in ObjC, but using ScriptingBridge, which you can similarly access from Python) that does it that way.

是的,但没有mailto:URL。例如,您可以使用AppleEvents为任何处理“标准邮件套件”的邮件程序(当然包括Mail.app)编写脚本。请参阅SBSendEmail以获取一些示例代码(在ObjC中,但使用ScriptingBridge,您可以类似地从Python访问)以这种方式执行。