Properly encode Email notification headers (Fix #137)
This commit is contained in:
@@ -31,14 +31,17 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.mail.Header;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import javax.mail.internet.MimeUtility;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class EmailSmtpConnector implements EmailConnector {
|
public class EmailSmtpConnector implements EmailConnector {
|
||||||
@@ -97,7 +100,16 @@ public class EmailSmtpConnector implements EmailConnector {
|
|||||||
try {
|
try {
|
||||||
InternetAddress sender = new InternetAddress(senderAddress, senderName);
|
InternetAddress sender = new InternetAddress(senderAddress, senderName);
|
||||||
MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
|
MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
|
||||||
msg.setHeader("X-Mailer", Mxisd.Agent);
|
|
||||||
|
// We must encode our headers ourselves as we have no guarantee that they were in the provided data.
|
||||||
|
// This is required to support UTF-8 characters from user display names or room names in the subject header per example
|
||||||
|
Enumeration<Header> headers = msg.getAllHeaders();
|
||||||
|
while (headers.hasMoreElements()) {
|
||||||
|
Header header = headers.nextElement();
|
||||||
|
msg.setHeader(header.getName(), MimeUtility.encodeText(header.getValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.setHeader("X-Mailer", MimeUtility.encodeText(Mxisd.Agent));
|
||||||
msg.setSentDate(new Date());
|
msg.setSentDate(new Date());
|
||||||
msg.setFrom(sender);
|
msg.setFrom(sender);
|
||||||
msg.setRecipients(Message.RecipientType.TO, recipient);
|
msg.setRecipients(Message.RecipientType.TO, recipient);
|
||||||
|
@@ -60,7 +60,8 @@ public class EmailNotificationTest {
|
|||||||
private final String user = "mxisd";
|
private final String user = "mxisd";
|
||||||
private final String notifiee = "john";
|
private final String notifiee = "john";
|
||||||
private final String sender = user + "@" + domain;
|
private final String sender = user + "@" + domain;
|
||||||
private final String senderEmail = "\"Mxisd Server (Unit Test)\" <" + sender + ">";
|
private final String senderName = "\"Mxisd Server あ (Unit Test)\" <" + sender + ">";
|
||||||
|
private final String senderNameEncoded = "=?UTF-8?Q?=22Mxisd_Server_=E3=81=82_=28Unit_T?= =?UTF-8?Q?est=29=22_=3Cmxisd=40localhost=3E?= <mxisd@localhost>";
|
||||||
private final String target = notifiee + "@" + domain;
|
private final String target = notifiee + "@" + domain;
|
||||||
|
|
||||||
private Mxisd m;
|
private Mxisd m;
|
||||||
@@ -76,7 +77,7 @@ public class EmailNotificationTest {
|
|||||||
EmailConfig eCfg = new EmailConfig();
|
EmailConfig eCfg = new EmailConfig();
|
||||||
eCfg.setConnector(EmailSmtpConnector.ID);
|
eCfg.setConnector(EmailSmtpConnector.ID);
|
||||||
eCfg.getIdentity().setFrom(sender);
|
eCfg.getIdentity().setFrom(sender);
|
||||||
eCfg.getIdentity().setName("Mxisd Server (Unit Test)");
|
eCfg.getIdentity().setName(senderName);
|
||||||
eCfg.getConnectors().put(EmailSmtpConnector.ID, GsonUtil.makeObj(smtpCfg));
|
eCfg.getConnectors().put(EmailSmtpConnector.ID, GsonUtil.makeObj(smtpCfg));
|
||||||
|
|
||||||
MxisdConfig cfg = new MxisdConfig();
|
MxisdConfig cfg = new MxisdConfig();
|
||||||
@@ -118,7 +119,7 @@ public class EmailNotificationTest {
|
|||||||
assertEquals(1, gm.getReceivedMessages().length);
|
assertEquals(1, gm.getReceivedMessages().length);
|
||||||
MimeMessage msg = gm.getReceivedMessages()[0];
|
MimeMessage msg = gm.getReceivedMessages()[0];
|
||||||
assertEquals(1, msg.getFrom().length);
|
assertEquals(1, msg.getFrom().length);
|
||||||
assertEquals(senderEmail, msg.getFrom()[0].toString());
|
assertEquals(senderNameEncoded, msg.getFrom()[0].toString());
|
||||||
assertEquals(1, msg.getRecipients(Message.RecipientType.TO).length);
|
assertEquals(1, msg.getRecipients(Message.RecipientType.TO).length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +136,7 @@ public class EmailNotificationTest {
|
|||||||
assertEquals(1, gm.getReceivedMessages().length);
|
assertEquals(1, gm.getReceivedMessages().length);
|
||||||
MimeMessage msg = gm.getReceivedMessages()[0];
|
MimeMessage msg = gm.getReceivedMessages()[0];
|
||||||
assertEquals(1, msg.getFrom().length);
|
assertEquals(1, msg.getFrom().length);
|
||||||
assertEquals(senderEmail, msg.getFrom()[0].toString());
|
assertEquals(senderNameEncoded, msg.getFrom()[0].toString());
|
||||||
assertEquals(1, msg.getRecipients(Message.RecipientType.TO).length);
|
assertEquals(1, msg.getRecipients(Message.RecipientType.TO).length);
|
||||||
|
|
||||||
// We just check on the text/plain one. HTML is multipart and it's difficult so we skip
|
// We just check on the text/plain one. HTML is multipart and it's difficult so we skip
|
||||||
@@ -165,7 +166,7 @@ public class EmailNotificationTest {
|
|||||||
assertEquals(1, gm.getReceivedMessages().length);
|
assertEquals(1, gm.getReceivedMessages().length);
|
||||||
MimeMessage msg = gm.getReceivedMessages()[0];
|
MimeMessage msg = gm.getReceivedMessages()[0];
|
||||||
assertEquals(1, msg.getFrom().length);
|
assertEquals(1, msg.getFrom().length);
|
||||||
assertEquals(senderEmail, msg.getFrom()[0].toString());
|
assertEquals(senderNameEncoded, msg.getFrom()[0].toString());
|
||||||
assertEquals(1, msg.getRecipients(Message.RecipientType.TO).length);
|
assertEquals(1, msg.getRecipients(Message.RecipientType.TO).length);
|
||||||
|
|
||||||
// We just check on the text/plain one. HTML is multipart and it's difficult so we skip
|
// We just check on the text/plain one. HTML is multipart and it's difficult so we skip
|
||||||
|
Reference in New Issue
Block a user