diff --git a/src/main/java/com/postmarkapp/postmark/client/data/model/message/BaseMessage.java b/src/main/java/com/postmarkapp/postmark/client/data/model/message/BaseMessage.java index 711cfc2..3feee71 100644 --- a/src/main/java/com/postmarkapp/postmark/client/data/model/message/BaseMessage.java +++ b/src/main/java/com/postmarkapp/postmark/client/data/model/message/BaseMessage.java @@ -367,6 +367,10 @@ private String convertRecipients(List recipients) { private String convertRecipient(String name, String address) { StringBuilder recipientsString = new StringBuilder(); + if (name != null) { + // Escape characters that otherwise cause parsing errors in the API + name = name.replace("\"", "\\\""); + } return recipientsString.append("\"").append(name).append("\"") .append("<").append(address).append(">").toString(); } diff --git a/src/test/java/unit/data/MessageTest.java b/src/test/java/unit/data/MessageTest.java index 82b716e..87542e2 100644 --- a/src/test/java/unit/data/MessageTest.java +++ b/src/test/java/unit/data/MessageTest.java @@ -183,6 +183,28 @@ void simpleMessageBcccRecipientsFullName() throws IOException { } + @Test + void recipientsWithQuotesAreEscapedProperly() { + Message message = new Message(); + String nameWithQuote = "John \"Smith\""; + String email = "john@example.com"; + + Map recipients = new HashMap<>(); + recipients.put(nameWithQuote, email); + + message.setTo(recipients); + assertEquals("\"John \\\"Smith\\\"\"", message.getTo()); + + message.setCc(recipients); + assertEquals("\"John \\\"Smith\\\"\"", message.getCc()); + + message.setBcc(recipients); + assertEquals("\"John \\\"Smith\\\"\"", message.getBcc()); + + message.setFrom(nameWithQuote, email); + assertEquals("\"John \\\"Smith\\\"\"", message.getFrom()); + } + @Test void attachmentException() throws IOException { Message message = new Message();