[ 
https://issues.apache.org/jira/browse/EMAIL-92?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12886267#action_12886267
 ] 

Dominik Stadler commented on EMAIL-92:
--------------------------------------

Some items that I found: 

- Currently you only replace img-tags under the following condition:      
{code} 
if(!this.inlineEmbeds.containsKey(imageDataSource.getName()))
{code} 

what if the same image is included in the HTML multiple times? With this check 
it would only be replaced once and then not any more? It seems the embed() call 
performs this check internally as well and returns the same cid for existing 
images, so it would be save to call embed() with every DataSource that we find.

- I enhanced the regex slightly to cover cases that we handled incorrectly 
before, the full regex is now:
    public static final String REGEX_IMG_SRC = 
"(<[Ii][Mm][Gg]\\s*[^>]*?\\s+[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])";

The Following addition to the regex-test verifies that the regex is working 
with the newly discovered cases:

{code}
                // had a problem with multiple img-source tags
                matcher = pattern
                                .matcher("<img src=\"file1\"/><img 
src=\"file2\"/>");
                assertTrue(matcher.find());
                assertEquals("file1", matcher.group(2));
                assertTrue(matcher.find());
                assertEquals("file2", matcher.group(2));

                matcher = pattern
                                .matcher("<img src=\"file1\"/><img 
src=\"file2\"/><img src=\"file3\"/><img src=\"file4\"/><img src=\"file5\"/>");
                assertTrue(matcher.find());
                assertEquals("file1", matcher.group(2));
                assertTrue(matcher.find());
                assertEquals("file2", matcher.group(2));
                assertTrue(matcher.find());
                assertEquals("file3", matcher.group(2));
                assertTrue(matcher.find());
                assertEquals("file4", matcher.group(2));
                assertTrue(matcher.find());
                assertEquals("file5", matcher.group(2));

                // try with invalid HTML that is seens sometimes, i.e. without 
closing "/" or "</img>"
                matcher = pattern
                                .matcher("<img src=\"file1\"><img 
src=\"file2\">");
                assertTrue(matcher.find());
                assertEquals("file1", matcher.group(2));
                assertTrue(matcher.find());
                assertEquals("file2", matcher.group(2));
{code}


> Improve support to embed images in HTML eMails
> ----------------------------------------------
>
>                 Key: EMAIL-92
>                 URL: https://issues.apache.org/jira/browse/EMAIL-92
>             Project: Commons Email
>          Issue Type: New Feature
>            Reporter: Dominik Stadler
>            Assignee: Siegfried Goeschl
>         Attachments: EMAIL-92-with-test.patch, ImageHtmlEmail.java, 
> ImageHtmlEmail.java
>
>
> I have created a improvement on top of HtmlEmail class which automatically 
> detects <img src=".."/> tags in the HTML source and which embeds all the URLs 
> correctly in the email so they are sent as attachments as part of the email. 
> It is implemented as new class ImageHtmlEmail, no existing code of commons 
> email is touched at all. 
> This make sending HTML Mails much easier, now it is as simple as 
>                 ImageHtmlEmail email = new ImageHtmlEmail();
>                 email.setHostName(SMTP_HOST);
>                 email.addTo(EMAIL_TO, "DS");
>                 email.setFrom(EMAIL_FROM, "Me");
>                 email.setSubject(EMAIL_SUBJECT);
>                 
>                 String html = FileUtils.readFileToString(new 
> File("/tmp/test_email.html"));
>                 
>                 // set the html message
>                 email.setHtmlMsg(html, new File("/tmp"));
>                 // set the alternative message
>                 email.setTextMsg("Your email client does not support HTML 
> messages");
>                 // send the email
>                 email.send();
> as stated in the mail, I am making this availalbe under the Apache License 
> 2.0 so that it can be integrated if it sounds useful.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to