Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent API parse error when names contain quotes #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ private String convertRecipients(List<String> 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();
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/unit/data/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,28 @@ void simpleMessageBcccRecipientsFullName() throws IOException {

}

@Test
void recipientsWithQuotesAreEscapedProperly() {
Message message = new Message();
String nameWithQuote = "John \"Smith\"";
String email = "[email protected]";

Map<String, String> recipients = new HashMap<>();
recipients.put(nameWithQuote, email);

message.setTo(recipients);
assertEquals("\"John \\\"Smith\\\"\"<[email protected]>", message.getTo());

message.setCc(recipients);
assertEquals("\"John \\\"Smith\\\"\"<[email protected]>", message.getCc());

message.setBcc(recipients);
assertEquals("\"John \\\"Smith\\\"\"<[email protected]>", message.getBcc());

message.setFrom(nameWithQuote, email);
assertEquals("\"John \\\"Smith\\\"\"<[email protected]>", message.getFrom());
}

@Test
void attachmentException() throws IOException {
Message message = new Message();
Expand Down