Skip to content

Commit

Permalink
change email address extraction behaviour to handle address like 'Foo…
Browse files Browse the repository at this point in the history
… Bar <foobar@example.com>' #17
  • Loading branch information
aepshteyn authored Feb 20, 2020
1 parent b47f7d3 commit 0313a20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static boolean isValidEmailAddress(String address) {
public static String extractEmailAddress(String args, int offset) {
String address = args.substring(offset).trim();
if (address.indexOf('<') == 0) {
address = address.substring(1, address.indexOf('>'));
address = address.substring(1, address.lastIndexOf('>'));
// spaces within the <> are also possible, Postfix apparently
// trims these away:
return address.trim();
Expand Down
28 changes: 20 additions & 8 deletions src/test/java/org/subethamail/smtp/util/EmailUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.subethamail.smtp.util;

import static org.junit.Assert.assertEquals;

import com.github.davidmoten.junit.Asserts;
import org.junit.Assert;
import org.junit.Test;
import org.subethamail.smtp.internal.util.EmailUtils;

import com.github.davidmoten.junit.Asserts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class EmailUtilsTest {

Expand All @@ -28,25 +28,37 @@ public void testBlankAddressIsValid() {
@Test
public void testExtract() {
assertEquals("anyone2@anywhere.com",
EmailUtils.extractEmailAddress("TO:<anyone2@anywhere.com>", 3));
extractAndValidate("TO:<anyone2@anywhere.com>", 3));
}

@Test
public void testExtractWithNoLessThanSymbolAtStartOfEmailAndPrecedingSpace() {
assertEquals("test@example.com",
EmailUtils.extractEmailAddress("FROM: test@example.com", 5));
extractAndValidate("FROM: test@example.com", 5));
}

@Test
public void testExtractWithNoLessThanSymbolAtStartOfEmailAndNoPrecedingSpace() {
assertEquals("test@example.com",
EmailUtils.extractEmailAddress("FROM:test@example.com", 5));
extractAndValidate("FROM:test@example.com", 5));
}


@Test
public void testExtractWithNoLessThanSymbolAtStartOfEmailAndSIZECommand() {
assertEquals("test@example.com",
EmailUtils.extractEmailAddress("FROM:test@example.com SIZE=1000", 5));
extractAndValidate("FROM:test@example.com SIZE=1000", 5));
}

@Test
public void testExtractWithEmbeddedPersonalName() {
// see https://github.com/davidmoten/subethasmtp/issues/17
assertEquals("Foo Bar <foobar@example.com>",
extractAndValidate("FROM:<Foo Bar <foobar@example.com>>", 5));
}

private static String extractAndValidate(String args, int offset) {
String address = EmailUtils.extractEmailAddress(args, offset);
assertTrue(EmailUtils.isValidEmailAddress(address));
return address;
}
}

0 comments on commit 0313a20

Please # to comment.