You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for dynamic datasource resolution (file/url/classpath) for embedded images in HTML body #260
Currently, in order to embed an image a user would have to modify both the HTML template and the Java code like this
java
currentEmailBuilder.withEmbeddedImage("smiley", new FileDataSource("smiley.jpg"));
and then the corresponding HTML code
<p>Smile!</p><img src='cid:smiley'>
this is fine but in many situations users are allowed to modify the HTML template but the java code would not be available to be modified by the users; thus would be useful to allow dynamically embedding images in emails only by referencing the image files in the HTML code.
I also used the apache commons email and they provide the option, only be modifying the HTML template to include images available both locally and also on remote websites.
Apache commons email has something like a DatasourceResolver which could be either DataSourceFileResolver or DataSourceUrlResolver like this
Is there something similar available in simple-java-mail?
P.S - I believe that simple-java-mail has a very good API and I intend switching from apache commons email for the additional features which your API provides like delivery / read receipt and other things. I also intend to use email-rfc2822-validator for validating email addresses. Thank you for the hard work you put in this project.
Sorry, I see I forgot to reply to this. I think this is a very interesting feature and I'm considering adding it. I'll look into this DataSourceFileResolver.
My first thought is that I would have to parse the HTML for all the src attributes. I solve that (rather ugly) with regular expression.
My first thought is that I would have to parse the HTML for all the src attributes. I solve that (rather ugly) with regular expression.
Looking an commons email, they indeed are using regex to find all images:
// Regular Expression to find all <IMG SRC="..."> entries in an HTML// document.It needs to cater for various things, like more whitespaces// including newlines on any place, HTML is not case sensitive and there// can be arbitrary text between "IMG" and "SRC" like IDs and other things./** Regexp for extracting {@code <img>} tags */publicstaticfinalStringREGEX_IMG_SRC =
"(<[Ii][Mm][Gg]\\s*[^>]*?\\s+[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])";
What do you think about the lenient flag Commons Email uses? Lenient means no error will occur for datasources that cannot be resolved, resulting in missing images in the email. Personally, I find that a bad practice, especially because it is a silent process.
changed the title [-]Is it possible to embed images dynamically only by referencing images in the HTML code?[/-][+]Add support for dynamic datasource resolution (file/url/classpath) for embedded images in HTML body[/+]on Jul 5, 2020
I also think that out of the box, if a flag like lenient is not specifically configured, IO Exceptions should be raised that images are missing.
However, if the user will specifically configure lenient as being true, then no exception will be thrown and the emails will be sent with missing images. Warnings could be raised in the log files in this case, but the emails will be sent.
Security follow-up: #678 implements the containment boundary intended by the base-directory, base-classpath, and base-URL controls introduced here. Auto-resolution remains opt-in, and the explicit “allow outside” options remain available; v9.2.0 will block traversal, alternate URL origins, symlink escapes, and escaping redirects when those options are false.
Activity
bbottema commentedon Jun 13, 2020
Sorry, I see I forgot to reply to this. I think this is a very interesting feature and I'm considering adding it. I'll look into this DataSourceFileResolver.
My first thought is that I would have to parse the HTML for all the src attributes. I solve that (rather ugly) with regular expression.
bbottema commentedon Jul 5, 2020
Looking an commons email, they indeed are using regex to find all images:
bbottema commentedon Jul 5, 2020
What do you think about the lenient flag Commons Email uses? Lenient means no error will occur for datasources that cannot be resolved, resulting in missing images in the email. Personally, I find that a bad practice, especially because it is a silent process.
#260: dynamic datasource resolution for html embedded images
[-]Is it possible to embed images dynamically only by referencing images in the HTML code?[/-][+]Add support for dynamic datasource resolution (file/url/classpath) for embedded images in HTML body[/+]#260: Added api for setting baseUrl as String and added more junit te…
distributev commentedon Jul 7, 2020
I also think that out of the box, if a flag like lenient is not specifically configured, IO Exceptions should be raised that images are missing.
However, if the user will specifically configure lenient as being true, then no exception will be thrown and the emails will be sent with missing images. Warnings could be raised in the log files in this case, but the emails will be sent.
bbottema commentedon Jul 7, 2020
I'm thinking of leaving the flag out completely.
distributev commentedon Jul 7, 2020
this works too
bbottema commentedon Jul 9, 2020
I'm going all in with this feature, it's a very cool addition!
So here's what I've added and tested fully with junit:
Supported by config properties as well:
Any thoughts on this API design? I think this is more robust than the Commons Email solution.
#260: Added lots of tests, fixed a name regarding naming and also wit…
#260: Changed default behavior to not auto-resolve, added api to enab…
bbottema commentedon Jul 10, 2020
Feature released in 6.2.0!
bbottema commentedon Aug 5, 2026
Security follow-up: #678 implements the containment boundary intended by the base-directory, base-classpath, and base-URL controls introduced here. Auto-resolution remains opt-in, and the explicit “allow outside” options remain available; v9.2.0 will block traversal, alternate URL origins, symlink escapes, and escaping redirects when those options are false.