Skip to content

Commit a005577

Browse files
committed
chg: improve email sending for order complete
1 parent 1929ba7 commit a005577

File tree

6 files changed

+239
-3
lines changed

6 files changed

+239
-3
lines changed

docker-compose.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ x-django-env: &django-env
1515
MSGRAPH_CLIENT_ID: "${MSGRAPH_CLIENT_ID:-}"
1616
MSGRAPH_CLIENT_SECRET: "${MSGRAPH_CLIENT_SECRET:-}"
1717
NOTIFICATIONS_NEW_PROJECTS: "${NOTIFICATIONS_NEW_PROJECTS:-}"
18-
DJANGO_EMAIL_BACKEND: "${DJANGO_EMAIL_BACKEND:-}"
18+
DJANGO_EMAIL_BACKEND: "${DJANGO_EMAIL_BACKEND:-django.core.mail.backends.smtp.EmailBackend}"
1919

2020
x-django-prod-env: &django-prod-env
2121
<<: *django-env
@@ -169,3 +169,10 @@ services:
169169
- 5173:5173
170170
volumes:
171171
- ./src/frontend/src:/app/src
172+
173+
mailtrap:
174+
profiles:
175+
- dev
176+
image: dbck/mailtrap
177+
ports:
178+
- "8090:80"

src/config/settings/local.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
3535
EMAIL_BACKEND = env(
3636
"DJANGO_EMAIL_BACKEND",
37-
default="django.core.mail.backends.console.EmailBackend",
37+
default="django.core.mail.backends.smtp.EmailBackend",
3838
)
39-
39+
EMAIL_HOST = env("EMAIL_HOST", default="mailtrap")
40+
EMAIL_PORT = env.int("EMAIL_PORT", default=25)
41+
EMAIL_HOST_USER = env("EMAIL_HOST_USER", default="mailtrap")
42+
EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD", default="mailtrap")
4043

4144
###########################################
4245
# django-debug-toolbar

src/genlab_bestilling/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.conf import settings
88
from django.db import models, transaction
99
from django.db.models import Q
10+
from django.template.loader import render_to_string
1011
from django.urls import reverse
1112
from django.utils import timezone
1213
from django.utils.translation import gettext_lazy as _
@@ -400,11 +401,21 @@ def __str__(self):
400401
)
401402
def notify_order_completed(self) -> None:
402403
o = self.get_real_instance()
404+
context = {
405+
"title": f"Order {o} is completed",
406+
"object": o,
407+
"object_type": o.__class__.__name__,
408+
}
409+
html_message = render_to_string(
410+
"genlab_bestilling/mail/order_complete.html", context=context
411+
)
412+
403413
app.configure_task("nina.tasks.send_email_async").defer(
404414
subject=f"{o} - completed",
405415
message="the order is completed",
406416
from_email=None,
407417
recipient_list=[self.contact_email],
418+
html_message=html_message,
408419
)
409420

