1

I am using ShellExecute to invoke the local Email client and populate the mailto Address, Subject and Body in the Email Client. This is the code I have used...

  HdrBdy := 'mailto: support@positonstudio.co.uk' +
                '?Subject=Test Message Subject' +
                '&Body=Positron Studio ' + fmIDEAbout.lblIDEVersion.caption +' '+ fmIDEAbout.lblBuildTarget.caption + #13+
                 CompInfo.SystemManufacturer + ' Model No. ' + CompInfo.SystemProductName  +#13+
                 'Processor ' + CompInfo.ProcessorName + ' OS Version ' + OSInfo.Description  +#13+
                '----------------------------------------------------------------------------------';

  ShellExecute(Self.Handle,
               nil,
               PChar(HdrBdy),
               nil,
               nil,
               SW_NORMAL);

This works but the body text appears as a single line in the Email client. How can I insert multiple lines in the Mail client.

John Barrat
  • 810
  • 4
  • 15
  • what version of Delphi you are using? – Zam Mar 16 '21 at 18:02
  • 1
    `#13` is a carriage return. `#10` is a line feed. Depending on who is consuming the text, a line feed may be expected as the termination character for a line. – J... Mar 16 '21 at 18:03
  • Delphi 10.4.1 - I have tried all combinations of #10 #13, the text appears in the Client in a single line. – John Barrat Mar 16 '21 at 18:13
  • 3
    Needs to be URL encoded so ends up as `%0D%0A`. See: https://stackoverflow.com/questions/10356329/mailto-link-multiple-body-lines – Brian Mar 16 '21 at 18:17

1 Answers1

2

You are nearly there, you need to have both carriage return and linefeed.

So instead of +#13+ you need +#13+#10+ or you can use +#13#10+

I suspect that you will also then need to Percent Encode the string that's set to the Body value to pass it properly in the URL.

You can use TURLEncoding.Encode() for this.

HdrBdy := 'mailto: support@positonstudio.co.uk' +
                '?Subject=Test Message Subject' +
                '&Body='+TURLEncoding.Encode('Positron Studio ' + fmIDEAbout.lblIDEVersion.caption +' '+ fmIDEAbout.lblBuildTarget.caption + #13#10+
                 CompInfo.SystemManufacturer + ' Model No. ' + CompInfo.SystemProductName  +#13#10+
                 'Processor ' + CompInfo.ProcessorName + ' OS Version ' + OSInfo.Description  +#13#10+
                '----------------------------------------------------------------------------------');
Rob Lambden
  • 2,175
  • 6
  • 15
  • 1
    Instead of ``#13#10`` you can use the constant ``sLineBreak`` – Delphi Coder Mar 16 '21 at 21:34
  • @DelphiCoder sLineBreak is not guaranteed to be #13#10 on all platforms - in fact the docs say specifically that it's #10 on Linux. For normal mail messages I _think_ the RFCs say line endings _should_ be CRLF. It does seem as though the environment is Windows (ShellExecute) so I admit that the distinction is academic. – Rob Lambden Mar 17 '21 at 12:01