Merge pull request #9 from getrebuild/feature-RB-98

Mail notify template
This commit is contained in:
Fangfang Zhao 2019-03-13 16:26:15 +08:00 committed by GitHub
commit 0a96365c1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 17 deletions

View file

@ -211,6 +211,11 @@
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<!--
<dependency>
<groupId>org.springframework.data</groupId>

View file

@ -19,11 +19,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package com.rebuild.server.helper;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
@ -42,22 +46,10 @@ public class SMSender {
private static final Log LOG = LogFactory.getLog(SMSender.class);
/**
* @param to
* @param htmlTemplate
* @return
* @see #sendMail(String, String, String, boolean)
*/
public static String sendMail(String to, File htmlTemplate) {
throw new UnsupportedOperationException();
}
/**
*
* @param to
* @param subject
* @param content
* @return
* @see #sendMail(String, String, String, boolean)
*/
public static String sendMail(String to, String subject, String content) {
return sendMail(to, subject, content, true);
@ -67,9 +59,10 @@ public class SMSender {
* @param to
* @param subject
* @param content
* @param useTemplate
* @return <tt>null</tt> if failed or SENDID
*/
public static String sendMail(String to, String subject, String content, boolean isHtml) {
public static String sendMail(String to, String subject, String content, boolean useTemplate) {
String account[] = SystemConfig.getMailAccount();
if (account == null) {
LOG.error("Mail send failed : " + to + " > " + subject + "\nError : No account set");
@ -83,8 +76,18 @@ public class SMSender {
params.put("from", account[2]);
params.put("from_name", account[3]);
params.put("subject", subject);
if (isHtml) {
params.put("html", content);
if (useTemplate) {
Element mailbody = null;
try {
mailbody = getMailTemplate();
} catch (IOException e) {
LOG.error("Cloud't load mail template", e);
return null;
}
mailbody.selectFirst(".rb-title").text(subject);
mailbody.selectFirst(".rb-content").html(content);
params.put("html", mailbody.html());
} else {
params.put("text", content);
}
@ -110,6 +113,16 @@ public class SMSender {
return null;
}
/**
* @return
* @throws IOException
*/
protected static Element getMailTemplate() throws IOException {
File temp = SystemConfig.getFileOfRes("locales/mail-notify.html");
Document html = Jsoup.parse(temp, "utf-8");
return html.body();
}
/**
* @param to
* @param content

View file

@ -19,6 +19,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package com.rebuild.server.helper;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@ -47,7 +49,7 @@ public class SystemConfig {
private static final Log LOG = LogFactory.getLog(SystemConfig.class);
/**
* 临时目录/文件
* 获取临时文件或目录
*
* @param file
* @return
@ -67,6 +69,22 @@ public class SystemConfig {
return new File(tmpFile, file);
}
/**
* 获取配置文件
*
* @param file
* @return
*/
public static File getFileOfRes(String file) {
URL fileUrl = SystemConfig.class.getClassLoader().getResource(file);
try {
File resFile = new File(fileUrl.toURI());
return resFile;
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Bad file path or name : " + file);
}
}
/**
* 云存储地址
*
@ -123,7 +141,7 @@ public class SystemConfig {
* @param items
* @return
*/
static String[] getsNoUnset(ConfigItem... items) {
private static String[] getsNoUnset(ConfigItem... items) {
List<String> list = new ArrayList<>();
for (ConfigItem item : items) {
String v = get(item);

View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mail Notification Template</title>
</head>
<body>
<div style="background-color: #f5f5f5; margin: 0; font-size: 0.9rem; padding: 20px 30px; color: #333; font-family: Roboto, Helvetica, 'Microsoft YaHei', '宋体', sans-serif; line-height: 1.5">
<div>
<a href="https://getrebuild.com/"><img src="https://getrebuild.com/img/logo.png" alt="REBUILD" style="height: 24px; display: inline-block"></a>
</div>
<div style="border: 1px solid #eee; padding: 30px 20px; background-color: #fff; border-radius: 3px; border-top: 3px solid #4285f4; max-width: 720px; margin: 10px 0">
<h2 class="rb-title" style="font-size: 1.4rem; margin: 0; font-weight: 400">%TITLE%</h2>
<div class="rb-content" style="margin-top: 10px">%CONTETN%</div>
</div>
<div>
<div class="rb-footer" style="margin: 0; color: #888; font-size: 13px;">这条信息是由 REBUILD 系统发送的,请不要直接回复。</div>
</div>
</div>
</body>
</html>