0

I have been tasked with writing an email archiving application. With lots of help from other SO questions and answers, I have succesfully saved the body of the email using Indy10 in Delphi 10.2.3, and it's image attachments as Base64encoded text files.

The body of an email with inline images contains strings like this:

<img width=360 height=153 id="Picture_x0020_8" src="cid:image002.png@01D4F3AE.C0AE8970">

And of course, the base64 encoding for that image looks like this:

data:image/x-png;base64,
iVBORw0KGg...Jggg==

My first question is about cr_lf. I have cr_lf just after base64, and at the end of the file (last 2 bytes). Is this correct for what I want to do?

Now I have to replace the <image width=360..8970"> with the base64encoded image text - and after that, save the new body to the archive, of course.

I can easily delete <image width=360..8970"> part from the body file, and then I want a simple way to insert the contents of the base64 txt file into that file at the right place.

I'll accept references to other SO questions I might have missed that do similar things. I haven't found anything in Delphi that helps me do this.

Thanks

Freddie Bell
  • 2,186
  • 24
  • 43
  • If you already know how to modify a string to delete characters from it, what is stopping you from modifying a string to insert characters into it? What is the actual problem you are trying to solve? A `data:` url can be used as the `src` attribute value of an `` tag, provided an HTML viewer displaying the email recognizes `data:` urls. But why are you saving attachments externally at all? Email attachments are already embedded in the email, just archive the whole email as-is. Your example `` tag is referencing an embedded attachment, not an external file. – Remy Lebeau Apr 24 '19 at 21:25
  • The images aren't embeded in the email, only the CID references are! And no, I don't know how to save the attachments so that a load of the html in a browser can find those images! I was hoping there'd be an existing Indy function that does base64 insertion for me. I'm now using filestreams to do the replacement of the with after I've saved the body and images locally. I can insert the whole thing into a varchar(max) in a table. – Freddie Bell Apr 25 '19 at 06:16
  • [by definition](https://tools.ietf.org/html/rfc2392), a `cid:` URL can't refer to a resource outside of the email. So if an `` tag has a `cid:` url as its `src`, then the image it refers to must be embedded in the same email, otherwise the reference is broken. Emails are self contained during transmission, so if you save an email as-is, you are also saving all of the attachments, images, etc that are in the email, just as they were sent. So I still don't see the point of what you are trying to accomplish. – Remy Lebeau Apr 25 '19 at 07:42
  • Ok I understand why you are confused. In my code I have tried: `aMsg.SaveToFile('test.html')` where `aMsg` is a `TIdMessage` instance, and what I got was "a mess" that wasn't understood by my Firefox browser. So then I started using the code as shown at // https://stackoverflow.com/questions/14671010 to save the messageparts... – Freddie Bell Apr 25 '19 at 08:06
  • of course the browser does not understand it. An email as a whole is not an HTML document, so saving the entire email will not work. You would need to find just the HTML part inside the email and save just that part to file, then parse the HTML to find the attachment references, find those attachments in the email and save them to files, and adjust the HTML to refer to those files. – Remy Lebeau Apr 28 '19 at 02:06
  • @RemyLebeau .. which would bind the html to a specific underlying folder structure. That's why replacing the attachment references with the base64-endoded image strings, makes the email entirely portable. I have achieved this now. Thanks. – Freddie Bell Apr 28 '19 at 04:44

0 Answers0