Semi-private image folder / sending images in newsletter / email

Is it possible to have a folder ( 'images' ) which is part of an intranet, but if an image is linked to in an email (a newsletter sent from the same site') it is shown in the email (newsletter).

You could create the newsletter in a way that it is self contained (i.e. it incorporates the image source rather than a link to the image) -- emails of this type are using the MIME type "multipart/related". This way, only the generation process must access the image source - and you can give it appropriate rights.

If you do not want to put the image source in the newletter itself but refer to the image via an url, then you must ensure that this url can access the image - even though this is usually not visible. You can achieve this with a special view accessing and delivering the image outside the usual security restrictions. Of course, you must be careful that such a view does not introduce access to objects you really want to be restricted.

Thanks a lot.
Putting the images in the email would work for me.
Not sure how to do it, ... I am thinking on two different approaches that could work (?):

  1. Look for image-urls in the html, change the urls in the html and attach the images
  2. Some kind of transform that transforms from html to 'mail-html with image data included'

(I am not sure what is meant by 'incorporates the images'

The simplest way is to make your 'images' folder publicly accessible by allowing 'View' for 'Anonymous' via the sharing tab of your images folder. That way, the images are public even through the rest of the site is not.

Yes, but I dont trust my users (and also it would be nice to do the same for attachments (pdf's).

Inlining attachments directly into the email will be more of a coding challenge. Basically like the following, except you need to take care that you're not performing .read() on a filehandle but on a image field.

This is how I have done it (for a former employer): look in the HTML mail message for local urls to content objects; replace the url by "cid:" urls (those are used by "multiplart/related" for the linkage); attach the content object's content using the same id as that used in the corresponding "cid:" url.

I have forgotten the precise relation between the "cid:" url and the corresponding attachment. Likely, the attachment's "Message-Id" was used in the form "cid:Message-Id" as "cid:" url. The "multipart/related" specification should answer this question (or a look into any email message using "mutipart/related").

Out of curiosity: why did you look in the html mail message for the urls, would it not be easier to look for the images in the body_text (this would probably also work for 'plain text mail' (?) and one could maybe use lxml ?

I had a specific use case: the input was HTML with references to content objects and this HTML should become the main body of an email message with attachments for the referenced objects and proper linkage between the main body and the attachments.
Your use case might be slightly different and with it your solution.

You can use lxml's HTML support to search for and transform the local urls.

I do not know whether the "multipart/related" linkage described in my previous response works for text only messages. Likely, it depends on the mail reader.

The "cid:" urls may not use the "Message-Id" MIME header but instead a "Content-Id" MIME header (the "cid" name indicates something like this).