2020-10-30 14:08:56 -04:00
---
stage: none
group: unassigned
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 14:08:56 -04:00
---
2021-03-15 14:09:05 -04:00
# Working with email in development
2017-09-12 11:41:55 -04:00
2020-06-03 14:08:28 -04:00
## Ensuring compatibility with mailer Sidekiq jobs
A Sidekiq job is enqueued whenever `deliver_later` is called on an `ActionMailer` .
If a mailer argument needs to be added or removed, it is important to ensure
2022-01-20 10:11:58 -05:00
both backward and forward compatibility. Adhere to the Sidekiq steps for
[changing the arguments for a worker ](sidekiq/compatibility_across_updates.md#changing-the-arguments-for-a-worker ).
2020-06-03 14:08:28 -04:00
2022-03-16 23:08:05 -04:00
The same applies to a new mailer method, or a new mailer. If you introduce either,
follow the steps for [adding new workers ](sidekiq/compatibility_across_updates.md#adding-new-workers ).
2020-06-03 14:08:28 -04:00
In the following example from [`NotificationService` ](https://gitlab.com/gitlab-org/gitlab/-/blob/33ccb22e4fc271dbaac94b003a7a1a2915a13441/app/services/notification_service.rb#L74 )
adding or removing an argument in this mailer's definition may cause problems
during deployment before all Rails and Sidekiq nodes have the updated code.
```ruby
mailer.unknown_sign_in_email(user, ip, time).deliver_later
```
2017-09-12 11:41:55 -04:00
## Sent emails
To view rendered emails "sent" in your development instance, visit
[`/rails/letter_opener` ](http://localhost:3000/rails/letter_opener ).
2021-07-23 14:10:06 -04:00
[S/MIME signed ](../administration/smime_signing_email.md ) emails
2019-08-26 16:31:04 -04:00
[cannot be currently previewed ](https://github.com/fgrehm/letter_opener_web/issues/96 ) with
2019-07-10 15:40:28 -04:00
`letter_opener` .
2017-09-12 11:41:55 -04:00
## Mailer previews
Rails provides a way to preview our mailer templates in HTML and plaintext using
2020-09-25 02:09:42 -04:00
sample data.
2017-09-12 11:41:55 -04:00
2020-04-21 11:21:10 -04:00
The previews live in [`app/mailers/previews` ](https://gitlab.com/gitlab-org/gitlab-foss/tree/master/app/mailers/previews ) and can be viewed at
2017-09-12 11:41:55 -04:00
[`/rails/mailers` ](http://localhost:3000/rails/mailers ).
2020-04-07 23:09:31 -04:00
See the [Rails guides ](https://guides.rubyonrails.org/action_mailer_basics.html#previewing-emails ) for more information.
2017-09-12 11:41:55 -04:00
2018-03-02 06:32:39 -05:00
## Incoming email
1. Go to the GitLab installation directory.
1. Find the `incoming_email` section in `config/gitlab.yml` , enable the
feature and fill in the details for your specific IMAP server and email
account:
2019-07-09 03:16:17 -04:00
Configuration for Gmail / Google Apps, assumes mailbox `gitlab-incoming@gmail.com` :
```yaml
incoming_email:
enabled: true
2021-03-16 11:11:17 -04:00
# The email address including the %{key} placeholder that will be replaced to reference the
# item being replied to. This %{key} should be included in its entirety within the email
# address and not replaced by another value.
2022-08-16 08:12:38 -04:00
# For example: emailaddress+%{key}@gmail.com.
2021-03-16 11:11:17 -04:00
# The placeholder must appear in the "user" part of the address (before the `@` ). It can be omitted but some features,
# including Service Desk, may not work properly.
2019-07-09 03:16:17 -04:00
address: "gitlab-incoming+%{key}@gmail.com"
# Email account username
# With third party providers, this is usually the full email address.
# With self-hosted email servers, this is usually the user part of the email address.
user: "gitlab-incoming@gmail.com"
# Email account password
password: "[REDACTED]"
# IMAP server host
host: "imap.gmail.com"
# IMAP server port
port: 993
# Whether the IMAP server uses SSL
ssl: true
# Whether the IMAP server uses StartTLS
start_tls: false
# The mailbox where incoming mail will end up. Usually "inbox".
mailbox: "inbox"
# The IDLE command timeout.
idle_timeout: 60
2020-05-12 20:07:50 -04:00
2022-05-06 14:09:03 -04:00
# Whether to expunge (permanently remove) messages from the mailbox when they are marked as deleted after delivery
2020-05-12 20:07:50 -04:00
expunge_deleted: false
2019-07-09 03:16:17 -04:00
```
2020-12-16 10:10:18 -05:00
As mentioned, the part after `+` is ignored, and this message is sent to the mailbox for `gitlab-incoming@gmail.com` .
2018-03-02 06:32:39 -05:00
2022-02-07 19:14:26 -05:00
1. Read the [MailRoom Gem updates ](#mailroom-gem-updates ) section for more details before you proceed to make sure you have the right version of MailRoom installed. In summary, you need to update the `gitlab-mail_room` version in the `Gemfile` to the latest `gitlab-mail_room` temporarily and run `bundle install` . **Do not commit** this change as it's a temporary workaround.
2018-03-20 22:33:28 -04:00
1. Run this command in the GitLab root directory to launch `mail_room` :
2018-03-02 06:32:39 -05:00
2020-01-30 10:09:15 -05:00
```shell
2019-07-09 03:16:17 -04:00
bundle exec mail_room -q -c config/mail_room.yml
```
2018-03-02 06:32:39 -05:00
1. Verify that everything is configured correctly:
2020-01-30 10:09:15 -05:00
```shell
2019-07-09 03:16:17 -04:00
bundle exec rake gitlab:incoming_email:check RAILS_ENV=development
```
2018-03-02 06:32:39 -05:00
1. Reply by email should now be working.
2018-04-09 05:39:03 -04:00
## Email namespace
2020-02-11 22:08:55 -05:00
As of GitLab 11.7, we support a new format for email handler addresses. This was done to
2019-01-07 19:12:59 -05:00
support catch-all mailboxes.
2018-04-09 05:39:03 -04:00
2019-01-07 19:12:59 -05:00
If you need to implement a feature which requires a new email handler, follow these rules
for the format of the email key:
2018-04-09 05:39:03 -04:00
2020-02-11 22:08:55 -05:00
- Actions are always at the end, separated by `-` . For example `-issue` or `-merge-request`
2019-01-07 19:12:59 -05:00
- If your feature is related to a project, the key begins with the project identifiers (project path slug
2020-05-07 02:09:38 -04:00
and project ID), separated by `-` . For example, `gitlab-org-gitlab-foss-20`
2019-01-07 19:12:59 -05:00
- Additional information, such as an author's token, can be added between the project identifiers and
2020-02-11 22:08:55 -05:00
the action, separated by `-` . For example, `gitlab-org-gitlab-foss-20-Author_Token12345678-issue`
2019-01-07 19:12:59 -05:00
- You register your handlers in `lib/gitlab/email/handler.rb`
Examples of valid email keys:
2019-09-18 14:06:14 -04:00
- `gitlab-org-gitlab-foss-20-Author_Token12345678-issue` (create a new issue)
- `gitlab-org-gitlab-foss-20-Author_Token12345678-merge-request` (create a new merge request)
2019-07-09 03:16:17 -04:00
- `1234567890abcdef1234567890abcdef-unsubscribe` (unsubscribe from a conversation)
- `1234567890abcdef1234567890abcdef` (reply to a conversation)
2019-01-07 19:12:59 -05:00
2021-07-23 14:10:06 -04:00
The action `-issue-` is used in GitLab as the handler for the Service Desk feature.
2019-01-07 19:12:59 -05:00
### Legacy format
Although we continue to support the older legacy format, no new features should use a legacy format.
These are the only valid legacy formats for an email handler:
2018-04-09 05:39:03 -04:00
2019-07-09 03:16:17 -04:00
- `path/to/project+namespace`
- `path/to/project+namespace+action`
- `namespace`
- `namespace+action`
2018-04-09 05:39:03 -04:00
2021-07-23 14:10:06 -04:00
In GitLab, the handler for the Service Desk feature is `path/to/project` .
2018-04-09 05:39:03 -04:00
2022-01-05 10:13:51 -05:00
### MailRoom Gem updates
We use [`gitlab-mail_room` ](https://gitlab.com/gitlab-org/gitlab-mail_room ), a
fork of [`MailRoom` ](https://github.com/tpitale/mail_room/ ), to ensure
that we can make updates quickly to the gem if necessary. We try to upstream
changes as soon as possible and keep the two projects in sync.
Updating the `gitlab-mail_room` dependency in `Gemfile` is deprecated at
the moment in favor of updating the version in
[Omnibus ](https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/master/config/software/mail_room.rb )
(see [example merge request ](https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/5816 ))
and Helm Chart configuration (see [example merge request ](https://gitlab.com/gitlab-org/build/CNG/-/merge_requests/854 )).
#### Rationale
This was done because to avoid [thread deadlocks ](https://github.com/ruby/net-imap/issues/14 ), `MailRoom` needs
2022-08-23 20:12:25 -04:00
an updated version of the `net-imap` gem. However, this
[version of the net-imap cannot be installed by an unprivileged user ](https://github.com/ruby/net-imap/issues/14 ) due to
[an error installing the digest gem ](https://github.com/ruby/digest/issues/14 ).
2022-08-07 23:09:21 -04:00
[This bug in the Ruby interpreter ](https://bugs.ruby-lang.org/issues/17761 ) was fixed in Ruby
2022-01-05 10:13:51 -05:00
3.0.2.
Updating the gem directly in the GitLab Rails `Gemfile` caused a [production incident ](https://gitlab.com/gitlab-com/gl-infra/production/-/issues/4053 )
since Kubernetes pods do not run as privileged users.
Note that Omnibus GitLab runs `/opt/gitlab/embedded/bin/mail_room`
instead of `bundle exec rake` to avoid loading the older version. With a
Kubernetes install, the MailRoom version has always been explicitly set
in the Helm Chart configuration rather than the `Gemfile` .
#### Preserving backwards compatibility
Removing the `Gemfile` would break incoming e-mail processing for source
installs. For now, source installs are advised to upgrade manually to
the version specified in Omnibus and run `bin/mail_room` directly as
done with Omnibus.
We can reconsider this deprecation once we upgrade to Ruby 3.0.
2017-09-12 11:41:55 -04:00
---
2021-06-28 08:38:12 -04:00
[Return to Development documentation ](index.md )