Avatar of graga
graga
 asked on

Delphi - HTML Formatted Mail - don't automatically download pictures

I wrote an email client and I am using profDHTMLEdit to display received, html formatted email message.
I want to have an option to prevent automatically downloading pictures, simmilar to the way outlook does it.
Thanks.
DelphiHTML

Avatar of undefined
Last Comment
graga

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ThievingSix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
ThievingSix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
graga

ASKER
Thanks, this looks like a good start. I will give it a try today.
I'm just thinking how to implement displaying a substitute 'frame' that is placed where the picture was and finally how to do the 'Download Pictures'. (like in outlook)
For the latter I think a re-reading of the message from the server.
graga
ThievingSix

Well, you could just save the original html and if they want the pictures you just replace the edited version with the original. One less GET to the server. You could just remove the picture completely. Or you can substitute your own picture there.
graga

ASKER
Hi,
I think I've got it.
I parse for '<imv ' and in the first ' src=' I insert some code. like [BlockedImageLink].
The displayed html preserves the location and size of the images, but does not display anything
To revert, I remove the inserted code from html.
I'm using madshi string libraries in the code.
I will give you points for pointing me in the right direction.

so here it is:

function BlockImageLinks(Source: string): string;
var ImgPos, SrcPos: Integer;
    FromPos, ToPos: Integer;
    c: char;
begin
     Result  := '';
     FromPos := 1;
     ToPos   := Length(Source);

     // process all occurences of '<img ' substring
     ImgPos := PosStr('<img ', LowerCase(Source), FromPos, Length(Source));

     while ImgPos > 0 do begin

           // find ' src=' substring following the '<img ' substring
           SrcPos := PosStr(' src=', LowerCase(Source), ImgPos+1, Length(Source));

           if SrcPos > 0 then begin
              Result := Result + Copy(Source, FromPos, (SrcPos + 6) - FromPos) + '[BlockedImageLink]';
              FromPos := SrcPos + 6;

              while FromPos < Length(Source) do begin

                    // search for the closing '>'
                    c := Source[FromPos];
                    Result := Result + c;
                    inc(FromPos);

                    if c = '>' then
                       Break;

                    end;
              end

           else begin

                // something is wrong with html code, return the original html
                Result := Source;
                Exit;
                end;

           ImgPos := PosStr('<img ', LowerCase(Source), FromPos, Length(Source));
           end;

     Result := Result + Copy(Source, FromPos, Length(Source));
end;

function UnBlockImageLinks(Source: string): string;
begin
     Result := Source;
     KillStr(Result, '[BlockedImageLink]');
end;

// and this is how it is called:

procedure TForm1.Button1Click(Sender: TObject);
begin
     profDHTMLEdit.LoadFromFile('D:\temp\MailBody.htm');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
     profDHTMLEdit.Source := BlockImageLinks(profDHTMLEdit.Source);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
     profDHTMLEdit.Source := UnBlockImageLinks(profDHTMLEdit.Source);
end;



Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
graga

ASKER
Pointed me in the right direction, but code the solution did not fully exploit the problem.