comparison sat/tools/email.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/tools/email.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SàT: a jabber client
5 # Copyright (C) 2009-2018 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], unicode): list of recipients
35 if unicode, it will be split to get emails
36 @param subject(unicode): subject of the message
37 @param body(unicode): body of the message
38 @param from_email(unicode): address of the sender
39 @return (D): same as smtp.sendmail
40 """
41 if isinstance(to_emails, basestring):
42 to_emails = to_emails.split()
43 email_host = host.memory.getConfig(None, u'email_server') or u'localhost'
44 email_from = host.memory.getConfig(None, u'email_from')
45 if email_from is None:
46 # we suppose that email domain and XMPP domain are identical
47 domain = host.memory.getConfig(None, u'xmpp_domain', u'example.net')
48 email_from = u'no_reply@' + 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))
51 email_username = host.memory.getConfig(None, u'email_username')
52 email_password = host.memory.getConfig(None, u'email_password')
53 email_auth = C.bool(host.memory.getConfig(None, 'email_auth', False))
54 email_starttls = C.bool(host.memory.getConfig(None, 'email_starttls', 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(email_host.encode("utf-8"),
62 email_from.encode("utf-8"),
63 [email.encode("utf-8") for email in to_emails],
64 msg.as_string(),
65 senderDomainName = email_sender_domain.encode("utf-8") if email_sender_domain else None,
66 port = email_port,
67 username = email_username.encode("utf-8") if email_username else None,
68 password = email_password.encode("utf-8") if email_password else None,
69 requireAuthentication = email_auth,
70 requireTransportSecurity = email_starttls)