# Sending mails

Misago provides its own API for sending e-mails to forum users thats layer of abstraction above the default [Django mailer](https://docs.djangoproject.com/en/dev/topics/email/).

This API lives in `misago.core.mail` and provides following functions:

## `build_mail`(recipient, subject, template, sender=None, context=None)

Build e-mail message using supplied template name and (optionally) context. Template name shouldn't contain file extension, as Misago will automatically append `.html` for html content and `.txt` for plaintext content of the message.

* `subject` - A string.
* `template` - A string.
* `sender` - User model instance for message sender.
* `context` - The dictionary with context values that should be used in template render.

Email templates are always passed following context values:

* `SITE_ADDRESS` - The site address set in `MISAGO_ADDRESS` setting.
* `SITE_HOST` - The site host extracted from the `MISAGO_ADDRESS` (eg. `mycommunity.com` for `https://mycommunity.com/`).
* `LANGUAGE_CODE` - Valid language code that can be used as value for `<html>` `lang` attribute.
* `LOGIN_URL` - The URL to login view specified in the `LOGIN_URL` setting.
* `user` - User model instance representing the message recipient.
* `sender` - If specified, instance of User model representing the message senderm eg. user that performed the action that triggered e-mail to be sent.
* `subject` - Subject of message being sent.
* `misago_settings` - Dict-like object enabling access to misago's running configuration.

## mail\_user(recipient, subject, template, sender=None, context=None)

Shortcut function that calls `build_mail` to build message, then sends it to user.

* `request` - HttpRequest object instance.
* `recipient` - User model instance.
* `subject` - A string.
* `template` - A string.
* `sender` - User model instance for message sender.
* `context` - The dictionary with context values that should be available for message templates.

## mail\_users(recipients, subject, template, sender=None, context=None)

Same as `mail_user`, but instead of sending message to single recipient, it sends it to many recipients at same time. Keep on mind this may be memory intensitive as this function creates one Mail object instance for every recipient specified, so you may want to batch the sending process.

* `recipients` - Iterable of User models.
* `subject` - A string.
* `template` - A string.
* `sender` - User model instance for message sender.
* `context` - The dictionary with context values that should be available for message templates.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://misago.gitbook.io/docs/mails.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
