annotate sat/tools/email.py @ 2869:148d30147890

plugin text syntaxes: fixed default syntax
author Goffi <goffi@goffi.org>
date Mon, 25 Mar 2019 07:06:48 +0100
parents 003b8b4b56a7
children c8cd5c30fb2e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT: a jabber client
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """email sending facilities"""
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from __future__ import absolute_import
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.constants import Const as C
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
25
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 log = getLogger(__name__)
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.mail import smtp
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from email.mime.text import MIMEText
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
31 def sendEmail(host, to_emails, subject=u"", body=u"", from_email=None):
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 """send an email using SàT configuration
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
2304
666ad9de044c core (tools/email): an unicode "to" email (instead of a list) can now be used
Goffi <goffi@goffi.org>
parents: 2181
diff changeset
34 @param to_emails(list[unicode], unicode): list of recipients
666ad9de044c core (tools/email): an unicode "to" email (instead of a list) can now be used
Goffi <goffi@goffi.org>
parents: 2181
diff changeset
35 if unicode, it will be split to get emails
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 @param subject(unicode): subject of the message
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @param body(unicode): body of the message
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 @param from_email(unicode): address of the sender
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 @return (D): same as smtp.sendmail
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 """
2304
666ad9de044c core (tools/email): an unicode "to" email (instead of a list) can now be used
Goffi <goffi@goffi.org>
parents: 2181
diff changeset
41 if isinstance(to_emails, basestring):
666ad9de044c core (tools/email): an unicode "to" email (instead of a list) can now be used
Goffi <goffi@goffi.org>
parents: 2181
diff changeset
42 to_emails = to_emails.split()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 email_host = host.memory.getConfig(None, u"email_server") or u"localhost"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 email_from = host.memory.getConfig(None, u"email_from")
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 if email_from is None:
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 # we suppose that email domain and XMPP domain are identical
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 domain = host.memory.getConfig(None, u"xmpp_domain", u"example.net")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 email_from = u"no_reply@" + domain
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 email_sender_domain = host.memory.getConfig(None, u"email_sender_domain")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 email_port = int(host.memory.getConfig(None, u"email_port", 25))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 email_username = host.memory.getConfig(None, u"email_username")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 email_password = host.memory.getConfig(None, u"email_password")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 email_auth = C.bool(host.memory.getConfig(None, "email_auth", False))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 email_starttls = C.bool(host.memory.getConfig(None, "email_starttls", False))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 msg = MIMEText(body, "plain", "UTF-8")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 msg[u"Subject"] = subject
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 msg[u"From"] = email_from
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 msg[u"To"] = u", ".join(to_emails)
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 return smtp.sendmail(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 email_host.encode("utf-8"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 email_from.encode("utf-8"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 [email.encode("utf-8") for email in to_emails],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 msg.as_string(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 senderDomainName=email_sender_domain.encode("utf-8")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 if email_sender_domain
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 else None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 port=email_port,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 username=email_username.encode("utf-8") if email_username else None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 password=email_password.encode("utf-8") if email_password else None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 requireAuthentication=email_auth,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 requireTransportSecurity=email_starttls,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 )