2017-06-11 06:54:18 +02:00
|
|
|
|
# Email
|
|
|
|
|
|
|
|
|
|
This page has developer documentation on the Zulip email system. If you're
|
|
|
|
|
trying to configure your server to send email, you might be looking for our
|
2019-09-30 19:37:56 +02:00
|
|
|
|
guide to [sending outgoing email](../production/email.md). If you're trying to
|
2024-07-04 12:33:43 +02:00
|
|
|
|
configure an email integration to receive incoming email (e.g., so that users
|
2021-04-20 23:27:25 +02:00
|
|
|
|
can reply to message notification emails via email), you might be interested in
|
2017-06-11 06:54:18 +02:00
|
|
|
|
our instructions for
|
2020-06-08 23:04:39 +02:00
|
|
|
|
[setting up an email integration](https://zulip.com/integrations/doc/email).
|
2017-06-11 06:54:18 +02:00
|
|
|
|
|
|
|
|
|
On to the documentation. Zulip's email system is fairly straightforward,
|
|
|
|
|
with only a few things you need to know to get started.
|
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- All email templates are in `templates/zerver/emails/`. Each email has three
|
2018-12-17 18:58:43 +01:00
|
|
|
|
template files: `<template_prefix>.subject.txt`, `<template_prefix>.txt`, and
|
2023-04-05 11:19:58 +02:00
|
|
|
|
`<template_prefix>.html`. Email templates, along with all other templates
|
2017-06-11 06:54:18 +02:00
|
|
|
|
in the `templates/` directory, are Jinja2 templates.
|
2023-04-05 11:19:58 +02:00
|
|
|
|
- Most of the CSS and HTML layout for emails is in `email_base_default.html`. Note
|
2017-06-11 06:54:18 +02:00
|
|
|
|
that email has to ship with all of its CSS and HTML, so nothing in
|
|
|
|
|
`static/` is useful for an email. If you're adding new CSS or HTML for an
|
2023-04-05 11:19:58 +02:00
|
|
|
|
email, there's a decent chance it should go in `email_base_default.html`.
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- All email is eventually sent by `zerver.lib.send_email.send_email`. There
|
2017-06-11 06:54:18 +02:00
|
|
|
|
are several other functions in `zerver.lib.send_email`, but all of them
|
|
|
|
|
eventually call the `send_email` function. The most interesting one is
|
2017-07-02 21:10:41 +02:00
|
|
|
|
`send_future_email`. The `ScheduledEmail` entries are eventually processed
|
2021-05-11 03:32:31 +02:00
|
|
|
|
by a supervisor job that runs `zerver/management/commands/deliver_scheduled_emails.py`.
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- Always use `user_profile.delivery_email`, not `user_profile.email`,
|
2021-08-20 21:53:28 +02:00
|
|
|
|
when passing data into the `send_email` library. The
|
2018-12-07 00:05:57 +01:00
|
|
|
|
`user_profile.email` field may not always be valid.
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- A good way to find a bunch of example email pathways is to `git grep` for
|
2017-06-11 06:54:18 +02:00
|
|
|
|
`zerver/emails` in the `zerver/` directory.
|
|
|
|
|
|
|
|
|
|
One slightly complicated decision you may have to make when adding an email
|
|
|
|
|
is figuring out how to schedule it. There are 3 ways to schedule email.
|
2021-08-20 22:54:08 +02:00
|
|
|
|
|
2024-07-04 12:33:43 +02:00
|
|
|
|
- Send it immediately, in the current Django process, e.g., by calling
|
2017-06-11 06:54:18 +02:00
|
|
|
|
`send_email` directly. An example of this is the `confirm_registration`
|
|
|
|
|
email.
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- Add it to a queue. An example is the `invitation` email.
|
|
|
|
|
- Send it (approximately) at a specified time in the future, using
|
2023-07-18 11:50:12 +02:00
|
|
|
|
`send_future_email`. An example is the `onboarding_zulip_topics` email.
|
2017-06-11 06:54:18 +02:00
|
|
|
|
|
|
|
|
|
Email takes about a quarter second per email to process and send. Generally
|
|
|
|
|
speaking, if you're sending just one email, doing it in the current process
|
|
|
|
|
is fine. If you're sending emails in a loop, you probably want to send it
|
|
|
|
|
from a queue. Documentation on our queueing system is available
|
2022-02-24 00:17:21 +01:00
|
|
|
|
[here](queuing.md).
|
2017-06-11 06:54:18 +02:00
|
|
|
|
|
2017-10-04 20:36:40 +02:00
|
|
|
|
## Development and testing
|
2017-06-11 06:54:18 +02:00
|
|
|
|
|
2017-10-04 20:36:40 +02:00
|
|
|
|
All the emails sent in the development environment can be accessed by
|
2021-08-20 21:53:28 +02:00
|
|
|
|
visiting `/emails` in the browser. The way that this works is that
|
2017-10-04 20:36:40 +02:00
|
|
|
|
we've set the email backend (aka what happens when you call the email
|
|
|
|
|
`.send()` method in Django) in the development environment to be our
|
2021-08-20 21:53:28 +02:00
|
|
|
|
custom backend, `EmailLogBackEnd`. It does the following:
|
2017-10-04 20:36:40 +02:00
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- Logs any sent emails to `var/log/email_content.log`. This log is
|
2017-10-04 20:36:40 +02:00
|
|
|
|
displayed by the `/emails` endpoint
|
2024-07-04 12:33:43 +02:00
|
|
|
|
(e.g., http://zulip.zulipdev.com:9991/emails).
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- Print a friendly message on console advertising `/emails` to make
|
2017-10-04 20:36:40 +02:00
|
|
|
|
this nice and discoverable.
|
|
|
|
|
|
2020-10-30 12:56:23 +01:00
|
|
|
|
### Testing in a real email client
|
|
|
|
|
|
2020-10-30 20:14:37 +01:00
|
|
|
|
You can also forward all the emails sent in the development
|
|
|
|
|
environment to an email account of your choice by clicking on
|
|
|
|
|
**Forward emails to an email account** on the `/emails` page. This
|
|
|
|
|
feature can be used for testing how the emails gets rendered by
|
2021-08-20 21:53:28 +02:00
|
|
|
|
actual email clients. This is important because web email clients
|
2020-10-30 20:14:37 +01:00
|
|
|
|
have limited CSS functionality, autolinkify things, and otherwise
|
|
|
|
|
mutate the HTML email one can see previewed on `/emails`.
|
|
|
|
|
|
docs: Add missing space to compound verbs “log in”, “set up”, etc.
Noun: backup, checkout, cleanup, login, logout, setup, shutdown, signup,
timeout.
Verb: back up, check out, clean up, log in, log out, set up, shut
down, sign up, time out.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-25 23:05:38 +02:00
|
|
|
|
To do this sort of testing, you need to set up an outgoing SMTP
|
2020-10-30 20:14:37 +01:00
|
|
|
|
provider. Our production advice for
|
2022-02-16 01:39:15 +01:00
|
|
|
|
[Gmail](../production/email.md#using-gmail-for-outgoing-email) and
|
2020-10-30 20:14:37 +01:00
|
|
|
|
[transactional email
|
2022-02-16 01:39:15 +01:00
|
|
|
|
providers](../production/email.md#free-outgoing-email-services) are
|
2020-10-30 20:14:37 +01:00
|
|
|
|
relevant; you can ignore the Gmail warning as Gmail's rate limits are
|
|
|
|
|
appropriate for this sort of low-volume testing.
|
|
|
|
|
|
|
|
|
|
Once you have the login credentials of the SMTP provider, since there
|
|
|
|
|
is not `/etc/zulip/settings.py` in development, configure it using the
|
|
|
|
|
following keys in `zproject/dev-secrets.conf`
|
2020-10-30 12:56:23 +01:00
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- `email_host` - SMTP hostname.
|
|
|
|
|
- `email_port` - SMTP port.
|
|
|
|
|
- `email_host_user` - Username of the SMTP user
|
|
|
|
|
- `email_password` - Password of the SMTP user.
|
|
|
|
|
- `email_use_tls` - Set to `true` for most providers. Else, don't set any value.
|
2020-10-30 12:56:23 +01:00
|
|
|
|
|
2020-10-30 20:14:37 +01:00
|
|
|
|
Here is an example of how `zproject/dev-secrets.conf` might look if
|
|
|
|
|
you are using Gmail.
|
2017-10-25 01:58:05 +02:00
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
|
```ini
|
2020-10-30 12:56:23 +01:00
|
|
|
|
email_host = smtp.gmail.com
|
|
|
|
|
email_port = 587
|
|
|
|
|
email_host_user = username@gmail.com
|
2021-01-28 19:32:01 +01:00
|
|
|
|
email_use_tls = true
|
2017-10-25 01:58:05 +02:00
|
|
|
|
|
2020-10-30 12:56:23 +01:00
|
|
|
|
# This is different from your Gmail password if you have 2FA enabled for your Google account.
|
|
|
|
|
# See the configuring Gmail to send email section above for more details
|
|
|
|
|
email_password = gmail_password
|
|
|
|
|
```
|
2017-10-25 01:58:05 +02:00
|
|
|
|
|
2020-10-30 13:12:58 +01:00
|
|
|
|
### Notes
|
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- Images won't be displayed in a real email client unless you change
|
2023-04-26 01:47:00 +02:00
|
|
|
|
the `images_base_url` used for emails to a public URL such as
|
2020-10-30 20:08:36 +01:00
|
|
|
|
`https://chat.zulip.org/static/images/emails` (image links to
|
|
|
|
|
`localhost:9991` aren't allowed by modern email providers). See
|
|
|
|
|
`zproject/email_backends.py` for more details.
|
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
|
- While running the backend test suite, we use
|
2020-10-30 20:08:36 +01:00
|
|
|
|
`django.core.mail.backends.locmem.EmailBackend` as the email
|
|
|
|
|
backend. The `locmem` backend stores messages in a special attribute
|
|
|
|
|
of the django.core.mail module, "outbox". The outbox attribute is
|
|
|
|
|
created when the first message is sent. It’s a list with an
|
|
|
|
|
EmailMessage instance for each message that would be sent.
|
2018-04-25 00:30:05 +02:00
|
|
|
|
|
2018-04-19 18:23:51 +02:00
|
|
|
|
## Email templates
|
|
|
|
|
|
2021-08-20 21:53:28 +02:00
|
|
|
|
Zulip's email templates live under `templates/zerver/emails`. Email
|
2018-04-19 18:23:51 +02:00
|
|
|
|
templates are a messy problem, because on the one hand, you want nice,
|
|
|
|
|
readable markup and styling, but on the other, email clients have very
|
2020-03-17 13:57:10 +01:00
|
|
|
|
limited CSS support and generally require us to inject any CSS we're
|
2021-08-20 21:53:28 +02:00
|
|
|
|
using in the emails into the email as inline styles. And then you
|
|
|
|
|
also need both plain-text and HTML emails. We solve these problems
|
2018-04-19 18:23:51 +02:00
|
|
|
|
using a combination of the
|
2023-04-03 17:30:03 +02:00
|
|
|
|
[css-inline](https://github.com/Stranger6667/css-inline) library and having
|
2018-04-19 18:23:51 +02:00
|
|
|
|
two copies of each email (plain-text and HTML).
|
|
|
|
|
|
2023-04-05 11:19:58 +02:00
|
|
|
|
So, for each email, there are two source templates: the `.txt` version
|
|
|
|
|
(for plain-text format) as well as a `.html` template. The `.txt` version
|
|
|
|
|
is used directly, while `.html` is processed by `css-inline`, which injects
|
|
|
|
|
the CSS we use for styling our emails (`templates/zerver/emails/email.css`)
|
|
|
|
|
into the templates just before sending an email.
|
2018-04-19 18:23:51 +02:00
|
|
|
|
|
|
|
|
|
While this model is great for the markup side, it isn't ideal for
|
2021-08-20 21:53:28 +02:00
|
|
|
|
[translations](../translating/translating.md). The Django
|
2018-04-19 18:23:51 +02:00
|
|
|
|
translation system works with exact strings, and having different new
|
|
|
|
|
markup can require translators to re-translate strings, which can
|
|
|
|
|
result in problems like needing 2 copies of each string (one for
|
2023-04-05 11:19:58 +02:00
|
|
|
|
plain-text, one for HTML). Re-translating these strings is
|
2018-04-19 18:23:51 +02:00
|
|
|
|
relatively easy in Transifex, but annoying.
|
|
|
|
|
|
|
|
|
|
So when writing email templates, we try to translate individual
|
|
|
|
|
sentences that are shared between the plain-text and HTML content
|
|
|
|
|
rather than larger blocks that might contain markup; this allows
|
|
|
|
|
translators to not have to deal with multiple versions of each string
|
|
|
|
|
in our emails.
|
|
|
|
|
|
|
|
|
|
One can test whether you did the translating part right by running
|
2023-04-05 11:19:58 +02:00
|
|
|
|
`manage.py makemessages` and then searching
|
2019-07-02 22:38:09 +02:00
|
|
|
|
for the strings in `locale/en/LC_MESSAGES/django.po`; if there
|
2018-04-19 18:23:51 +02:00
|
|
|
|
are multiple copies or they contain CSS colors, you did it wrong.
|
|
|
|
|
|
|
|
|
|
A final note for translating emails is that strings that are sent to
|
|
|
|
|
user accounts (where we know the user's language) are higher-priority
|
|
|
|
|
to translate than things sent to an email address (where we don't).
|
2024-07-04 12:33:43 +02:00
|
|
|
|
E.g., for password reset emails, it makes sense for the code path for
|
2018-04-19 18:23:51 +02:00
|
|
|
|
people with an actual account can be tagged for translation, while the
|
|
|
|
|
code path for the "you don't have an account email" might not be,
|
|
|
|
|
since we might not know what language to use in the second case.
|
|
|
|
|
|
|
|
|
|
Future work in this space could be to actually generate the plain-text
|
2023-04-05 11:19:58 +02:00
|
|
|
|
versions of emails from the `.html` markup, so that we don't
|
2018-04-19 18:23:51 +02:00
|
|
|
|
need to maintain two copies of each email's text.
|