comparison sat/tools/common/email.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 3caf460bc125
children ca6c695c65da
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SàT: a jabber client 4 # SàT: a jabber client
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """email sending facilities""" 20 """email sending facilities"""
21 21
22 from __future__ import absolute_import 22
23 from email.mime.text import MIMEText 23 from email.mime.text import MIMEText
24 from twisted.mail import smtp 24 from twisted.mail import smtp
25 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
26 from sat.tools import config as tools_config 26 from sat.tools import config as tools_config
27 27
28 28
29 def sendEmail(config, to_emails, subject=u"", body=u"", from_email=None): 29 def sendEmail(config, to_emails, subject="", body="", from_email=None):
30 """Send an email using SàT configuration 30 """Send an email using SàT configuration
31 31
32 @param config (SafeConfigParser): the configuration instance 32 @param config (SafeConfigParser): the configuration instance
33 @param to_emails(list[unicode], unicode): list of recipients 33 @param to_emails(list[unicode], unicode): list of recipients
34 if unicode, it will be split to get emails 34 if unicode, it will be split to get emails
35 @param subject(unicode): subject of the message 35 @param subject(unicode): subject of the message
36 @param body(unicode): body of the message 36 @param body(unicode): body of the message
37 @param from_email(unicode): address of the sender 37 @param from_email(unicode): address of the sender
38 @return (D): same as smtp.sendmail 38 @return (D): same as smtp.sendmail
39 """ 39 """
40 if isinstance(to_emails, basestring): 40 if isinstance(to_emails, str):
41 to_emails = to_emails.split() 41 to_emails = to_emails.split()
42 email_host = tools_config.getConfig(config, None, u"email_server") or u"localhost" 42 email_host = tools_config.getConfig(config, None, "email_server") or "localhost"
43 email_from = tools_config.getConfig(config, None, u"email_from") 43 email_from = tools_config.getConfig(config, None, "email_from")
44 if email_from is None: 44 if email_from is None:
45 # we suppose that email domain and XMPP domain are identical 45 # we suppose that email domain and XMPP domain are identical
46 domain = tools_config.getConfig(config, None, u"xmpp_domain", u"example.net") 46 domain = tools_config.getConfig(config, None, "xmpp_domain", "example.net")
47 email_from = u"no_reply@" + domain 47 email_from = "no_reply@" + domain
48 email_sender_domain = tools_config.getConfig(config, None, u"email_sender_domain") 48 email_sender_domain = tools_config.getConfig(config, None, "email_sender_domain")
49 email_port = int(tools_config.getConfig(config, None, u"email_port", 25)) 49 email_port = int(tools_config.getConfig(config, None, "email_port", 25))
50 email_username = tools_config.getConfig(config, None, u"email_username") 50 email_username = tools_config.getConfig(config, None, "email_username")
51 email_password = tools_config.getConfig(config, None, u"email_password") 51 email_password = tools_config.getConfig(config, None, "email_password")
52 email_auth = C.bool(tools_config.getConfig(config, None, "email_auth", C.BOOL_FALSE)) 52 email_auth = C.bool(tools_config.getConfig(config, None, "email_auth", C.BOOL_FALSE))
53 email_starttls = C.bool(tools_config.getConfig(config, None, "email_starttls", 53 email_starttls = C.bool(tools_config.getConfig(config, None, "email_starttls",
54 C.BOOL_FALSE)) 54 C.BOOL_FALSE))
55 55
56 msg = MIMEText(body, "plain", "UTF-8") 56 msg = MIMEText(body, "plain", "UTF-8")
57 msg[u"Subject"] = subject 57 msg["Subject"] = subject
58 msg[u"From"] = email_from 58 msg["From"] = email_from
59 msg[u"To"] = u", ".join(to_emails) 59 msg["To"] = ", ".join(to_emails)
60 60
61 return smtp.sendmail( 61 return smtp.sendmail(
62 email_host.encode("utf-8"), 62 email_host.encode("utf-8"),
63 email_from.encode("utf-8"), 63 email_from.encode("utf-8"),
64 [email.encode("utf-8") for email in to_emails], 64 [email.encode("utf-8") for email in to_emails],