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

Posted by victorhooi on Stack Overflow See other posts from Stack Overflow or by victorhooi
Published on 2010-06-17T06:37:37Z Indexed on 2010/06/17 6:43 UTC
Read the original article Hit count: 331

Filed under:
|

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:

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 (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

© Stack Overflow or respective owner

Related posts about python

Related posts about email