Compose an e-mail in HTML format / Save image to HTML

    • Offizieller Beitrag

    Compose an e-mail in HTML format / Save image to HTML
    We have created an example project which shows how to create the data required for HTML e-mails:


    b) OnPrepareImageforSaving
    It uses two events:

    a) HeaderFooter.BeforeSaveToStream - assigned in From.OnCreate

    which is used to write the background image and uses the property 'BodyExtra' property to add a parameter to the BODY tag:

    Code
    procedure TWPMailForm.DoRTFDataSaveToStream(    RTFData: TWPRTFDataCollection;    Writer: TWPCustomTextWriter;    Stream: TStream;    DisplayedRTFBlock: TWPRTFDataBlock;    par_s, par_e: TParagraph; pos_in_par_s, pos_in_par_e:    Integer);begin  if Writer is TWPHTMLWriter then  begin    TWPHTMLWriter(Writer).BodyExtra :=       'background="back.jpg"' ;    Image1.Picture.SaveToFile(HTMLFolder.Text + '\' + 'back.jpg'  );    AttachmentList.Items.Add('back.jpg');  end;end;

    This event is called for each image. In case we write to HTML we create a temporary files (using funtion TWPObject.SaveToFile) and assign the name to the property 'FileName' of the image object:

    Code
    procedure TWPMailForm.WPRichText1PrepareImageforSaving(  RTFData: TWPRTFDataCollection; Writer: TWPCustomTextWriter;  TextObject: TWPTextObj; var DontSave: Boolean);begin  // We clear Source and StreamName to make sure the embedded data is saved to file  TextObject.Source := '';  if TextObject.ObjRef<>nil then  begin     TextObject.ObjRef.StreamName := '';     if FWritingHTML then     begin        inc(FHTMLImgNr);        TextObject.ObjRef.FileName :=          ExtractFileName(  // we want relative filename only        TextObject.ObjRef.SaveToFile(           HTMLFolder.Text,           Format( 'img%d', [FHTMLImgNr] ), '' ));        AttachmentList.Items.Add(TextObject.ObjRef.FileName);     end;  end;end;

    (Info: The RTF writer ALSO triggers the event OnPrepareImageforSaving!)

    To print the tiled image in the background we use the watermark event:

    Code
    procedure TWPMailForm.WPRichText1PaintWatermark(Sender: TObject;
      RTFEngine: TWPRTFEnginePaint; toCanvas: TCanvas; PageRect: TRect;
      PaintPageNr, RTFPageNr: Integer; WaterMarkRef: TObject; XRes,
      YRes: Integer; CurrentZoom: Single; PaintMode: TWPPaintModes);
    begin
     WPPrintTiledBackground(toCanvas, PageRect, Image1.Picture.Graphic,
       XRes / WPScreenPixelsPerInch);
    end;

    The demo appends the created temporary files to the items in combobox AttachmentList - in your project you would use the Append method of your e-mail tool (Indy, Synapse)

    Julian Ziersch