简体   繁体   中英

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. The body of the email contains 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).

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

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 ?

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

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

  • 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:

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 eg, base64 or quoted-printable; see [RFC2045]) is used for the "body" field value. As a consequence, header fields related to message encoding (eg, 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).

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.

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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