|
| 1 | +# simple mailing |
| 2 | + |
| 3 | +```python |
| 4 | +from fastapi_pundra.common.mailer.mail import send_mail |
| 5 | + |
| 6 | +await send_mail( |
| 7 | + subject="Hello, World!", |
| 8 | + to=["test@example.com"], |
| 9 | + template_name="welcome_email.html", |
| 10 | + context={"name": "John Doe"}, |
| 11 | +) |
| 12 | +``` |
| 13 | + |
| 14 | +# background mailing |
| 15 | + |
| 16 | +```python |
| 17 | +from fastapi_pundra.common.mailer.mail import send_mail_background |
| 18 | + |
| 19 | +await send_mail_background( |
| 20 | + background_tasks=background_tasks, |
| 21 | + subject="Hello, World!", |
| 22 | + to=["test@example.com"], |
| 23 | + template_name="welcome_email.html", |
| 24 | + context={"name": "John Doe"}, |
| 25 | +) |
| 26 | +``` |
| 27 | + |
| 28 | +## routing tips |
| 29 | + |
| 30 | +- if you want to send mail in background, you can use `background_tasks` in your route |
| 31 | + |
| 32 | +```python |
| 33 | +from fastapi import BackgroundTasks |
| 34 | + |
| 35 | +@router.post("/users/registration") |
| 36 | +@dto(UserCreateSchema) |
| 37 | +async def registration(request: Request, background_tasks: BackgroundTasks) -> JSONResponse: |
| 38 | + ... |
| 39 | +``` |
| 40 | + |
| 41 | +> note: background_tasks paramter will be after `request` parameter in your route |
| 42 | +
|
| 43 | +# queue mailing with celery |
| 44 | + |
| 45 | +```python |
| 46 | +from fastapi_pundra.common.mailer.task import send_email_queue_task |
| 47 | + |
| 48 | +send_email_queue_task.delay( |
| 49 | + subject="Hello, World!", |
| 50 | + to=["test@example.com"], |
| 51 | + template_name="welcome_email.html", |
| 52 | + context={"name": "John Doe"}, |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +# queue mailing with celery with countdown |
| 57 | + |
| 58 | +```python |
| 59 | +from fastapi_pundra.common.mailer.task import send_email_queue_task |
| 60 | + |
| 61 | +send_email_queue_task.apply_async( |
| 62 | + args=[ |
| 63 | + f"Welcome, {new_user.name or new_user.email}!", |
| 64 | + [new_user.email], |
| 65 | + template_name, |
| 66 | + context, |
| 67 | + ], |
| 68 | + countdown=120, |
| 69 | +) |
| 70 | +``` |
0 commit comments