aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/EmailManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/main/java/com/juick/server/EmailManager.java')
-rw-r--r--juick-server/src/main/java/com/juick/server/EmailManager.java70
1 files changed, 43 insertions, 27 deletions
diff --git a/juick-server/src/main/java/com/juick/server/EmailManager.java b/juick-server/src/main/java/com/juick/server/EmailManager.java
index 66e84358..6e8d0d76 100644
--- a/juick-server/src/main/java/com/juick/server/EmailManager.java
+++ b/juick-server/src/main/java/com/juick/server/EmailManager.java
@@ -8,6 +8,7 @@ import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.UserService;
import com.juick.util.MessageUtils;
+import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
@@ -23,7 +24,9 @@ import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import static com.juick.formatters.PlainTextFormatter.formatPost;
@@ -68,6 +71,31 @@ public class EmailManager implements ApplicationListener<MessageEvent> {
}
private void emailNotify(String email, String subject, Message msg) {
+ Map<String, String> headers = new HashMap<>();
+ if (!MessageUtils.isPM(msg)) {
+ headers.put("Message-ID", String.format("<%d.%d@juick.com>", msg.getMid(), msg.getRid()));
+ }
+ if (MessageUtils.isReply(msg)) {
+ if (msg.getReplyto() > 0) {
+ Message replyto = messagesService.getReply(msg.getMid(), msg.getReplyto());
+ headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", replyto.getMid(), replyto.getRid()));
+ } else {
+ Message original = messagesService.getMessage(msg.getMid());
+ headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid()));
+ }
+ }
+ String plainText = String.format("%s\n\n--\nYou are receiving this because you are subscribed to this user " +
+ ", discussion or tag. Reply to this email directly or view it on Juick: %s.",
+ formatPost(msg), formatUrl(msg));
+ String hash = userService.getHashByUID(userService.getUserByEmail(email).getUid());
+ String htmlText = String.format("%s<br /><br />--<br />You are receiving this because you are subscribed to this user" +
+ ", discussion or tag. Reply to this email directly or <a href=\"%s\"><img src=\"https://api.juick.com/thread/mark_read/%d-%d.gif?hash=%s\" />view it</a> on Juick." +
+ "<br /><a href=\"https://juick.com/settings?hash=%s\">Configure or disable notifications</a>",
+ MessageUtils.formatHtml(msg), formatUrl(msg),
+ msg.getMid(), msg.getRid(), hash, hash);
+ sendEmail(email, subject, plainText, htmlText, headers);
+ }
+ public void sendEmail(String to, String subject, String textPart, String htmlPart, Map<String, String> messageHeaders) {
Properties prop = System.getProperties();
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(prop);
@@ -75,42 +103,30 @@ public class EmailManager implements ApplicationListener<MessageEvent> {
Transport transport = session.getTransport("smtp");
MimeMessage message = new MimeMessage(session) {
protected void updateMessageID() throws MessagingException {
- if (!MessageUtils.isPM(msg)) {
- setHeader("Message-ID", String.format("<%d.%d@juick.com>", msg.getMid(), msg.getRid()));
- }
- if (MessageUtils.isReply(msg)) {
- if (msg.getReplyto() > 0) {
- Message replyto = messagesService.getReply(msg.getMid(), msg.getReplyto());
- setHeader("In-Reply-To", String.format("<%d.%d@juick.com>", replyto.getMid(), replyto.getRid()));
- } else {
- Message original = messagesService.getMessage(msg.getMid());
- setHeader("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid()));
- }
+ for (Map.Entry<String, String> entry: messageHeaders.entrySet()) {
+ setHeader(entry.getKey(), entry.getValue());
}
}
};
message.setFrom(new InternetAddress("juick@juick.com"));
- message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email));
+ message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
- String plainText = String.format("%s\n\n--\nYou are receiving this because you are subscribed to this user " +
- ", discussion or tag. Reply to this email directly or view it on Juick: %s.",
- formatPost(msg), formatUrl(msg));
MimeBodyPart textBodyPart = new MimeBodyPart();
- String hash = userService.getHashByUID(userService.getUserByEmail(email).getUid());
- textBodyPart.setContent(plainText, "text/plain; charset=UTF-8");
- String htmlText = String.format("%s<br /><br />--<br />You are receiving this because you are subscribed to this user" +
- ", discussion or tag. Reply to this email directly or <a href=\"%s\"><img src=\"https://api.juick.com/thread/mark_read/%d-%d.gif?hash=%s\" />view it</a> on Juick." +
- "<br /><a href=\"https://juick.com/settings?hash=%s\">Configure or disable notifications</a>",
- MessageUtils.formatHtml(msg), formatUrl(msg),
- msg.getMid(), msg.getRid(), hash, hash);
- MimeBodyPart htmlBodyPart = new MimeBodyPart();
- htmlBodyPart.setContent(htmlText, "text/html; charset=UTF-8");
+ textBodyPart.setContent(textPart, "text/plain; charset=UTF-8");
+
Multipart multipart = new MimeMultipart("alternative");
multipart.addBodyPart(textBodyPart);
- multipart.addBodyPart(htmlBodyPart);
+ if (StringUtils.isNotBlank(htmlPart)) {
+ MimeBodyPart htmlBodyPart = new MimeBodyPart();
+ htmlBodyPart.setContent(htmlPart, "text/html; charset=UTF-8");
+ multipart.addBodyPart(htmlBodyPart);
+ }
message.setContent(multipart);
- message.setHeader("List-Unsubscribe", String.format("https://juick.com/settings?hash=%s",
- userService.getHashByUID(userService.getUserByEmail(email).getUid())));
+ User emailUser = userService.getUserByEmail(to);
+ if (!emailUser.isAnonymous()) {
+ message.setHeader("List-Unsubscribe", String.format("https://juick.com/settings?hash=%s",
+ userService.getHashByUID(emailUser.getUid())));
+ }
message.saveChanges();
transport.connect();
transport.sendMessage(message, message.getAllRecipients());