Python - 使用mailto打开包含多个收件人的默认邮件客户端 [英] Python - Open default mail client using mailto, with multiple recipients

查看:454
本文介绍了Python - 使用mailto打开包含多个收件人的默认邮件客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用默认安装的邮件客户端编写一个Python函数来发送电子邮件到用户列表。我想打开电子邮件客户端,并给用户编辑用户列表或电子邮件正文。



我做了一些搜索,根据这里:



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



显然,RFC规范将多个以逗号分隔的收件人放在mailto链接中。然而,这是所有人似乎都在这样做的方式。无论如何,我发现以下两个网站:





似乎建议使用urllib.parse(url.parse.quote为我)和webbrowser.open



我尝试了第一个链接(2ality.blogspot.com)中的示例代码,并且工作正常,并打开了我的默认邮件客户端。但是,当我尝试使用我自己的模块中的代码时,似乎打开了我的默认浏览器,有些奇怪的原因。地址栏中没有有趣的文字,它只是打开浏览器。



email_incorrect_phone_numbers()函数在Employees类中,其中包含Employee的字典(employee_dict)对象,其本身具有多个员工属性(sn,givenName,邮件等)。完整代码实际上在这里( Python - 将CSV转换为对象 - 代码设计

  from urllib.parse import quote 
import webbrowser

....

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

任何建议?



干杯,
维克多

解决方案

嗯,既然你提出了建议:忘记了 mailto: scheme和 webbrowser ,并使用Python的 smtplib 模块编写一个小型SMTP客户端。这是标准的,完全支持所有系统,并且文档中包含一个示例,您可以实际地将其复制和粘贴。



当然,如果您正在使用 smtplib ,您将不得不向用户询问要使用的SMTP服务器的详细信息(主机名和端口,可能是登录名/密码)。这是不可信的,所以我可以看到你为什么要委托给系统上的现有程序来处理电子邮件。问题是,没有系统独立的方式来做到这一点。即使 webbrowser 模块也无法运作;有些人使用模块无法检测到默认(或任何)浏览器的系统,即使可以,当您提供 mailto:链接时会发生什么完全取决于浏览器。



如果您不想或不能使用SMTP,最好的方法是写一个自定义模块,检测并打开尽可能多的不同系统的默认电子邮件客户端 - 基本上是 webbrowser 模块所做的,除了电子邮件客户端而不是浏览器。在这种情况下,您需要确定用户已安装哪些邮件客户端,并确保您支持它们。如果您足够彻底,您可能会发布您的模块在PyPI(Python包索引),甚至可以将其包含在Python标准库的未来版本中 - 我相信有很多人会喜欢那个。


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

解决方案

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.

这篇关于Python - 使用mailto打开包含多个收件人的默认邮件客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