How do I embed the image in an html email?

General TRichView support forum. Please post your questions here
Post Reply
Vandy1Fan
Posts: 10
Joined: Thu Jun 21, 2012 6:47 pm
Location: United States

How do I embed the image in an html email?

Post 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.
Vandy1Fan
Posts: 10
Joined: Thu Jun 21, 2012 6:47 pm
Location: United States

Post 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;
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Assign OnSaveImage2 event, like in the demo.
Vandy1Fan
Posts: 10
Joined: Thu Jun 21, 2012 6:47 pm
Location: United States

Post by Vandy1Fan »

Got it, thanks.
Post Reply