comparison sat/tools/email.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
20 """email sending facilities""" 20 """email sending facilities"""
21 21
22 from __future__ import absolute_import 22 from __future__ import absolute_import
23 from sat.core.constants import Const as C 23 from sat.core.constants import Const as C
24 from sat.core.log import getLogger 24 from sat.core.log import getLogger
25
25 log = getLogger(__name__) 26 log = getLogger(__name__)
26 from twisted.mail import smtp 27 from twisted.mail import smtp
27 from email.mime.text import MIMEText 28 from email.mime.text import MIMEText
28 29
29 30
30 31 def sendEmail(host, to_emails, subject=u"", body=u"", from_email=None):
31 def sendEmail(host, to_emails, subject=u'', body=u'', from_email=None):
32 """send an email using SàT configuration 32 """send an email using SàT configuration
33 33
34 @param to_emails(list[unicode], unicode): list of recipients 34 @param to_emails(list[unicode], unicode): list of recipients
35 if unicode, it will be split to get emails 35 if unicode, it will be split to get emails
36 @param subject(unicode): subject of the message 36 @param subject(unicode): subject of the message
38 @param from_email(unicode): address of the sender 38 @param from_email(unicode): address of the sender
39 @return (D): same as smtp.sendmail 39 @return (D): same as smtp.sendmail
40 """ 40 """
41 if isinstance(to_emails, basestring): 41 if isinstance(to_emails, basestring):
42 to_emails = to_emails.split() 42 to_emails = to_emails.split()
43 email_host = host.memory.getConfig(None, u'email_server') or u'localhost' 43 email_host = host.memory.getConfig(None, u"email_server") or u"localhost"
44 email_from = host.memory.getConfig(None, u'email_from') 44 email_from = host.memory.getConfig(None, u"email_from")
45 if email_from is None: 45 if email_from is None:
46 # we suppose that email domain and XMPP domain are identical 46 # we suppose that email domain and XMPP domain are identical
47 domain = host.memory.getConfig(None, u'xmpp_domain', u'example.net') 47 domain = host.memory.getConfig(None, u"xmpp_domain", u"example.net")
48 email_from = u'no_reply@' + domain 48 email_from = u"no_reply@" + domain
49 email_sender_domain = host.memory.getConfig(None, u'email_sender_domain') 49 email_sender_domain = host.memory.getConfig(None, u"email_sender_domain")
50 email_port = int(host.memory.getConfig(None, u'email_port', 25)) 50 email_port = int(host.memory.getConfig(None, u"email_port", 25))
51 email_username = host.memory.getConfig(None, u'email_username') 51 email_username = host.memory.getConfig(None, u"email_username")
52 email_password = host.memory.getConfig(None, u'email_password') 52 email_password = host.memory.getConfig(None, u"email_password")
53 email_auth = C.bool(host.memory.getConfig(None, 'email_auth', False)) 53 email_auth = C.bool(host.memory.getConfig(None, "email_auth", False))
54 email_starttls = C.bool(host.memory.getConfig(None, 'email_starttls', False)) 54 email_starttls = C.bool(host.memory.getConfig(None, "email_starttls", False))
55 55
56 msg = MIMEText(body, 'plain', 'UTF-8') 56 msg = MIMEText(body, "plain", "UTF-8")
57 msg[u'Subject'] = subject 57 msg[u"Subject"] = subject
58 msg[u'From'] = email_from 58 msg[u"From"] = email_from
59 msg[u'To'] = u", ".join(to_emails) 59 msg[u"To"] = u", ".join(to_emails)
60 60
61 return smtp.sendmail(email_host.encode("utf-8"), 61 return smtp.sendmail(
62 email_from.encode("utf-8"), 62 email_host.encode("utf-8"),
63 [email.encode("utf-8") for email in to_emails], 63 email_from.encode("utf-8"),
64 msg.as_string(), 64 [email.encode("utf-8") for email in to_emails],
65 senderDomainName = email_sender_domain.encode("utf-8") if email_sender_domain else None, 65 msg.as_string(),
66 port = email_port, 66 senderDomainName=email_sender_domain.encode("utf-8")
67 username = email_username.encode("utf-8") if email_username else None, 67 if email_sender_domain
68 password = email_password.encode("utf-8") if email_password else None, 68 else None,
69 requireAuthentication = email_auth, 69 port=email_port,
70 requireTransportSecurity = email_starttls) 70 username=email_username.encode("utf-8") if email_username else None,
71 password=email_password.encode("utf-8") if email_password else None,
72 requireAuthentication=email_auth,
73 requireTransportSecurity=email_starttls,
74 )