Uploaded image for project: 'Commons Email'
  1. Commons Email
  2. EMAIL-127

ImageHtmlEmail: Dollar or Backslash in certain places in <img ... src...>-tags causes exceptions "IllegalArgumentException: Illegal group reference"

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Fixed
    • 1.3.1
    • 1.3.2
    • None
    • Linux

    Description

      I have a unit test which shows that dollar sign and backslash cause ImageHtmlEmail to throw an Exception (unfortunately not easy to provide a complete unit test, but it should be easy to enhance one of the existing tests for ImageHtmlEmail):

      FileUtils.writeStringToFile(file, "<html>teststring <img title=\"$\" src=\"file://" + file.getAbsolutePath() + "\"/></html>");

      sender.sendHTMLEmail(files, mailConfig, emailConfig, REPORT_DIR);

      It will throw the following Exception:

      java.lang.IllegalArgumentException: Illegal group reference
      at java.util.regex.Matcher.appendReplacement(Matcher.java:808)
      at org.apache.commons.mail.ImageHtmlEmail.replacePattern(ImageHtmlEmail.java:180)
      at org.apache.commons.mail.ImageHtmlEmail.buildMimeMessage(ImageHtmlEmail.java:103)
      at org.apache.commons.mail.Email.send(Email.java:1427)
      at EmailSender.sendHTMLEmail(EmailSender.java:93)
      at EmailSenderTest.testImageHtmlEmailDollarBackslash(EmailSenderTest.java:801)

      the cause is that Matcher.appendReplacement() actually treats Dollar and Backslash as special characters. From the javadoc of Matcher:

      • Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
      • the replacement string may cause the results to be different than if it
      • were being treated as a literal replacement string. Dollar signs may be
      • treated as references to captured subsequences as described above, and
      • backslashes are used to escape literal characters in the replacement
      • string.

      A possible fix is to replace line 180 of ImageHtmlEmail.java with the following, i.e. replace $ by \$ and \ by \ \ to have them properly escaped.

      matcher.appendReplacement(stringBuffer, (matcher.group(1) + "cid:" + cid + matcher.group(3)).replace("\\", "\\\\").replace("$", "\\$"));
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            centic Dominik Stadler
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: