Mercurial > libervia-backend
comparison libervia/backend/tools/common/email.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/tools/common/email.py@524856bd7b19 |
children | b0c36f3d3e04 |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SàT: a jabber client | |
5 # Copyright (C) 2009-2021 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 | |
23 from email.mime.text import MIMEText | |
24 from twisted.mail import smtp | |
25 from libervia.backend.core.constants import Const as C | |
26 from libervia.backend.tools import config as tools_config | |
27 from libervia.backend.core.log import getLogger | |
28 | |
29 log = getLogger(__name__) | |
30 | |
31 | |
32 def send_email(config, to_emails, subject="", body="", from_email=None): | |
33 """Send an email using SàT configuration | |
34 | |
35 @param config (SafeConfigParser): the configuration instance | |
36 @param to_emails(list[unicode], unicode): list of recipients | |
37 if unicode, it will be split to get emails | |
38 @param subject(unicode): subject of the message | |
39 @param body(unicode): body of the message | |
40 @param from_email(unicode): address of the sender | |
41 @return (D): same as smtp.sendmail | |
42 """ | |
43 if isinstance(to_emails, str): | |
44 to_emails = to_emails.split() | |
45 email_host = tools_config.config_get(config, None, "email_server") or "localhost" | |
46 email_from = from_email or tools_config.config_get(config, None, "email_from") | |
47 | |
48 # we suppose that email domain and XMPP domain are identical | |
49 domain = tools_config.config_get(config, None, "xmpp_domain") | |
50 if domain is None: | |
51 if email_from is not None: | |
52 domain = email_from.split("@", 1)[-1] | |
53 else: | |
54 domain = "example.net" | |
55 | |
56 if email_from is None: | |
57 email_from = "no_reply@" + domain | |
58 email_sender_domain = tools_config.config_get( | |
59 config, None, "email_sender_domain", domain | |
60 ) | |
61 email_port = int(tools_config.config_get(config, None, "email_port", 25)) | |
62 email_username = tools_config.config_get(config, None, "email_username") | |
63 email_password = tools_config.config_get(config, None, "email_password") | |
64 email_auth = C.bool(tools_config.config_get(config, None, "email_auth", C.BOOL_FALSE)) | |
65 email_starttls = C.bool(tools_config.config_get(config, None, "email_starttls", | |
66 C.BOOL_FALSE)) | |
67 | |
68 msg = MIMEText(body, "plain", "UTF-8") | |
69 msg["Subject"] = subject | |
70 msg["From"] = email_from | |
71 msg["To"] = ", ".join(to_emails) | |
72 | |
73 return smtp.sendmail( | |
74 email_host.encode("utf-8"), | |
75 email_from.encode("utf-8"), | |
76 [email.encode("utf-8") for email in to_emails], | |
77 msg.as_bytes(), | |
78 senderDomainName=email_sender_domain if email_sender_domain else None, | |
79 port=email_port, | |
80 username=email_username.encode("utf-8") if email_username else None, | |
81 password=email_password.encode("utf-8") if email_password else None, | |
82 requireAuthentication=email_auth, | |
83 requireTransportSecurity=email_starttls, | |
84 ) |