comparison src/tools/email.py @ 2181:968b0d13bcc7

plugin account, tools: some cleaning account + email and password tools: - plugin misc account has been reordered and cleaned a bit. - new xmpp_domain setting is used to get the domain name of main XMPP server - password generation method has been moved to tools.utils - email sending method has been moved to tools.email
author Goffi <goffi@goffi.org>
date Sun, 12 Mar 2017 19:32:59 +0100
parents
children 666ad9de044c
comparison
equal deleted inserted replaced
2180:be54e1c3394c 2181:968b0d13bcc7
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SàT: a jabber client
5 # Copyright (C) 2009-2016 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 sat.core.constants import Const as C
24 from sat.core.log import getLogger
25 log = getLogger(__name__)
26 from twisted.mail import smtp
27 from email.mime.text import MIMEText
28
29
30
31 def sendEmail(host, to_emails, subject=u'', body=u'', from_email=None):
32 """send an email using SàT configuration
33
34 @param to_emails(list[unicode]): list of recipients
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 email_host = host.memory.getConfig(None, u'email_server') or u'localhost'
41 email_from = host.memory.getConfig(None, u'email_from')
42 if email_from is None:
43 # we suppose that email domain and XMPP domain are identical
44 domain = host.memory.getConfig(None, u'xmpp_domain', u'example.net')
45 email_from = u'no_reply@' + domain
46 email_sender_domain = host.memory.getConfig(None, u'email_sender_domain')
47 email_port = int(host.memory.getConfig(None, u'email_port', 25))
48 email_username = host.memory.getConfig(None, u'email_username')
49 email_password = host.memory.getConfig(None, u'email_password')
50 email_auth = C.bool(host.memory.getConfig(None, 'email_auth', False))
51 email_starttls = C.bool(host.memory.getConfig(None, 'email_starttls', False))
52
53 msg = MIMEText(body, 'plain', 'UTF-8')
54 msg[u'Subject'] = subject
55 msg[u'From'] = email_from
56 msg[u'To'] = u", ".join(to_emails)
57
58 return smtp.sendmail(email_host.encode("utf-8"),
59 email_from.encode("utf-8"),
60 [email.encode("utf-8") for email in to_emails],
61 msg.as_string(),
62 senderDomainName = email_sender_domain.encode("utf-8") if email_sender_domain else None,
63 port = email_port,
64 username = email_username.encode("utf-8") if email_username else None,
65 password = email_password.encode("utf-8") if email_password else None,
66 requireAuthentication = email_auth,
67 requireTransportSecurity = email_starttls)