Wrong Content-Type of Service Desk emails with template & attachments
Solves Wrong Content-Type of Service Desk `new_note` e... (#404967 - closed)
What does this MR do and why?
Service Desk new_note
emails with a new_note.md
template file in a repository and attachments used a wrong Content-Type
text/plain
instead of text/html
. We also had no test coverage for this and also had no specs for service desk notification emails without a template.
Before native attachments we always set the content type to text/html
. While working on native attachments we added a condition that only sets the content type to text/html
if no attachments are present. But that does not work for cases with template and attachments.
This is the comment that explain why we added the condition. TL;DR before we always set it to html
, but with attachments we need a different content type
What we missed in that MR (maybe due to test coverage issues) was that we also needed to set this for cases with template and attachments.
Email envelopes and email parts
- Emails with both text and html have a content type of
multipart/alternative
. Inside, it has two parts,text/plain
andtext/html
. - Emails with attachments have a content type of
multipart/mixed
. If they only contain text or html then they have atext/plain
ortext/html
part followed by the attachment part. The attachment is always last. - Emails with attachments that have both text and html, have the
multipart/mixed
multipart/alternative
content type.The first part is an envelopeIt contains the two partsmultipart/alternative
that contains two partstext/plain
andtext/html
. After the envelope all attachment parts follow.text/plain
andtext/html
followed by all attachments.
How does GitLab render these emails?
-
Without template: We provide a text and html version.
-
Without attachments the content type is
multipart/alternative
and the email contains the two content parts. -
With attachments the content type is
multipart/mixed
multipart/alternative
.First part is aIt contains the two content partsmultipart/alternative
envelope with the two content parts. After that all attachments.text/plain
andtext/html
followed by all attachments.
-
Without attachments the content type is
-
With template: We only provide a html version.
-
Without attachments the content type is
text/html
. End of story. -
With attachments the content type is
multipart/mixed
. The first part istext/html
(bug behavior wastext/plain
) with the template content followed by all attachments.
-
Without attachments the content type is
What has been done?
- Because email parts for emails with and without attachments and with and without templates are quite different we need strict tests that also inspect envelopes, email parts, their content types and the content.
- I added a new shared example
a service desk notification email
that checks envelopes and parts for emails without template and with and without attachments - I modified the existing shared example for template content and renamed it to
a service desk notification email with template content
, so it also checks the envelope and content type (it only checked the content before).
- I added a new shared example
- We reordered some method calls and add the attachments after the mail has been generated.
-
For some reason the content part that was generated from a template had the content typeWe only set thetext/plain
. We can only change content types for parts, once the email has been generated. So I change the part content type inservice_desk_new_note_email
when attachments are present and we use a template.content_type
for template content directly via the options.ActionMailer
overrides this when we add new attachments after the mail has been generated. -
We don't have any information whether a template was used or not. We can only determine this from the parts that are used in the email. So I added a helper methodWe don't need that anymore because we rely onuses_template?
that checks that we do not have anymultipart/alternative
envelopes in our email.ActionMailer
to figure things out. - Refactored some variable naming in the spec file as they were not very clear or misleading.
- We did not have examples in place that checked emails without template. We now have that for all notification emails.
- Pulled specs for attachments out of the
with template
context and addressed both with and without template in one go, to avoid code duplication.
Screenshots or screen recordings
Before:
After:
How to set up and validate locally
- Find a Service Desk issue, make an issue a Service Desk issue or create a new one (email ingestion required)
# Make Service Desk issue from existing issue # Use whatever project with activated Service Desk. Defaults to "on" for every project project = Project.find_by(name: 'Flight') # E.g. issue with iid 12 in Flight project issue = Issue.find_by(project: project, iid: 12) your_email = 'yourname@gitlab.com' # Service Desk issues are authored by GitLab Support Bot issue.update!(service_desk_reply_to: your_email, author_id: User.support_bot.id) # Issue Email Participant is mandatory to generate service desk emails IssueEmailParticipant.create!(issue_id: issue.id, email: your_email)
- Add a template for the
new_note
email in the project's repository in.gitlab/service_deak_templates/new_note.md
with some content. E.g.%{SYSTEM_HEADER} New message for issue **%{ISSUE_ID}** on **%{ISSUE_PATH}** %{NOTE_TEXT} [Unsubscribe](%{UNSUBSCRIBE_URL}) %{SYSTEM_FOOTER}
- Go back to the issue and add a public comment. You should see the email in letter opener and it should use the template
- Write a public comment and add an upload to the comment. You should see the email in letter opener and it should display correctly (html) and have the attachments (link displayed in the list view).
- (Optional) If you want to further inspect the email, you could set up outgoing emails by having a
config/initializers/smtp_settings.rb
file in place and configured (see configuration inconfig/initializers/smtp_settings.rb.sample
. I use a mailgun test account. Works fine). Aftergdk restart
and sending another email with attachments you should find that email in the email participants mailbox (e.g. your company's gmail account). If you inspect the original email, you should see the content typemultipart/mixed
. The content part should have a content type oftext/html
.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #404967 (closed)