Skip to content

Commit

Permalink
Issue #69: Fixed SenderError in register example
Browse files Browse the repository at this point in the history
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
  • Loading branch information
debarone committed Sep 19, 2024
1 parent c2dd41b commit a5b869e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
5 changes: 3 additions & 2 deletions example/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions sanic_beskar/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a5b869e

Please sign in to comment.