410421

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>{{ title }}</title>
8+
<style media="all" type="text/css">
9+
@media all {
10+
.btn-primary table td:hover {
11+
background-color: #ec0867 !important;
12+
}
13+
14+
.btn-primary a:hover {
15+
background-color: #ec0867 !important;
16+
border-color: #ec0867 !important;
17+
}
18+
}
19+
20+
@media only screen and (max-width: 640px) {
21+
22+
.main p,
23+
.main td,
24+
.main span {
25+
font-size: 16px !important;
26+
}
27+
28+
.wrapper {
29+
padding: 8px !important;
30+
}
31+
32+
.content {
33+
padding: 0 !important;
34+
}
35+
36+
.container {
37+
padding: 0 !important;
38+
padding-top: 8px !important;
39+
width: 100% !important;
40+
}
41+
42+
.main {
43+
border-left-width: 0 !important;
44+
border-radius: 0 !important;
45+
border-right-width: 0 !important;
46+
}
47+
48+
.btn table {
49+
max-width: 100% !important;
50+
width: 100% !important;
51+
}
52+
53+
.btn a {
54+
font-size: 16px !important;
55+
max-width: 100% !important;
56+
width: 100% !important;
57+
}
58+
}
59+
60+
@media all {
61+
.ExternalClass {
62+
width: 100%;
63+
}
64+
65+
.ExternalClass,
66+
.ExternalClass p,
67+
.ExternalClass span,
68+
.ExternalClass font,
69+
.ExternalClass td,
70+
.ExternalClass div {
71+
line-height: 100%;
72+
}
73+
74+
.apple-link a {
75+
color: inherit !important;
76+
font-family: inherit !important;
77+
font-size: inherit !important;
78+
font-weight: inherit !important;
79+
line-height: inherit !important;
80+
text-decoration: none !important;
81+
}
82+
83+
#MessageViewBody a {
84+
color: inherit;
85+
text-decoration: none;
86+
font-size: inherit;
87+
font-family: inherit;
88+
font-weight: inherit;
89+
line-height: inherit;
90+
}
91+
}
92+
93+
p {
94+
font-family: Helvetica, sans-serif;
95+
font-size: 16px;
96+
font-weight: normal;
97+
margin: 0;
98+
margin-bottom: 16px;
99+
}
100+
</style>
101+
</head>
102+
103+
<body
104+
style="font-family: Helvetica, sans-serif; -webkit-font-smoothing: antialiased; font-size: 16px; line-height: 1.3; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; background-color: #f4f5f6; margin: 0; padding: 0;">
105+
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body"
106+
style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f4f5f6; width: 100%;"
107+
width="100%" bgcolor="#f4f5f6">
108+
<tr>
109+
<td style="font-family: Helvetica, sans-serif; font-size: 16px; vertical-align: top;" valign="top">&nbsp;
110+
</td>
111+
<td class="container"
112+
style="font-family: Helvetica, sans-serif; font-size: 16px; vertical-align: top; max-width: 600px; padding: 0; padding-top: 24px; width: 600px; margin: 0 auto;"
113+
width="600" valign="top">
114+
<div class="content"
115+
style="box-sizing: border-box; display: block; margin: 0 auto; max-width: 600px; padding: 0;">
116+
117+
<!-- START CENTERED WHITE CONTAINER -->
118+
<!--span class="preheader"
119+
style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">{{
120+
text_preview }}</span-->
121+
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="main"
122+
style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background: #ffffff; border: 1px solid #eaebed; border-radius: 16px; width: 100%;"
123+
width="100%">
124+
125+
<!-- START MAIN CONTENT AREA -->
126+
<tr>
127+
<td class="wrapper"
128+
style="font-family: Helvetica, sans-serif; font-size: 16px; vertical-align: top; box-sizing: border-box; padding: 24px;"
129+
valign="top">
130+
{% block content %}{% endblock content %}
131+
</td>
132+
</tr>
133+
134+
<!-- END MAIN CONTENT AREA -->
135+
</table>
136+
137+
<!-- START FOOTER -->
138+
<!--div class="footer" style="clear: both; padding-top: 24px; text-align: center; width: 100%;">
139+
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;" width="100%">
140+
<tr>
141+
<td class="content-block" style="font-family: Helvetica, sans-serif; vertical-align: top; color: #9a9ea6; font-size: 16px; text-align: center;" valign="top" align="center">
142+
<span class="apple-link" style="color: #9a9ea6; font-size: 16px; text-align: center;">Company Inc, 7-11 Commercial Ct, Belfast BT1 2NB</span>
143+
<br> Don't like these emails? <a href="http://htmlemail.io/blog" style="text-decoration: underline; color: #9a9ea6; font-size: 16px; text-align: center;">Unsubscribe</a>.
144+
</td>
145+
</tr>
146+
<tr>
147+
<td class="content-block powered-by" style="font-family: Helvetica, sans-serif; vertical-align: top; color: #9a9ea6; font-size: 16px; text-align: center;" valign="top" align="center">
148+
Powered by <a href="http://htmlemail.io" style="color: #9a9ea6; font-size: 16px; text-align: center; text-decoration: none;">HTMLemail.io</a>
149+
</td>
150+
</tr>
151+
</table>
152+
</div-->
153+
154+
<!-- END FOOTER -->
155+
156+
<!-- END CENTERED WHITE CONTAINER -->
157+
</div>
158+
</td>
159+
<td style="font-family: Helvetica, sans-serif; font-size: 16px; vertical-align: top;" valign="top">&nbsp;
160+
</td>
161+
</tr>
162+
</table>
163+
</body>
164+
165+
</html>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{% extends "genlab_bestilling/mail/base.html" %}
2+
{% load order_tags %}
3+
4+
{% block content %}
5+
<p>The order is completed</p>
6+
7+
<p>Summary:</p>
8+
{% object-detail-staff object=object %}
9+
10+
{% if object_type == 'ExtractionOrder' %}
11+
12+
<h5>Delivered Samples</h5>
13+
<div>
14+
<p>Uploaded {{ object.samples.count }} samples</p>
15+
</div>
16+
17+
{% elif object_type == 'AnalysisOrder' %}
18+
19+
<h5>Samples to analyze</h5>
20+
<div>
21+
<p>Analyzed {{ object.samples.count }} samples</p>
22+
</div>
23+
24+
{% elif object_type == 'EquipmentOrder' %}
25+
26+
<h5>Requested Equipment</h5>
27+
{% fragment as table_header %}
28+
{% #table-cell header=True %}Equipment{% /table-cell %}
29+
{% #table-cell header=True %}Buffer/Volume{% /table-cell %}
30+
{% #table-cell header=True %}Qty{% /table-cell %}
31+
{% endfragment %}
32+
33+
{% #table headers=table_header %}
34+
{% for oq in object.equipments.all %}
35+
<tr>
36+
{% #table-cell %}{{ oq.equipment.name }}{% /table-cell %}
37+
{% #table-cell %}{{ oq.buffer.name }} {{ oq.buffer_quantity}} {{ oq.buffer.unit }}{% /table-cell %}
38+
{% #table-cell %}{{ oq.quantity }}{% /table-cell %}
39+
</tr>
40+
{% empty %}
41+
<tr>
42+
<td colspan="3" class="text-center font-bold">No Equipment requested</td>
43+
</tr>
44+
{% endfor %}
45+
{% /table %}
46+
{% endif %}
47+
48+
{% endblock %}

src/nina/tasks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def send_email_async(
99
message: str,
1010
from_email: str | None,
1111
recipient_list: list[str],
12+
html_message: str | None = None,
1213
) -> None:
1314
"""
1415
Send an email asynchronously using the task queue.
@@ -26,4 +27,5 @@ def send_email_async(
2627
from_email,
2728
recipient_list,
2829
fail_silently=settings.EMAIL_FAIL_SILENTLY,
30+
html_message=html_message,
2931
)

0 commit comments

Comments
 (0)