开发者

Python - Open default mail client using mailto, with multiple recipients

I'm attempting to write a Python function to send an email to a list of users, using the default installed mail client. I want to open the email client, and give the user the opportunity to edit the list of users or the email body.

I did some searching, and according to here:

http://www.sightspecific.com/~mosh/WWW_FAQ/multrec.html

It's apparently against the RFC spec to put multiple comma-delimited recipients in a mailto link. However, that's the way everybody else seems to be doing it. What exactly is the modern stance on this?

Anyhow, I found the following two sites:

  • http://2ality.blogspot.com/2009/02/generate-emails-with-mailto-urls-and.html
  • http://www.megasolutions.net/python/invoke-users-standard-mail-client-64348.aspx

which seem to suggest solutions using urllib.parse (url.parse.quote for me), and webbrowser.open.

I tried the sample code from the first link (2ality.blogspot.com), and that worked fine, and opened my default mail client. However, when I try to use the code in my own module, it seems to open up my default browser, for some weird reason. No funny text in the address bar, it just opens up the browser.

The email_incorrect_phone_numbers() function is in the Employees class, which contains a dictionary (employee_dict) of Employee objects, which themselves have a number of employee attributes (sn, givenName, mail etc.). Full code is actually here (Python - Converting C开发者_开发知识库SV to Objects - Code Design)

from urllib.parse import quote
import webbrowser

....

    def email_incorrect_phone_numbers(self):
        email_list = []
        for employee in self.employee_dict.values():
            if not PhoneNumberFormats.standard_format.search(employee.telephoneNumber):
                print(employee.telephoneNumber, employee.sn, employee.givenName, employee.mail)
                email_list.append(employee.mail)
        recipients = ', '.join(email_list)
        webbrowser.open("mailto:%s?subject=%s&body=%s" %
                    (recipients, quote("testing"), quote('testing'))
                    )

Any suggestions?

Cheers, Victor


Well, since you asked for suggestions: forget about the mailto: scheme and webbrowser, and write a small SMTP client using Python's smtplib module. It's standard, fully supported on all systems, and there's an example included in the documentation which you can practically just copy-and-paste pieces out of.

Of course, if you're using smtplib you will have to ask the user for the details of an SMTP server to use (hostname and port, and probably a login/password). That is admittedly inconvenient, so I can see why you'd want to delegate to existing programs on the system to handle the email. Problem is, there's no system-independent way to do that. Even the webbrowser module doesn't work everywhere; some people use systems on which the module isn't able to detect the default (or any) browser, and even when it can, what happens when you provide a mailto: link is entirely up to the browser.

If you don't want to or can't use SMTP, your best bet might be to write a custom module that is able to detect and open the default email client on as many different systems as possible - basically what the webbrowser module does, except for email clients instead of browsers. In that case it's up to you to identify what kinds of mail clients your users have installed and make sure you support them. If you're thorough enough, you could probably publish your module on PyPI (Python package index) and perhaps even get it included in a future version of the Python standard library - I'm sure there are plenty of people who would appreciate something like that.


As is often the case in Python, somebody's already done most of the hard work. Check out this recipe.


In the following line, there shouldn’t be a space after the comma.

recipients = ', '.join(email_list)

Furthermore, Outlook needs semicolons, not commas. Apart from that, mailto never gave me grief.

The general tip is to test mailto URLs manually in the browser first and to debug URLs by printing them out and entering them manually.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