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

Support nested brackets in EmailUtils.extractEmailAddress #17

Closed
aepshteyn opened this issue Feb 14, 2020 · 1 comment
Closed

Support nested brackets in EmailUtils.extractEmailAddress #17

aepshteyn opened this issue Feb 14, 2020 · 1 comment

Comments

@aepshteyn
Copy link

Originally posted on voodoodyne#97 (comment):

I'm using Wiser to test the mail functionality of my Google App Engine app and discovered that there is an issue in the Google App Engine SDK's usage of the Python smtplib.SMTP.sendmail function such that when given a FROM header like "Foo Bar <[email protected]>", it wraps the address string within another pair of angle brackets and issues an SMTP command like mail FROM:<Foo Bar <[email protected]>>, which is unusual, and Wiser returns a 553 / "Invalid email address" response.

I found that changing the address = address.substring(1, address.indexOf('>')) line in org.subethamail.smtp.util.EmailUtils.extractEmailAddress to use lastIndexOf instead of indexOf is a simple solution to the aforementioned problem,

  /**
   * Extracts the email address within a <> after a specified offset.
   */
  public static String extractEmailAddress(String args, int offset) {
    String address = args.substring(offset).trim();
    if (address.indexOf('<') == 0) {
      // address = address.substring(1, address.indexOf('>'));
      // **this patch allows nested angle brackets (e.g. "<Foo Bar <[email protected]>>"):**
      address = address.substring(1, address.lastIndexOf('>'));
      // spaces within the <> are also possible, Postfix apparently
      // trims these away:
      return address.trim();
    }

    // find space (e.g. SIZE argument)
    int nextarg = address.indexOf(" ");
    if (nextarg > -1) {
      address = address.substring(0, nextarg).trim();
    }
    return address;
  }

I believe that this change will not break compatibility with the SMTP RFC, which specifies only that:

The <reverse-path> portion of the first or only argument contains the source mailbox (between < and > brackets)

This spec does not appear to explicitly prohibit nested brackets.

My patched code still supports addresses without additional nested brackets (i.e. both MAIL FROM:Foo Bar <[email protected]> and MAIL FROM:<Foo Bar <[email protected]>> will be accepted.

I realize that the App Engine/Python peculiarity that I described doesn't have anything to do with subethasmtp, but I was wondering if you'd be willing to accept this tiny patch anyways. Would be happy to create a pull request if you accept.

Since Wiser is intended to be used for testing, I think it should be pretty lenient with what it accepts. Another argument in favor of this patch is that Dumbster is able to handle such email addresses without a problem.

@davidmoten
Copy link
Owner

fixed in #18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants