comparison sat/tools/common/email.py @ 2953:3caf460bc125

tools (email): moved email code to common so it can be used by frontends
author Goffi <goffi@goffi.org>
date Sun, 26 May 2019 22:20:33 +0200
parents sat/tools/email.py@c8cd5c30fb2e
children ab2696e34d29
comparison
equal deleted inserted replaced
2952:d8857e913309 2953:3caf460bc125
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SàT: a jabber client
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20 """email sending facilities"""
21
22 from __future__ import absolute_import
23 from email.mime.text import MIMEText
24 from twisted.mail import smtp
25 from sat.core.constants import Const as C
26 from sat.tools import config as tools_config
27
28
29 def sendEmail(config, to_emails, subject=u"", body=u"", from_email=None):
30 """Send an email using SàT configuration
31
32 @param config (SafeConfigParser): the configuration instance
33 @param to_emails(list[unicode], unicode): list of recipients
34 if unicode, it will be split to get emails
35 @param subject(unicode): subject of the message
36 @param body(unicode): body of the message
37 @param from_email(unicode): address of the sender
38 @return (D): same as smtp.sendmail
39 """
40 if isinstance(to_emails, basestring):
41 to_emails = to_emails.split()
42 email_host = tools_config.getConfig(config, None, u"email_server") or u"localhost"
43 email_from = tools_config.getConfig(config, None, u"email_from")
44 if email_from is None:
45 # we suppose that email domain and XMPP domain are identical
46 domain = tools_config.getConfig(config, None, u"xmpp_domain", u"example.net")
47 email_from = u"no_reply@" + domain
48 email_sender_domain = tools_config.getConfig(config, None, u"email_sender_domain")
49 email_port = int(tools_config.getConfig(config, None, u"email_port", 25))
50 email_username = tools_config.getConfig(config, None, u"email_username")
51 email_password = tools_config.getConfig(config, None, u"email_password")
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",
54 C.BOOL_FALSE))
55
56 msg = MIMEText(body, "plain", "UTF-8")
57 msg[u"Subject"] = subject
58 msg[u"From"] = email_from
59 msg[u"To"] = u", ".join(to_emails)
60
61 return smtp.sendmail(
62 email_host.encode("utf-8"),
63 email_from.encode("utf-8"),
64 [email.encode("utf-8") for email in to_emails],
65 msg.as_string(),
66 senderDomainName=email_sender_domain.encode("utf-8") if email_sender_domain
67 else None,
68 port=email_port,
69 username=email_username.encode("utf-8") if email_username else None,
70 password=email_password.encode("utf-8") if email_password else None,
71 requireAuthentication=email_auth,
72 requireTransportSecurity=email_starttls,
73 )