This repository was archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Split multiplart email sending into a dedicated handler #9977
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2147f06
Split multiplart email sending into a dedicated handler
babolivier 1df0c8c
Changelog
babolivier a26185a
Lint
babolivier efc82b7
Fix typo in changelog file
babolivier 1df5d74
Incorporate review
babolivier 0d7cb16
Typo
babolivier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Split multipart email sending into a dedicated handler. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2021 The Matrix.org C.I.C. Foundation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import email.utils | ||
import logging | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from typing import TYPE_CHECKING | ||
|
||
from synapse.logging.context import make_deferred_yieldable | ||
|
||
if TYPE_CHECKING: | ||
from synapse.server import HomeServer | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SendEmailHandler: | ||
def __init__(self, hs: "HomeServer"): | ||
self.hs = hs | ||
|
||
self._sendmail = hs.get_sendmail() | ||
self._reactor = hs.get_reactor() | ||
|
||
self._from = hs.config.email.email_notif_from | ||
self._smtp_host = hs.config.email.email_smtp_host | ||
self._smtp_port = hs.config.email.email_smtp_port | ||
self._smtp_user = hs.config.email.email_smtp_user | ||
self._smtp_pass = hs.config.email.email_smtp_pass | ||
self._require_transport_security = hs.config.email.require_transport_security | ||
|
||
async def send_email( | ||
self, | ||
email_address: str, | ||
subject: str, | ||
app_name: str, | ||
html: str, | ||
text: str, | ||
) -> None: | ||
"""Send a multipart email with the given information. | ||
|
||
Args: | ||
email_address: The address to send the email to. | ||
subject: The email's subject. | ||
app_name: The app name to include in the From header. | ||
html: The HTML content to include in the email. | ||
text: The plain text content to include in the email. | ||
""" | ||
try: | ||
from_string = self._from % {"app": app_name} | ||
except (KeyError, TypeError): | ||
from_string = self._from | ||
|
||
raw_from = email.utils.parseaddr(from_string)[1] | ||
raw_to = email.utils.parseaddr(email_address)[1] | ||
|
||
if raw_to == "": | ||
raise RuntimeError("Invalid 'to' address") | ||
|
||
html_part = MIMEText(html, "html", "utf8") | ||
text_part = MIMEText(text, "plain", "utf8") | ||
|
||
multipart_msg = MIMEMultipart("alternative") | ||
multipart_msg["Subject"] = subject | ||
multipart_msg["From"] = from_string | ||
multipart_msg["To"] = email_address | ||
multipart_msg["Date"] = email.utils.formatdate() | ||
multipart_msg["Message-ID"] = email.utils.make_msgid() | ||
multipart_msg.attach(text_part) | ||
multipart_msg.attach(html_part) | ||
|
||
logger.info("Sending email to %s" % email_address) | ||
|
||
await make_deferred_yieldable( | ||
self._sendmail( | ||
self._smtp_host, | ||
raw_from, | ||
raw_to, | ||
multipart_msg.as_string().encode("utf8"), | ||
reactor=self._reactor, | ||
port=self._smtp_port, | ||
requireAuthentication=self._smtp_user is not None, | ||
username=self._smtp_user, | ||
password=self._smtp_pass, | ||
requireTransportSecurity=self._require_transport_security, | ||
) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.