Skip to content

Commit

Permalink
refactor: optimize email content templates
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Sep 21, 2023
1 parent 2ea0612 commit 43985da
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package run.halo.app.notification;

import static org.apache.commons.lang3.BooleanUtils.isNotTrue;

import com.fasterxml.jackson.databind.JsonNode;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
Expand Down Expand Up @@ -33,6 +35,7 @@
public class EmailNotifier implements ReactiveNotifier {

private final SubscriberEmailResolver subscriberEmailResolver;
private final NotificationTemplateRender notificationTemplateRender;
private final AtomicReference<Pair<EmailSenderConfig, JavaMailSenderImpl>>
emailSenderConfigPairRef = new AtomicReference<>();

Expand All @@ -47,21 +50,47 @@ public Mono<Void> notify(NotificationContext context) {
String recipient = context.getMessage().getRecipient();
var subscriber = new Subscription.Subscriber();
subscriber.setName(recipient);
var payload = context.getMessage().getPayload();
return subscriberEmailResolver.resolve(subscriber)
.map(toEmail -> (MimeMessagePreparator) mimeMessage -> {
MimeMessageHelper helper =
new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.name());
helper.setFrom(emailSenderConfig.getUsername(), emailSenderConfig.getDisplayName());
var payload = context.getMessage().getPayload();
helper.setSubject(payload.getTitle());
helper.setText(payload.getRawBody(), payload.getHtmlBody());
helper.setTo(toEmail);
.flatMap(toEmail -> {
if (isNotTrue(emailSenderConfig.getAppendFooter())) {
return Mono.just(toEmail);
}
var htmlMono = appendHtmlBodyFooter(payload.getAttributes())
.doOnNext(footer -> {
if (StringUtils.isNotBlank(payload.getRawBody())) {
payload.setHtmlBody(payload.getHtmlBody() + footer);
}
});
var rawMono = appendRawBodyFooter(payload.getAttributes())
.doOnNext(footer -> {
if (StringUtils.isNotBlank(payload.getHtmlBody())) {
payload.setRawBody(payload.getRawBody() + footer);
}
});
return Mono.when(htmlMono, rawMono)
.thenReturn(toEmail);
})
.map(toEmail -> getMimeMessagePreparator(toEmail, emailSenderConfig, payload))
.publishOn(Schedulers.boundedElastic())
.doOnNext(javaMailSender::send)
.then();
}

@NonNull
private static MimeMessagePreparator getMimeMessagePreparator(String toEmail,
EmailSenderConfig emailSenderConfig, NotificationContext.MessagePayload payload) {
return mimeMessage -> {
MimeMessageHelper helper =
new MimeMessageHelper(mimeMessage, true, StandardCharsets.UTF_8.name());
helper.setFrom(emailSenderConfig.getUsername(), emailSenderConfig.getDisplayName());

helper.setSubject(payload.getTitle());
helper.setText(payload.getRawBody(), payload.getHtmlBody());
helper.setTo(toEmail);
};
}

@NonNull
private static JavaMailSenderImpl createJavaMailSender(EmailSenderConfig emailSenderConfig) {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
Expand Down Expand Up @@ -89,13 +118,37 @@ JavaMailSenderImpl getJavaMailSender(EmailSenderConfig emailSenderConfig) {
}).getSecond();
}

Mono<String> appendRawBodyFooter(ReasonAttributes attributes) {
return notificationTemplateRender.render("""
---
如果您不想再收到此类通知,点击链接退订: [(${unsubscribeUrl})]
[(${site.title})]
""", attributes);
}

Mono<String> appendHtmlBodyFooter(ReasonAttributes attributes) {
return notificationTemplateRender.render("""
---
<div class="footer" style="font-size: 12px; color: #666">
<a th:href="${site.url}" th:text="${site.title}"></a>
<p class="unsubscribe">
&mdash;<br />请勿直接回复此回邮件,
<a th:href="|${site.url}/console|">查看通知</a>
<a th:href="${unsubscribeUrl}">取消订阅</a>。
</p>
</div>
""", attributes);
}

@Data
static class EmailSenderConfig {
private String displayName;
private String username;
private String password;
private String host;
private Integer port;
private Boolean appendFooter;

/**
* Gets email display name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ spec:
[(${commenter})] 评论了您的文章 《[(${postTitle})]》,以下是评论的具体内容:
[(${content})]
---
如果您不想再收到此类通知,点击链接退订: [(${unsubscribeUrl})]
[(${site.title})]
htmlBody: |
<div class="head">
<p class="honorific" th:text="|${subscriber.displayName} 你好:|"></p>
Expand All @@ -29,15 +24,6 @@ spec:
<p class="content" th:text="${content}"></p>
</div>
<div>
<a th:href="${site.url}" th:text="${site.title}"></a>
</div>
<div class="footer" style="font-size: 12px; color: #666">
<p class="unsubscribe">
&mdash;<br />请勿直接回复此回邮件,
<a th:href="|${site.url}/console|">查看通知</a>
<a th:href="${unsubscribeUrl}">取消订阅</a>。
</p>
</div>
---
apiVersion: notification.halo.run/v1alpha1
Expand All @@ -56,11 +42,6 @@ spec:
[(${commenter})] 评论了您的页面 《[(${pageTitle})]》,以下是评论的具体内容:
[(${content})]
---
如果您不想再收到此类通知,点击链接退订: [(${unsubscribeUrl})]
[(${site.title})]
htmlBody: |
<div class="head">
<p class="honorific" th:text="|${subscriber.displayName} 你好:|"></p>
Expand All @@ -71,15 +52,6 @@ spec:
<p class="content" th:text="${content}"></p>
</div>
<div>
<a th:href="${site.url}" th:text="${site.title}"></a>
</div>
<div class="footer" style="font-size: 12px; color: #666">
<p class="unsubscribe">
&mdash;<br />请勿直接回复此回邮件,
<a th:href="|${site.url}/console|">查看通知</a>
<a th:href="${unsubscribeUrl}">取消订阅</a>。
</p>
</div>
---
apiVersion: notification.halo.run/v1alpha1
Expand All @@ -98,11 +70,6 @@ spec:
[(${replier})] 在评论“[(${isQuoteReply ? quoteContent : commentContent})]”中回复了您,以下是回复的具体内容:
[(${content})]
---
如果您不想再收到此类通知,点击链接退订: [(${unsubscribeUrl})]
[(${site.title})]
htmlBody: |
<div class="head">
<p class="honorific" th:text="|${subscriber.displayName} 你好:|"></p>
Expand All @@ -114,13 +81,4 @@ spec:
<p class="content" th:text="${content}"></p>
</div>
<div>
<a th:href="${site.url}" th:text="${site.title}"></a>
</div>
<div class="footer" style="font-size: 12px; color: #666">
<p class="unsubscribe">
&mdash;<br />请勿直接回复此回邮件,
<a th:href="|${site.url}/console|">查看通知</a>
<a th:href="${unsubscribeUrl}">取消订阅</a>。
</p>
</div>
4 changes: 4 additions & 0 deletions application/src/main/resources/extensions/notification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ spec:
label: "端口号"
name: port
validation: required
- $formkit: appendFooter
label: "邮件内容追加页脚"
name: appendFooter
value: true
---
apiVersion: notification.halo.run/v1alpha1
kind: ReasonType
Expand Down

0 comments on commit 43985da

Please sign in to comment.