Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add email service #123

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open

Add email service #123

wants to merge 10 commits into from

Conversation

ttoino
Copy link
Member

@ttoino ttoino commented Feb 23, 2023

Closes #82.

Adds a new EmailService with a method send, which receives an instance of EmailBuilder.
Two builders were implemented: SimpleEmailBuilder, which receives all properties directly, and TemplateEmailBuilder, which generates the subject, content and attachments from the given data using mustache templates.

Each different template email should be a subclass of TemplateEmailBuilder, here's an example:

data class ExampleContext(
    val title: String,
    val todos: List<String>
)

class ExampleEmailBuilder : TemplateEmailBuilder<ExampleContext>("example")
<!-- .../resources/templates/email/example.mustache -->
---
subject: Example: {{title}}
attachments:
    - {{title}}.txt :: classpath:application.properties
inline:
    - image :: classpath:image.png
---

# {{title}}

{{#todos}}
- {{.}}
{{/todos}}

![Image](cid:image)
service.send(
    ExampleEmailBuilder()
        .data(ExampleContext("Grocery list", listOf("Potatoes", "Tomatoes", "Cheese")))
        .to("[email protected]")
)

This sends an email that looks something like this:

image image

Review checklist

  • Contains enough appropriate tests
  • Behavior is as expected
  • Clean, well structured code

@ttoino

This comment was marked as resolved.

@ttoino ttoino force-pushed the feature/email-service branch 2 times, most recently from 78a6476 to c1c29c2 Compare February 25, 2023 11:53
@codecov-commenter
Copy link

codecov-commenter commented Mar 4, 2023

Codecov Report

Patch coverage: 16.66% and project coverage change: -11.07 ⚠️

Comparison is base (2a423b6) 88.49% compared to head (33dc731) 77.43%.

Additional details and impacted files
@@              Coverage Diff               @@
##             develop     #123       +/-   ##
==============================================
- Coverage      88.49%   77.43%   -11.07%     
- Complexity       345      355       +10     
==============================================
  Files             61       67        +6     
  Lines            791      935      +144     
  Branches          65       76       +11     
==============================================
+ Hits             700      724       +24     
- Misses            57      177      +120     
  Partials          34       34               
Impacted Files Coverage Δ
.../pt/up/fe/ni/website/backend/BackendApplication.kt 50.00% <ø> (ø)
...up/fe/ni/website/backend/email/BaseEmailBuilder.kt 0.00% <0.00%> (ø)
.../fe/ni/website/backend/email/SimpleEmailBuilder.kt 0.00% <0.00%> (ø)
...e/ni/website/backend/email/TemplateEmailBuilder.kt 0.00% <0.00%> (ø)
...t/up/fe/ni/website/backend/service/EmailService.kt 37.50% <37.50%> (ø)
.../fe/ni/website/backend/config/email/EmailConfig.kt 100.00% <100.00%> (ø)
...site/backend/config/email/EmailConfigProperties.kt 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ttoino ttoino marked this pull request as ready for review March 4, 2023 01:43
@ttoino
Copy link
Member Author

ttoino commented Mar 4, 2023

I'm marking as ready for review, but a few things are still missing:

  • Support for images and other embedded resources like styles
  • Default email layout and styles - Supported, design not finalized
  • Some kind of testing (not sure what to do here)
  • Maybe markdown support
  • Documentation - in the wiki

@github-actions
Copy link

Check the documentation preview: https://64148c8507396b40a734ad10--niaefeup-backend-docs.netlify.app

@github-actions
Copy link

Check the documentation preview: https://64161557a5113f2455baeb87--niaefeup-backend-docs.netlify.app

@jamcunha jamcunha self-requested a review March 19, 2023 14:00
Copy link
Member

@BrunoRosendo BrunoRosendo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a great PR 🎉 Tremendous job! I left minor/cosmetic suggestions

One thing I don't like is having email as a top-level package, I think we already have enough of them. What do you think of storing the builders under service/email/? together with EmailService?

import pt.up.fe.ni.website.backend.config.email.EmailConfigProperties
import pt.up.fe.ni.website.backend.model.Account

abstract class BaseEmailBuilder : EmailBuilder {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing the builders should be trivial with unit tests (testing validation, whether emails are added in the right places, etc.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One can also mock the java mail library and verify if the methods are being called with the correct arguments

src/main/resources/email/style.css Show resolved Hide resolved
this.data = data
}

override fun build(helper: MimeMessageHelper, emailConfigProperties: EmailConfigProperties) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is a bit verbose, I'd consider splitting it into more methods if possible

Comment on lines +29 to +46
spring.mail.host=
spring.mail.port=
spring.mail.username=
spring.mail.password=
Copy link
Member

@BrunoRosendo BrunoRosendo Mar 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we need to configure this in prod? It'd be nice to have some example in the wiki if so

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll add a section on how to configure it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is it? I may be legally blind

@github-actions
Copy link

Check the documentation preview: https://641c7e9b8ddee704dad18da7--niaefeup-backend-docs.netlify.app

@bdmendes bdmendes self-requested a review March 29, 2023 15:22
@BrunoRosendo
Copy link
Member

Have you decided something about the tests?

@github-actions
Copy link

Check the documentation preview: https://649c5a2becd22626c57b25e2--niaefeup-backend-docs.netlify.app

private var html: String? = null
private var subject: String? = null
private var attachments: MutableList<EmailFile> = mutableListOf()
private var inlines: MutableList<EmailFile> = mutableListOf()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are inlines?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An inline is a file that is sent along with an email, similar to an attachment, except it doesn't show as a downloadable file and instead can be referenced from the email html using the cid: protocol. This is how inline images are implemented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice. Maybe we should have a comment explaining that since I'm guessing a lot of people won't know that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Email service
6 participants