iTranslated by AI
Domain Modeling for Emails
Overview
When building systems, there are many situations where you need to send emails.
Previously, I tried to model this in a DDD (Domain-Driven Design) way by defining and implementing the email itself as a Domain Model and the email sending function as a Domain Service.
However, I began to question whether an "email" or the act of "sending an email" is truly domain knowledge. I have reconsidered how to model (or not model) this, and I would like to summarize my personal views.
It Depends on the Domain
To put it simply, how you should model it depends on the domain.
For example, if you are developing a mailer or an email delivery service, then an Email is exactly what the domain model should be. This seems beyond doubt.
In this article, I want to focus on how to model it in a system where email is used merely as a means of notification.
Issues with Modeling Email as a Domain Model
The modeling I described in the overview looks something like the diagram below.

package Domain <<Rectangle>> {
class Email
interface EmailService {
void sendEmail(Email)
}
}
package Application <<Rectangle>> {
class SomeEmailSendingUseCase
}
SomeEmailSendingUseCase -> Email: Create
SomeEmailSendingUseCase -> EmailService: Send
In this modeling, we can see that there is a concept of an Email in this domain, but the problem is that we cannot tell what kind of content the email notifies without looking at the use case code.
So, what if we define a model for each type of email one by one?

package Domain <<Rectangle>> {
class Email
class OvertimeApplicationNotificationEmail
class OvertimeApplicationApprovalNotificationEmail
interface EmailService {
void sendEmail(Email)
}
}
package Application <<Rectangle>> {
class OvertimeApplicationNotificationEmailSendingUseCase
class OvertimeApplicationApprovalNotificationEmailSendingUseCase
}
Email <|-- OvertimeApplicationNotificationEmail
Email <|-- OvertimeApplicationApprovalNotificationEmail
OvertimeApplicationNotificationEmailSendingUseCase -> OvertimeApplicationNotificationEmail: Create
OvertimeApplicationNotificationEmailSendingUseCase -> EmailService: Send
OvertimeApplicationApprovalNotificationEmailSendingUseCase -> OvertimeApplicationApprovalNotificationEmail: Create
OvertimeApplicationApprovalNotificationEmailSendingUseCase -> EmailService: Send
This makes it clear what kinds of emails exist in this domain.
However, when using this modeling, each email class would likely end up holding the email subject and body.
If we model it such that the necessary information is passed to the constructor of each email class and the subject and body are generated inside the class, the domain model ends up taking on the responsibility of how the email should be displayed (e.g., constructing the email text). This deviates from the concerns of a domain model, and if you need to support multiple languages or switch to HTML emails, the logic within the domain model will be further cluttered with presentation-specific code.
What if we stop having the domain model construct the body and instead have the use case class set the text constructed into the email object?
In this case, we can tell that a concept like "Overtime Application Notification Email" exists, but it becomes unclear what specific information is being passed within that email.
A domain model should be an entity that speaks about domain knowledge.
If it becomes something "vague and mysterious," it can be said to be inappropriate as a domain model.
Email Sending as a "Notification" Use Case
Looking at it this way, is an email truly a domain model?
In most systems that send emails, isn't an Email just a means to perform a notification, a concept attached to an application use case, rather than being the domain model (domain knowledge) itself?
In other words, I thought that what exists as domain knowledge is a separate concept that holds the information to be notified, and the use case that delivers the information held by that model is sending by email.
For example, in the case of the overtime application mentioned above, what exists in the overtime application domain are models like Overtime Application or Overtime Application Approval, and there is an application use case for notifying these by email.

