annotate libervia/backend/tools/common/email.py @ 4332:71c939e34ca6

XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author Syndace <me@syndace.dev>
date Sat, 13 Jul 2024 18:28:28 +0200
parents 060d695ae98e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2953
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3465
diff changeset
5 # Copyright (C) 2009-2021 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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2953
diff changeset
22
4298
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
23 from configparser import ConfigParser
2953
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 email.mime.text import MIMEText
4298
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
25 from twisted.internet import defer
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 twisted.mail import smtp
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.tools import config as tools_config
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.log import getLogger
3465
1c7f4ce6a4a2 tools (common/email): try to guess sender domain when necessary:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
30
1c7f4ce6a4a2 tools (common/email): try to guess sender domain when necessary:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
31 log = getLogger(__name__)
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
4298
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
34 def send_email(
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
35 config: ConfigParser,
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
36 to_emails: list[str] | str,
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
37 subject: str = "",
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
38 body: str = "",
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
39 from_email: str | None = None,
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
40 ) -> defer.Deferred:
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
41 """Send an email using Libervia configuration
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
4298
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
43 @param config: the configuration instance
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
44 @param to_emails: list of recipients
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
45 if unicode, it will be split to get emails
4298
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
46 @param subject: subject of the message
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
47 @param body: body of the message
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
48 @param from_email: address of the sender
060d695ae98e plugin XEP-0106, tools (common/email): type hints.
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
49 @return:Same as smtp.sendmail.
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2953
diff changeset
51 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
52 to_emails = to_emails.split()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
53 email_host = tools_config.config_get(config, None, "email_server") or "localhost"
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
54 email_from = from_email or tools_config.config_get(config, None, "email_from")
3780
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
55
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
56 # we suppose that email domain and XMPP domain are identical
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
57 domain = tools_config.config_get(config, None, "xmpp_domain")
3780
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
58 if domain is None:
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
59 if email_from is not None:
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
60 domain = email_from.split("@", 1)[-1]
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
61 else:
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
62 domain = "example.net"
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
63
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 if email_from is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2953
diff changeset
65 email_from = "no_reply@" + domain
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
66 email_sender_domain = tools_config.config_get(
3780
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
67 config, None, "email_sender_domain", domain
2c187445a3d3 tools (common/email): use `from_email` is specified, always set `email_sender_domain`
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
68 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
69 email_port = int(tools_config.config_get(config, None, "email_port", 25))
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
70 email_username = tools_config.config_get(config, None, "email_username")
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
71 email_password = tools_config.config_get(config, None, "email_password")
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3780
diff changeset
72 email_auth = C.bool(tools_config.config_get(config, None, "email_auth", C.BOOL_FALSE))
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4238
diff changeset
73 email_starttls = C.bool(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4238
diff changeset
74 tools_config.config_get(config, None, "email_starttls", C.BOOL_FALSE)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4238
diff changeset
75 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
76
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 msg = MIMEText(body, "plain", "UTF-8")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2953
diff changeset
78 msg["Subject"] = subject
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2953
diff changeset
79 msg["From"] = email_from
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2953
diff changeset
80 msg["To"] = ", ".join(to_emails)
2181
968b0d13bcc7 plugin account, tools: some cleaning account + email and password tools:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 return smtp.sendmail(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 email_host.encode("utf-8"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 email_from.encode("utf-8"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 [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
86 msg.as_bytes(),
3465
1c7f4ce6a4a2 tools (common/email): try to guess sender domain when necessary:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
87 senderDomainName=email_sender_domain if email_sender_domain else None,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 port=email_port,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 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
90 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
91 requireAuthentication=email_auth,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 requireTransportSecurity=email_starttls,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 )