From a5b869e5f0a40965602cb0f9cb6359438183ece4 Mon Sep 17 00:00:00 2001 From: "Andrew M." Date: Thu, 19 Sep 2024 12:31:14 +0300 Subject: [PATCH] Issue #69: Fixed SenderError in register example A BeskarError is being raised as a result of SenderError in the async_sender library when running the register example; this is because the Message being sent does not have the to, cc or bcc attributes as consequence of being reinitialised. This was resolved by directly adding the Message arguements to the `self.app.ctx.mail.send_message()` method. Also: * Changed the `reply_to` value from a list to a str * Changed the response on the `/register` endpoint to JSONResponse --- example/register.py | 5 +++-- sanic_beskar/base.py | 9 +++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/example/register.py b/example/register.py index 56c677b..88c8281 100644 --- a/example/register.py +++ b/example/register.py @@ -4,6 +4,7 @@ import sanic_beskar from async_sender import Mail # type: ignore from sanic import Sanic, json +from sanic.response.types import JSONResponse from sanic_beskar import Beskar from tortoise import fields from tortoise.contrib.sanic import register_tortoise @@ -243,9 +244,9 @@ async def register(request): roles="operator", ) - await _guard.send_registration_email(email, user=new_user) + await _guard.send_registration_email(email, user=new_user, confirmation_sender="you@whatever.com") ret = {"message": f"successfully sent registration email to user {new_user.username}"} - return (json(ret), 201) + return JSONResponse(ret, 201) @sanic_app.route("/finalize") async def finalize(*args): diff --git a/sanic_beskar/base.py b/sanic_beskar/base.py index 423f92a..6e13c0b 100644 --- a/sanic_beskar/base.py +++ b/sanic_beskar/base.py @@ -1798,18 +1798,15 @@ async def send_token_email( jinja_tmpl = jinja2.Template(template, autoescape=True, enable_async=True) notification["message"] = (await jinja_tmpl.render_async(notification)).strip() - _mail = import_module(self.app.ctx.mail.__module__) - msg = _mail.Message( + logger.debug(f"Sending email to {email}") + notification["result"] = await self.app.ctx.mail.send_message( subject=notification["subject"], to=[notification["email"]], from_address=action_sender, html=notification["message"], - reply_to=[action_sender], + reply_to=action_sender ) - logger.debug(f"Sending email to {email}") - notification["result"] = await self.app.ctx.mail.send_message(msg) - return notification async def get_user_from_registration_token(self, token: str) -> Any: