Mercurial > libervia-backend
annotate sat/tools/common/email.py @ 3352:a3f940e3d72e
install (setup.py): added missing `extra_requires`
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 13 Sep 2020 22:49:10 +0200 |
parents | 559a625a236b |
children | 1c7f4ce6a4a2 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2181
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 |
3136 | 5 # Copyright (C) 2009-2020 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 |
3028 | 22 |
2953
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
23 from email.mime.text import MIMEText |
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
24 from twisted.mail import smtp |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from sat.core.constants import Const as C |
2953
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
26 from sat.tools import config as tools_config |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
3028 | 29 def sendEmail(config, to_emails, subject="", body="", from_email=None): |
2953
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
30 """Send an email using SàT configuration |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
2953
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
32 @param config (SafeConfigParser): the configuration instance |
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
|
33 @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
|
34 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
|
35 @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
|
36 @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
|
37 @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
|
38 @return (D): same as smtp.sendmail |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 """ |
3028 | 40 if isinstance(to_emails, str): |
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 to_emails = to_emails.split() |
3028 | 42 email_host = tools_config.getConfig(config, None, "email_server") or "localhost" |
43 email_from = tools_config.getConfig(config, None, "email_from") | |
2181
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 if email_from is None: |
968b0d13bcc7
plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 # we suppose that email domain and XMPP domain are identical |
3028 | 46 domain = tools_config.getConfig(config, None, "xmpp_domain", "example.net") |
47 email_from = "no_reply@" + domain | |
48 email_sender_domain = tools_config.getConfig(config, None, "email_sender_domain") | |
49 email_port = int(tools_config.getConfig(config, None, "email_port", 25)) | |
50 email_username = tools_config.getConfig(config, None, "email_username") | |
51 email_password = tools_config.getConfig(config, None, "email_password") | |
2953
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
52 email_auth = C.bool(tools_config.getConfig(config, None, "email_auth", C.BOOL_FALSE)) |
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
53 email_starttls = C.bool(tools_config.getConfig(config, None, "email_starttls", |
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
54 C.BOOL_FALSE)) |
2624
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") |
3028 | 57 msg["Subject"] = subject |
58 msg["From"] = email_from | |
59 msg["To"] = ", ".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], |
3034
ca6c695c65da
tools (common/email): fixed email sending after python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
65 msg.as_bytes(), |
2953
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
66 senderDomainName=email_sender_domain.encode("utf-8") if email_sender_domain |
3caf460bc125
tools (email): moved email code to common so it can be used by frontends
Goffi <goffi@goffi.org>
parents:
2951
diff
changeset
|
67 else None, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 port=email_port, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 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
|
70 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
|
71 requireAuthentication=email_auth, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 requireTransportSecurity=email_starttls, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 ) |