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 18 commits into
base: develop
Choose a base branch
from
Open

Add email service #123

wants to merge 18 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
Collaborator

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

Copy link
Collaborator

@MRita443 MRita443 Jan 24, 2025

Choose a reason for hiding this comment

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

Could you try to add this @rubuy-74 ? (Meant to comment under the open conversation, about adding instructions to the wiki)

@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?

@ttoino ttoino force-pushed the feature/email-service branch from 744c69c to 33dc731 Compare June 28, 2023 15:58
@github-actions
Copy link

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

Copy link

Check the documentation preview: https://6748a29cc73b7330bd8b3797--niaefeup-backend-docs.netlify.app

@MRita443 MRita443 requested review from MRita443 and removed request for jamcunha December 4, 2024 16:46
@MRita443 MRita443 requested a review from BrunoRosendo January 24, 2025 17:22
Copy link

Check the documentation preview: https://6793cddd62e627d5c653cc1b--niaefeup-backend-docs.netlify.app

Copy link
Collaborator

@MRita443 MRita443 left a comment

Choose a reason for hiding this comment

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

It's looking good so far. Shouldn't we add tests for SimpleEmailBuilder and TemplateEmailBuildertoo?

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

@MRita443 MRita443 Jan 24, 2025

Choose a reason for hiding this comment

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

Could you try to add this @rubuy-74 ? (Meant to comment under the open conversation, about adding instructions to the wiki)

Comment on lines +81 to +89
class BaseEmailBuilderImpl(
override val emailConfigProperties: EmailConfigProperties
) : BaseEmailBuilder() {
fun getToEmails(): Set<String> = to
fun getCcEmails(): Set<String> = cc
fun getBccEmails(): Set<String> = bcc
fun getFromEmail(): String? = from
fun getName(): String? = fromPersonal
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this needed?

@rubuy-74
Copy link
Contributor

rubuy-74 commented Jan 26, 2025

Don't forget to add instructions for testing on the wiki.

@rubuy-74 rubuy-74 requested a review from MRita443 January 28, 2025 15:31
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.

I like the changes from the last time I saw it. Good job with the tests!

One thing that would be great to improve is the size of the PRs. We didn't use to do this in NI but it's much easier to do small and incremental PRs, even if the feature or issue isn't fully closed. This should help make the reviews quicker and avoid having stale CRs for months at a time

PS: you need to fix the lint, it seems

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.

approve to unblock, but look at the comments

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
8 participants