Page 1 of 1

How do I embed the image in an html email?

Posted: Fri Jun 29, 2012 9:52 pm
by Vandy1Fan
I noticed that when I extract the rvf data from the database field using your demo email program, it sends the email but the images are references to 1.jpg, etc.

How do you have the image actually be in the email instead of a reference?

Thanks.

Posted: Fri Jun 29, 2012 9:55 pm
by Vandy1Fan
Here is the code. I am using the DBRichViewEdit. I am opening a query and the DBRichViewEdit is connected to the datasource.

Code: Select all

procedure TfrmMain.BuildEmail;
var Stream: TMemoryStream;
    ws: String;
    s: TRVRawByteString;
    i: Integer;
    txtpart, htmpart, txthtmpart, txthtmlimgpart: TIdText;
    imgpart: TIdAttachmentMemory;
begin
  // saving text
  txtpart := TIdText.Create(IdMessage1.MessageParts);
  Stream := TMemoryStream.Create;
  DBRichViewEdit1.SaveTextToStreamW('', Stream, 80, False, False);
  Stream.Position := 0;
  SetLength(ws, Stream.Size div 2);
  if Stream.Size<>0 then
    Stream.ReadBuffer(Pointer(ws)^, Stream.Size);
  Stream.Free;
  txtpart.Body.Text := stringreplace(ws, '#CUSTID#', 'Customer goes here', [rfReplaceAll]);
  txtpart.ContentType := 'text/plain';
  txtpart.ContentTransfer := 'quoted-printable';
  txtpart.CharSet := 'utf-8';
  // saving HTML
  htmpart := TIdText.Create(IdMessage1.MessageParts);
  Stream := TMemoryStream.Create;
  DBRichViewEdit1.SaveHTMLToStreamEx(Stream, '', txtSubject.Text, '', '', '', '',
    [rvsoUseCheckpointsNames, rvsoUTF8]);
  Stream.Position := 0;
  SetLength(s, Stream.Size);
  if Stream.Size<>0 then
    Stream.ReadBuffer(Pointer(s)^, Stream.Size);
  Stream.Free;
  htmpart.Body.Text := UTF8ToUnicodeString(s);
  htmpart.Body.Text := stringreplace(htmpart.Body.Text, '#CUSTID#', 'Customer goes here', [rfReplaceAll]);
  htmpart.ContentType := 'text/html';
  htmpart.ContentTransfer := 'quoted-printable';
  htmpart.CharSet := 'utf-8';
  // combining text and HTML
  IdMessage1.ContentType := 'multipart/alternative';
  if (Attachments.Count=0) and (HTMLImages.Count=0) then
    exit;
  txthtmpart := TIdText.Create(IdMessage1.MessageParts);
  txthtmpart.ContentType := 'multipart/alternative';
  txthtmpart.Index := 0;
  txtpart.ParentPart := txthtmpart.Index;
  htmpart.ParentPart := txthtmpart.Index;
  // images
  for i := 0 to HTMLImages.Count-1 do begin
    HTMLImages[i].Stream.Position := 0;
    imgpart := TIdAttachmentMemory.Create(IdMessage1.MessageParts, HTMLImages[i].Stream);
    imgpart.ContentType := HTMLImages[i].ContentType;
    imgpart.ContentID := '<'+HTMLImages[i].Name+'>';
    imgpart.ContentDisposition := 'inline';
  end;
  // combining images and text+html
  IdMessage1.ContentType := 'multipart/related; type="multipart/alternative"';
  if Attachments.Count=0 then
    exit;
  if HTMLImages.Count>0 then begin
    txthtmlimgpart := TIdText.Create(IdMessage1.MessageParts);
    txthtmlimgpart.ContentType := 'multipart/related; type="multipart/alternative"';
    txthtmlimgpart.Index := 0;

    txthtmpart.ParentPart := txthtmlimgpart.Index;
    for i := IdMessage1.MessageParts.Count-1-HTMLImages.Count to IdMessage1.MessageParts.Count-1 do
      IdMessage1.MessageParts[i].ParentPart := txthtmlimgpart.Index;

    txtpart.ParentPart := txthtmpart.Index;
    htmpart.ParentPart := txthtmpart.Index;
  end;
  // files
  IdMessage1.ContentType := 'multipart/mixed';
  for i := 0 to Attachments.Count-1 do
    TIdAttachmentFile.Create(IdMessage1.MessageParts, Attachments[i]);
end;

Posted: Sat Jun 30, 2012 2:40 pm
by Sergey Tkachenko
Assign OnSaveImage2 event, like in the demo.

Posted: Mon Jul 02, 2012 1:21 pm
by Vandy1Fan
Got it, thanks.