简体   繁体   English

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

[英]Open the default Mail app with html on Mac OS

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. 我正在尝试在Mac OS(和其他OS,但我仅在Mac上有问题)上启动默认的邮件应用,并且已写了一封电子邮件。 The body of the email contains HTML. 电子邮件的正文包含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. 在Windows和Linux上,此代码仅打开默认邮件应用程序,并填写所有字段。 The email is ready to be sent, and the html tags are not visible (the tags are correctly translated into formatted text). 电子邮件已准备好发送,并且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 我无法使用网络浏览器模块

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. “正文”伪头字段主要用于生成用于自动处理的短消息(例如,用于邮件列表的“订阅”消息),而不是用于一般的MIME正文。 Except for the encoding of characters based on UTF-8 and percent-encoding, no additional encoding (such as eg, base64 or quoted-printable; see [RFC2045]) is used for the "body" field value. 除了基于UTF-8的字符编码和百分比编码外,“正文”字段值不使用任何其他编码(例如base64或quoted-printable;请参见[RFC2045])。 As a consequence, header fields related to message encoding (eg, Content-Transfer-Encoding) in a 'mailto' URI are irrelevant and MUST be ignored. 结果,与“ mailto” URI中的消息编码(例如Content-Transfer-Encoding)有关的头字段是不相关的,必须忽略。 The "body" pseudo header field name has been registered with IANA for this special purpose (see Section 8.2). 为此特殊目的,已向IANA注册了“正文”伪标题字段名称(请参见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. 另外,将整个主体作为命令行参数传递(就像您在所有三个平台上所做的一样)可能会遇到命令行长度的限制,并且许多邮件程序都会截断过长的mailto即使它到达了它们也是如此。无动于衷。

Meanwhile: 与此同时:

Can we write HTML in the body of an email on Mac OS ? 我们可以在Mac OS的电子邮件正文中编写HTML吗?

Yes, but not with a mailto: URL. 是的,但没有mailto: URL。 For example, you can use AppleEvents to script any mailer that handles the "standard mail suite" (which of course includes Mail.app). 例如,您可以使用AppleEvents编写脚本来处理任何处理“标准邮件套件”(当然包括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. 请参阅SBSendEmail,获取达到此目的的一些示例代码(在ObjC中,但使用ScriptingBridge,您可以类似地从Python访问)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM