Skip to content

Commit 37d6178

Browse files
committed
邮件模板
1 parent 07c7316 commit 37d6178

File tree

4 files changed

+98
-42
lines changed

4 files changed

+98
-42
lines changed

blog-api/src/main/java/com/yoyling/controller/CommentController.java

Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,22 @@ public class CommentController {
4545
MailProperties mailProperties;
4646
@Autowired
4747
MailUtils mailUtils;
48+
49+
private static String blogName;
50+
private static String cmsUrl;
4851
private static String websiteUrl;
4952

50-
@Value("${server.website.url}")
53+
@Value("${custom.mail.blog.name}")
54+
public void setBlogName(String blogName) {
55+
this.blogName = blogName;
56+
}
57+
58+
@Value("${custom.mail.url.cms}")
59+
public void setCmsUrl(String cmsUrl) {
60+
this.cmsUrl = cmsUrl;
61+
}
62+
63+
@Value("${custom.mail.url.website}")
5164
public void setWebsiteUrl(String websiteUrl) {
5265
this.websiteUrl = websiteUrl;
5366
}
@@ -246,18 +259,7 @@ public Result postComment(@RequestBody Comment comment,
246259
}
247260
}
248261
commentService.saveComment(comment);
249-
String path = "";
250-
if (comment.getPage() == 0) {
251-
//普通博客
252-
path = "/blog/" + comment.getBlogId();
253-
} else if (comment.getPage() == 1) {
254-
//关于我页面
255-
path = "/about";
256-
} else if (comment.getPage() == 2) {
257-
//友链页面
258-
path = "/friends";
259-
}
260-
judgeSendMail(comment, isVisitorComment, path);
262+
judgeSendMail(comment, isVisitorComment);
261263
return Result.ok("评论成功");
262264
}
263265

@@ -329,40 +331,42 @@ private void setCommentRandomAvatar(Comment comment) {
329331
comment.setAvatar(avatar);
330332
}
331333

