WPCubed Product Support

Create HTML from RichText-content

Hi Julian,

I placed one TWPRichEdit and a TWPToolBar on a Form.
Than I entered some formated text into the editor and added an image.
The content of the editor needs to be saved as HTML in order to insert it into the body of an E-mail.
For sending the Email I use Indy 10 -> TIdSMTP and TIdMessage.

I've looked at [](http://wpcubed.com/forum/viewtopic.php?t=3194). The HTML-file that is created is containing the right format, but the E-mail is only containing plain text.

procedure TFSendMail.SendEmailMessage(body : string; Attachments : TStringList);
var attachimg : TIdAttachmentFile;
    emptybody, textbody, htmlbody : TIdText;
begin
  // When using Indy you need to uise the message object
  // "EML" is just a multi part MIME version of the message with all
  // attachments. Usually the links in embedded HTML are replaced by
  // CID references. But this is done by the e-mail library when converting the HTML
  emptybody := TIdText.Create(MailMessage.MessageParts, nil);
  emptybody.ContentType := 'multipart/alternative';
  emptybody.ParentPart := -1;

  textbody := TIdText.Create(MailMessage.MessageParts, nil);
  textbody.Body.Text := '';
  textbody.ContentType := 'text/plain';
  textbody.ParentPart := 0;

  htmlbody := TIdText.Create(MailMessage.MessageParts, nil);
  htmlbody.body.text := body;
  htmlbody.ContentType := 'text/html';
  htmlbody.ParentPart := 0;

  Attachimg := TIdAttachmentFile.Create(MailMessage.MessageParts, PChar(Attachments));
  Attachimg.ContentID                   := '<12345>';
  Attachimg.FileIsTempFile              := False;
  Attachimg.ContentDisposition          := 'inline';
  Attachimg.ContentType                 := 'image/jpeg';
  Attachimg.ParentPart                  := -1;

  MailMessage.ContentType               := 'multipart/related; type="multipart/alternative"';
  MailMessage.CharSet                   := 'iso-8859-1';
  MailMessage.Encoding                  := meMIME;
  MailMessage.From.Address              := 'mail@mymail.nl';
  MailMessage.From.Name                 := 'My name';
  MailMessage.Recipients.EMailAddresses := 'info@mymail-plus.nl';
  MailMessage.Subject                   := 'Test';

  Mail.Username := 'my@mymail.nl';
  Mail.Password := 'secretcode';
  Mail.Host     := 'mail.mymail.nl';
  Mail.Port     := 25;

  try
    Mail.Connect();
    try
      Mail.Send(MailMessage);
    except
      on E: Exception do
        showmessage('Send Failed ' + E.Message);
    end;
  finally
    MailMessage.MessageParts.Clear;
    MailMessage.Headers.Clear;
    Mail.Disconnect;
    showmessage('Send Successful');
  end;
end;

Can you give me an simple example on how add the HTM-file to the body including all images that are in the RTF?

Thank you!

#1 ♥ 0
8.7k Views 1 links 3 users read 6 min
support

Hi,

As far as I know you cannot use HTML in the body of an HTML file. HTML is always attached to a message. So you need to create a HTML file with all the images on same directory and add all of it to the mail as attachments.

Julian

#2 ♥ 0

Well I can do something like below and it will work as expected. So I need to have the HTML-part of TWPRichText that is saved to the HTML-file.

uses idMessage;

procedure TForm1.Button1Click(Sender: TObject);
var
  html: TStrings;
  htmpart, txtpart: TIdText;
  bmppart: TIdAttachment;
  email: TIdMessage;
  filename: string;
begin
  filename := ExtractFilePath(Application.ExeName) + 'us.jpg';

  html := TStringList.Create();
  html.Add('<html>');
  html.Add('<head>');
  html.Add('</head>');
  html.Add('<body><h1>Hello</h1>');
  html.Add('<img>');
  html.Add('This is a picture of us!</body>');
  html.Add('</html>');

  email := TIdMessage.Create(nil);
  email.From.Text := 'Pete@NooooSpammmm.Droopyeyes.com';
  email.Recipients.EMailAddresses := 'Pete@NoooSpammmm.droopyeyes.com';
  email.Subject := 'Hello';
  email.ContentType := 'multipart/mixed';
  email.Body.Assign(html);

  txtpart := TIdText.Create(email.MessageParts);
  txtpart.ContentType := 'text/plain';
  txtpart.Body.Text := '';

  htmpart := TIdText.Create(email.MessageParts, html);
  htmpart.ContentType := 'text/html';

  bmppart := TIdAttachment.Create(email.MessageParts, filename);
  bmppart.ContentType := 'image/jpeg';
  bmppart.FileIsTempFile := true;
  bmppart.ContentDisposition := 'inline';
  bmppart.ExtraHeaders.Values['content-id'] := 'us.jpg';
  bmppart.DisplayName := 'us.jpg';

  try
    idSMTP.Connect();
    try
      idSMTP.Send(email);
      ShowMessage('Sent');
    except
      on E: Exception do
        ShowMessage('Failed: ' + E.Message);
    end;
  finally
    idSMTP.Disconnect();
    email.Free();
    html.Free();
  end;
