Page 1 of 1

html email with pics

Posted: Mon Nov 10, 2014 2:58 pm
by Lucian
Hi,

I am trying to send an html email with a picture in the body of the email and the picture is transformed in an attachment. Is this normal?

For this, I am using TRVReportHelper, TRvHtmlImporter, TRichView. Code is something like this:

lRVHelper:= TRVReportHelper.Create(nil);
lRVHtmlImporter := TRvHtmlImporter.Create(nil);
lRVHtmlImporter.RichView := lRVHelper.RichView;
...
SaveOptions := [rvsoOverrideImages];
FileName := Format('%sph_mail.html', [sMailDir]);
lRVHelper.RichView.SaveHTML(FileName, '', 'htmlimage', SaveOptions);

... and from here I grab the file and the eventual images and use Indy to send an email, something like this:

MB := TIdMessageBuilderHtml.Create;
TIdMessageBuilderHtml(MB).Html.Text := MailBody;
if imgs <> nil then
for i := 0 to imgs.Count - 1 do
TIdMessageBuilderHtml(MB).HtmlFiles.Add(imgs);
AddAttachments(MB);


My boss wants to get the picture in the body of the email, like it was when the message was created in TRichView. Is this possbile?

Posted: Mon Nov 10, 2014 4:29 pm
by Sergey Tkachenko
Sorry, I do not have an example building email using TIdMessageBuilderHtml.

The example of building HTML email using TIdText and TIdAttachmentMemory parts can be found here:
http://www.trichview.com/support/files/mime.zip
(in Delphi\Indy\ folder)

Posted: Mon Nov 10, 2014 4:55 pm
by Lucian
The problem is not with Indy, I am not asking about that. The problem is with RichView.SaveHTML method which saves images in external files. I am asking if there's a way of saving the html file, without using external files (might be a silly question, but I never had any interests in learning html, so I don't know if that's even possible). Something like a <BINARY> tag, the way PDF format supports saving image content in the pdf itself.

Because the images are saved in external files, they must be attached, and so, the program that receives the email, has the picture as an attachment, but the picture was in the body when the message was created.... I hope it's clear now.

Posted: Tue Nov 11, 2014 12:20 pm
by Sergey Tkachenko
Please look in the code:
http://www.trichview.com/support/files/mime.zip
(in Delphi\Indy\ folder)
It solves all these problems.

This demo uses OnSaveImage2 event to store images in memory (in a collection) instead of files, and providing 'cid:' links for <img src>. Later, these images are saved as TIdAttachmentMemory.

Please note that images and attached files are saved in different places of HTML email. It is important!
Generally, nesting structure of HTML email containing all parts is the following:
(((text HTML) image1 image2 ...) file1 file2 ...)
All parts must have correct content type.
For (text HTML): ContentType := 'multipart/alternative'.
For part combining (text HTML) and images: ContentType := 'multipart/related; type="multipart/alternative"'.
For the whole message: ContentType := 'multipart/mixed'.
If the message does not have attachments or images, some parts are not needed.

You can use the ready-to-use code from that demo, or can try to create your code using TIdMessageBuilderHtml.

Thank you very much

Posted: Tue Nov 11, 2014 2:04 pm
by Lucian
I'll check the sample.