332-
private void judgeSendMail(Comment comment, boolean isVisitorComment, String path) {
333-
//6种情况:
334-
//我以父评论提交:不用邮件提醒
335-
//我回复我自己:不用邮件提醒
336-
//我回复访客的评论:只提醒该访客
337-
//访客以父评论提交:只提醒我自己
338-
//访客回复我的评论:只提醒我自己
339-
//访客回复访客的评论(即使是他自己先前的评论):提醒我自己和他回复的评论
334+
private void judgeSendMail(Comment comment, boolean isVisitorComment) {
335+
/*
336+
6种情况:
337+
我以父评论提交:不用邮件提醒
338+
我回复我自己:不用邮件提醒
339+
我回复访客的评论:只提醒该访客
340+
访客以父评论提交:只提醒我自己
341+
访客回复我的评论:只提醒我自己
342+
访客回复访客的评论(即使是他自己先前的评论):提醒我自己和他回复的评论
343+
*/
340344
if (isVisitorComment) {
341345
//访客的评论
342346
if (comment.getParentCommentId() != -1) {
343347
com.yoyling.entity.Comment parentComment = commentService.getCommentById(comment.getParentCommentId());
344348
//访客回复我的评论,邮件提醒我自己
345349
if (parentComment.getAdminComment()) {
346-
sendMailToMe(parentComment.getEmail(), path);
350+
sendMailToMe(comment);
347351
} else {
348352
if (parentComment.getNotice()) {
349353
//访客回复访客的评论(即使是他自己先前的评论),且对方接收提醒,邮件提醒对方
350-
sendMailToParentComment(parentComment.getEmail(), path);
354+
sendMailToParentComment(parentComment, comment);
351355
}
352356
//不管对方是否接收提醒,都要提醒我有新评论
353-
sendMailToMe(mailProperties.getUsername(), path);
357+
sendMailToMe(comment);
354358
}
355359
} else {
356360
//访客以父评论提交,只邮件提醒我自己
357-
sendMailToMe(mailProperties.getUsername(), path);
361+
sendMailToMe(comment);
358362
}
359363
} else {
360364
//我的评论
361365
if (comment.getParentCommentId() != -1) {
362366
com.yoyling.entity.Comment parentComment = commentService.getCommentById(comment.getParentCommentId());
363367
//我回复访客的评论,且对方接收提醒,邮件提醒对方
364368
if (!parentComment.getAdminComment() && parentComment.getNotice()) {
365-
sendMailToParentComment(parentComment.getEmail(), path);
369+
sendMailToParentComment(parentComment, comment);
366370
}
367371
}
368372
}
@@ -371,28 +375,71 @@ private void judgeSendMail(Comment comment, boolean isVisitorComment, String pat
371375
/**
372376
* 发送邮件提醒回复对象
373377
*
374-
* @param email 邮件接收方
375-
* @param path 评论所在的页面
378+
* @param parentComment 父评论
379+
* @param comment 当前评论
376380
*/
377-
private void sendMailToParentComment(String email, String path) {
378-
String url = websiteUrl + path;
379-
String toAccount = email;
380-
String subject = "YOYLING · BLOG 评论回复";
381-
String content = "<body><h2>您的评论有新回复</h2><p><a href='" + url + "'>详情请看" + url + "</a></p><p>此邮件为自动发送,如不想再收到此类消息,请回复TD</p></body>";
382-
mailUtils.sendHTMLMail(toAccount, subject, content);
381+
private void sendMailToParentComment(top.naccl.entity.Comment parentComment, Comment comment) {
382+
String path = "";
383+
String title = "";
384+
if (comment.getPage() == 0) {
385+
//普通博客
386+
title = parentComment.getBlog().getTitle();
387+
path = "/blog/" + comment.getBlogId();
388+
} else if (comment.getPage() == 1) {
389+
//关于我页面
390+
title = "关于我";
391+
path = "/about";
392+
} else if (comment.getPage() == 2) {
393+
//友链页面
394+
title = "友人帐";
395+
path = "/friends";
396+
}
397+
Map<String, Object> map = new HashMap<>();
398+
map.put("parentNickname", parentComment.getNickname());
399+
map.put("nickname", comment.getNickname());
400+
map.put("title", title);
401+
map.put("time", comment.getCreateTime());
402+
map.put("parentContent", parentComment.getContent());
403+
map.put("content", comment.getContent());
404+
map.put("url", websiteUrl + path);
405+
String toAccount = parentComment.getEmail();
406+
String subject = "您在 " + blogName + " 的评论有了新回复";
407+
mailUtils.sendHtmlTemplateMail(map, toAccount, subject, "guest.html");
383408
}
384409

385410
/**
386411
* 发送邮件提醒我自己
387412
*
388-
* @param email 邮件接收方
389-
* @param path 评论所在的页面
413+
* @param comment 当前评论
390414
*/
391-
private void sendMailToMe(String email, String path) {
392-
String url = websiteUrl + path;
393-
String toAccount = email;
394-
String subject = "YOYLING · BLOG 新评论";
395-
String content = "<body><p><a href='" + url + "'>新评论" + url + "</a></p></body>";
396-
mailUtils.sendHTMLMail(toAccount, subject, content);
415+
private void sendMailToMe(Comment comment) {
416+
String path = "";
417+
String title = "";
418+
if (comment.getPage() == 0) {
419+
//普通博客
420+
title = blogService.getTitleByBlogId(comment.getBlogId());
421+
path = "/blog/" + comment.getBlogId();
422+
} else if (comment.getPage() == 1) {
423+
//关于我页面
424+
title = "关于我";
425+
path = "/about";
426+
} else if (comment.getPage() == 2) {
427+
//友链页面
428+
title = "友人帐";
429+
path = "/friends";
430+
}
431+
Map<String, Object> map = new HashMap<>();
432+
map.put("title", title);
433+
map.put("time", comment.getCreateTime());
434+
map.put("nickname", comment.getNickname());
435+
map.put("content", comment.getContent());
436+
map.put("ip", comment.getIp());
437+
map.put("email", comment.getEmail());
438+
map.put("status", comment.getPublished() ? "公开" : "待审核");
439+
map.put("url", websiteUrl + path);
440+
map.put("manageUrl", cmsUrl + "/comments");
441+
String toAccount = mailProperties.getUsername();
442+
String subject = blogName + " 收到新评论";
443+
mailUtils.sendHtmlTemplateMail(map, toAccount, subject, "owner.html");
397444
}
398445
}

blog-api/src/main/java/com/yoyling/mapper/BlogMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public interface BlogMapper {
6262

6363
Blog getBlogById(Long id);
6464

65+
String getTitleByBlogId(Long id);
66+
6567
BlogDetail getBlogByIdAndIsPublished(Long id);
6668

6769
String getBlogPassword(Long blogId);

blog-api/src/main/java/com/yoyling/service/BlogService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public interface BlogService {
5151

5252
Blog getBlogById(Long id);
5353

54+
String getTitleByBlogId(Long id);
55+
5456
BlogDetail getBlogByIdAndIsPublished(Long id);
5557

5658
String getBlogPassword(Long blogId);

blog-api/src/main/java/com/yoyling/service/impl/BlogServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ public Blog getBlogById(Long id) {
324324
return blog;
325325
}
326326

327+
@Override
328+
public String getTitleByBlogId(Long id) {
329+
return blogMapper.getTitleByBlogId(id);
330+
}
331+
327332
@Override
328333
public BlogDetail getBlogByIdAndIsPublished(Long id) {
329334
BlogDetail blog = blogMapper.getBlogByIdAndIsPublished(id);

0 commit comments

Comments
 (0)