tags:

views:

39

answers:

1

Hi,

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:

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 (http://stackoverflow.com/questions/2963975/python-converting-csv-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

+1  A: 

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.

David Zaslavsky
+1, I second the motion.
Craig Trader
Hmm, yeah, it seems mailto is quite broken. The only issue is, we want the user to be able to edit the mailto list, the subject, and the body if they want to. Opening their email client with things pre-populated is a good way of doing that. Also, this is a fairly homogenous corporate environment, so we know what email client the user will have, and how their environment is setup. That, and it's a utility that's really only run by one user (at least for now).
victorhooi
And secondly, smtplib isn't really an option, since this is a enterprise environment. This is a small utility running off a user's desktop, there's no internal SMTP servers we'd have access to, and we can't punch out to external ones either because of the corporate proxy/firewall (and we shouldn't be doing that either).Is there no clean way of doing this, save for using an SMTP server?Your last option looks intriguing, just not sure how to achieve it.
victorhooi
@victorhooi: What do your users use to send email, if not SMTP? (How are the email clients configured?) As far as the last option, writing a custom module: since there's really only one email client to support, that shouldn't be too hard. I'd suggest taking a look at the source code for the `webbrowser` module as a starting point, just to see how you might structure a custom emailer module. The tricky part will probably be figuring out how to open a new message in the email client using some non-graphical command. That might be a good topic for another SO question.
David Zaslavsky
@David Zaslavsky: The user's use Lotus Notes - I don't think the SMTP servers are exposed for me to make a direct connection, it's a fairly large global company, and it all goes via Notes (and I don't have much knowledge of the underlying architecture there).
victorhooi
@victorhooi: I don't know anything about Lotus Notes but you could try searching the web to see if you can find some information about non-graphical invocation.
David Zaslavsky