Skip to content

Commit

Permalink
fix signal argument order (#219)
Browse files Browse the repository at this point in the history
app is signal sender, message is argument

Co-authored-by: Sergey Konyukhovskiy <sk@onthe.net.ru>
  • Loading branch information
davidism and ElvisTheKing authored May 23, 2024
1 parent 7b450bd commit 9075a7a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Unreleased
`importlib.metadata.version("flask-mail")` instead.
- Indicate that the deprecated `is_bad_headers` will be removed in the next
version.
- Fix the `email_dispatched` signal to pass the current app as the sender and
`message` as an argument, rather than the other way around.


## Version 0.9.1
Expand Down
8 changes: 4 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ Flask-Mail provides signalling support through a {data}`.email_dispatched`
signal. This is sent whenever an email is dispatched (even if the email is not
actually sent, i.e. in a testing environment).

A function connecting to the {data}`.email_dispatched` signal takes a
{class}`.Message` instance as a first argument, and the Flask app instance as an
optional argument:
A function connecting to the {data}`.email_dispatched` signal is sent with the
{class}`~flask.Flask` instance as the first argument, and the {class}`.Message}`
instance as the `message` argument.

```py
def log_message(message, app):
def log_message(app, message):
app.logger.debug(message.subject)

email_dispatched.connect(log_message)
Expand Down
15 changes: 3 additions & 12 deletions src/flask_mail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def send(self, message, envelope_from=None):
message.rcpt_options,
)

email_dispatched.send(message, app=current_app._get_current_object())
email_dispatched.send(current_app._get_current_object(), message=message)

self.num_emails += 1

Expand Down Expand Up @@ -456,24 +456,15 @@ def record_messages(self):
assert len(outbox) == 1
assert outbox[0].subject == "testing"
You must have blinker installed in order to use this feature.
:versionadded: 0.4
"""

if not email_dispatched:
raise RuntimeError("blinker must be installed")

outbox = []

def _record(message, app):
def record(app, message):
outbox.append(message)

email_dispatched.connect(_record)

try:
with email_dispatched.connected_to(record):
yield outbox
finally:
email_dispatched.disconnect(_record)

def send(self, message):
"""Sends a single message instance. If TESTING is True the message will
Expand Down

0 comments on commit 9075a7a

Please sign in to comment.