end;
#3 ♥ 0

Since Julian is on his vacation i have dug up this code that i wrote a long time ago. This code has not been used or maintained since, so i can't be certain if it will still work, but it is something we are going to use sometime soon.

TCsTreeDoc is an override class for TWpRichtext.

procedure TCsTreeDoc.PrepareRTFAttachments(pvTempFolder: string);
  {* @bug [2251] 21.09.2005 LE  procedure updates all Image objects inside the RTF to
  support linked images. If no link exists then a file for the image object is created
  in the folder pvTempFolder. All paths to the images are stored in the list FAttachments.
  Attachment list can be retrieved using TCsTreeDoc.Attachments *}
var
 i : integer;
begin
  FAttachments.Clear;
  for i := 0 to textObjects.ObjCount - 1 do
  begin
    with TextObjects.ObjList[i] do
    begin
      if ObjType = wpobjImage
      then if objref <> nil then
      begin
        if ObjRef.StreamName = '' then  //if image isn't linked to a file then a file
                                        //will be created and the object linked to this file
        begin
          inc(FlinkedImgID);            //increase ID to make sure the Filename is unique.

          ObjRef.StreamName :=          //Link the image...
           ObjRef.SaveToFile(           //..to the location where we save the file
             pvTempFolder,
              Format( 'img~%d', [FlinkedImgID] ), '' {)});
          ObjRef.FileName := ObjRef.StreamName;  //Link for HTML mail.
        end;
        // Add to the attachment list if file is not yet included in this list
        // Multiple images can be linked to the same file
        if FAttachments.IndexOf(ObjRef.StreamName) = -1
        then FAttachments.Add(ObjRef.StreamName);
      end;
    end;
  end;

I think WpTools will generate HTML with the correct Content Identifiers for the attachments. These content identifiers must match the content identifiers in the email file (content-id) for the TIdAttachment object.

#4 ♥ 0

Hi,

I hope I found the problem for my issue. Currently I'm able to sent the email, but images that are included in the HTML aren't visible in Outlook Express (they are in MS Outlook). This has to do with the reference to the image in the created HTML-file.

<img src="Img1.JPG" style="width:1.04in;height:0.78in;"/>

Should be altered to:

    <img src="cid:Img1.JPG" style="width:1.04in;height:0.78in;"/>

MS Outlook is more lenient about that kind of error, but the HTML-code should be fixed for other readers.

Do you have any solution for this?

#5 ♥ 0
support

Hi,

please note the image writing code in WPIOHTML.PAS

if (fnam = '') and (obj <> nil) then
fnam := obj.FileName;
if (fnam = '') and (obj <> nil) then
fnam := obj.StreamName;

// Before WRITE HTML Images
if fnam <> '' then
begin
WriteString('<img src="');
WriteString(fnam);
WriteString('"');

I am not sure why WPTools should add "cid:" here since it does not explicitely writes HTML for e-mail but you can of course add a

if Copy(fnam ,1,4)<>'cid:' then
fnam := fnam + 'cid:';

here. If this works well I make it an option for the future.

Julian

#6 ♥ 0

Yes it works perfectly.
You just have to change the proposed code a little bit to:

if Copy(fnam ,1,4)<>'cid:' then 
fnam := 'cid:' + fnam;

It would be great if you could add it as an option. Please keep me informed.

#7 ♥ 0

I had hoped the proposed option would be added in 5.38 but it is not.
Do you still consider to add it (as it works perfectly)?

Thanks, Stef

#8 ♥ 0
support

I forgot about this.

I intended to place this in IFFDEFS

maybe you can mail me the modified unit and I see where I have to place it.

Julian

#9 ♥ 0

Hi Julian,

I've just send you an E-mail with the unit and specifications.

With regards,
Stef

#10 ♥ 0
This category is archived. No new posts are possible.