package Domain <<Rectangle>> {
class OvertimeApplication
interface OvertimeApplicationRepository
}
package Application <<Rectangle>> {
class OvertimeApplicationNotificationEmail {
+ ApplicationDetails
+ Recipient
+ Sender
+ ...
}
class OvertimeApplicationNotificationEmailSendingUseCase
interface EmailService
}
OvertimeApplicationNotificationEmailSendingUseCase -> OvertimeApplicationRepository: Get Overtime Application
OvertimeApplicationNotificationEmailSendingUseCase -> OvertimeApplicationNotificationEmail: Create
OvertimeApplicationNotificationEmailSendingUseCase -> EmailService: Send
The application layer (use case) is given the responsibility of acquiring the domain model that forms the main content of the notification, acquiring the information necessary to send the email (such as the notification recipient's email address), and processing these to construct the email object.
By doing this, only knowledge regarding the overtime application business remains in the domain, and the application layer can express the use case that notifications regarding these are performed via email in this application.
Sending via email is actually a requirement for each application; it just happened to be email this time, and the concept of email doesn't necessarily exist within the overtime application domain itself. (Of course, that perspective will change depending on the domain.)
With this modeling, even if the requirements increase to send overtime application notifications via SMS or Push notifications, you only need to add use cases that create various notifications using the overtime application model, without having to modify the domain model.
The means of notification should have nothing to do with the business content of the overtime application, which is the domain itself.
Notifications Involve "Display"
There is still an issue with the modeling above.
Whether it's email or a push notification, a notification is ultimately something meant for "displaying" information.
Constructing the display content of an email within the application layer (inside the use case class) and sending it exceeds the responsibilities of the application layer.
Initially, you might be sending plain text emails, but then you might want to send HTML emails or change the wording; the parts that handle display are highly likely to change.
If the application layer is modified every time these changes occur, it could potentially affect processes other than the display logic.
Pushing frequently changing logic to outer layers was one of the goals of Clean Architecture.
Therefore, shouldn't the responsibility of the application layer be limited to gathering and structuring information for email transmission, while the actual text or layout used to send the email should be pushed to the presentation layer?

package Domain <<Rectangle>> {
class OvertimeApplication
interface OvertimeApplicationRepository
}
package Application <<Rectangle>> {
class OvertimeApplicationNotificationEmail {
+ ApplicationDetails
+ Recipient
+ Sender
+ ...
}
class OvertimeApplicationNotificationEmailConstructionUseCase
interface OvertimeApplicationNotificationEmailConstructionInputPort
interface OvertimeApplicationNotificationEmailConstructionOutputPort
}
package Presentation <<Rectangle>> {
class OvertimeApplicationNotificationEmailSendingController
class OvertimeApplicationNotificationEmailSendingPresenter {
void constructEmailDisplayContent()
}
interface EmailSendingPort
}
package Infrastructure <<Rectangle>> {
class SendGridEmailSendingPort
class GmailEmailSendingPort
}
OvertimeApplicationNotificationEmailConstructionInputPort <|.. OvertimeApplicationNotificationEmailConstructionUseCase
OvertimeApplicationNotificationEmailSendingPresenter ..|> OvertimeApplicationNotificationEmailConstructionOutputPort
OvertimeApplicationNotificationEmailSendingController -> OvertimeApplicationNotificationEmailConstructionInputPort: IN OvertimeApplicationID
OvertimeApplicationNotificationEmailConstructionUseCase -> OvertimeApplicationRepository: Get Overtime Application
OvertimeApplicationNotificationEmailConstructionUseCase -> OvertimeApplicationNotificationEmail: Create
OvertimeApplicationNotificationEmailConstructionUseCase -> OvertimeApplicationNotificationEmailConstructionOutputPort: OUT OvertimeApplicationNotificationEmail
OvertimeApplicationNotificationEmailSendingPresenter -> EmailSendingPort: Send Email
SendGridEmailSendingPort ..|> EmailSendingPort
GmailEmailSendingPort ..|> EmailSendingPort
By constructing the email display content in the presentation layer based on the data object received from the application layer, you can implement various output formats—such as a presenter for plain text or one for HTML emails—using the same use case class without modifying the application layer.
Summary
Email sending sounds very much like a functional requirement of a system, making me hesitant to include it in the domain layer. I used to wonder, "Is this actually domain knowledge surrounding the system?" but I found this approach to be quite clarifying.
Of course, this isn't necessarily the only correct answer, and as I mentioned at the beginning, there are certainly patterns where Email itself is domain knowledge.
Exploring domain models is an endless journey with no single answer.
Discussion