How to set an HTML mailto href by calling a Delphi function?

I want in my HTML an href link which when clicked does a "mailto", like this:

<a href="">Mail me</a>

Now, instead of the hard coded email address I want to return the address from a Delphi function. So I add a function like this (in a Unit named FormImprint):

function GetEMailAddress(AddrNo: Integer): String;
 Result := '';

Then in the HTML I have tried something like:

<a href="javascript:pas.FormImprint.GetEMailAddress(1)">Mail me</a>

The Pascal function seems to get called successfully, but instead of opening an empty email I see a blank page with nothing but the string "".

What do I have to do for the "mailto:" action to execute? Is my way of approaching this the best practice?

Have you considered something like:

  webhtmlanchor1.HREF :='';
  webhtmlanchor1.Caption := 'mail tms';

Thank you Bruno, but then the email address is visible in the HTML DOM as if it was hardcoded, which is not wanted. The email address should be obfuscated to prevent spam and therefore rertrieved through a function call, which e.g. gets the email address from a DB or from some kind of decryption. So the email address should be provided in the very moment when the user clicks the href. My approach of

<a href="javascript:pas.FormImprint.GetEMailAddress(1)">Mail me</a>

basically already does what I want by means of that the pascal function is called. My problem is just that this does not trigger the email client program to popup and show a correctly pre-addressed email envelope.

When my pascal function returns "mailto:name@server" the browser opens a new empty page with the text "mailto:name@server". When I do

<a href="mailto:javascript:pas.FormImprint.GetEMailAddress(1)">Mail me</a>

and my pascal function returns just "name@server", the email client pops up as desired but the TO address is set to "javascript:pas.FormImprint.GetEMailAddress(1)". I tried some other keyword bingo but to no avail so far.

Any further ideas?

Got it! The solution is to not use the HREF property at all (leave it empty) and instead in the OnClick of the linked TWebHTMLAnchor do an Application.Navigate('mailto:name@server');

1 Like

Hi Walter,

Have you been able to add an attachment to the email?



Hello Ken,
I had no need to test that yet, so unfortunately, I can't tell.