Recent

Author Topic: Opening a new message in default email Client  (Read 2315 times)

Badger

  • Full Member
  • ***
  • Posts: 144
Opening a new message in default email Client
« on: September 13, 2019, 11:13:18 am »
I want to be able to open a new email with multiple recipients in the user's default email client so they can send emails to a number of club members.  The operator adds the message then clicks send.

In the dim dark days when I used Delphi, this could be done in Windows with the ShellExecute command.  I am in the process of transporting this project to Lazarus and want to know if it is possible to do it easily across multiple operating systems.

I've looked at the Lazarus Forums and the last reference to this subject i could find was back in 2013 when it seemed rather complicated and not very reliable.  Has there been any advance in Lazarus since then?
« Last Edit: September 13, 2019, 11:16:17 am by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

krexon

  • Jr. Member
  • **
  • Posts: 80
Re: Opening a new message in default email Client
« Reply #1 on: September 13, 2019, 03:36:16 pm »
Sometimes I use below code to run something from command line. Try it :)
Code: Pascal  [Select][+][-]
  1.       AProcess:=TProcess.Create(nil);
  2.       AProcess.CommandLine:='insert_command_line_here';
  3.       AProcess.Options:=AProcess.Options+[poWaitOnExit];
  4.       AProcess.Execute;
  5.       AProcess.Free;

EDIT:
There is also ShellExecute in Lazarus. You need to add ShellApi in uses clause
https://wiki.freepascal.org/Executing_External_Programs
« Last Edit: September 13, 2019, 03:38:37 pm by krexon »

peardox

  • Jr. Member
  • **
  • Posts: 70
Re: Opening a new message in default email Client
« Reply #2 on: September 13, 2019, 05:26:17 pm »
A major issue here is that in many cases your users default email client will be Chrome / Firefox / IE etc

In general start with https://wiki.freepascal.org/Executing_External_Programs

That's got the simplistic OpenURL and OpenDocument as well as the far more flexible TProcess and derivatives (which is how you ShellExec across multiple platforms)

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Opening a new message in default email Client
« Reply #3 on: September 14, 2019, 05:07:27 am »
I tried using -  OpenURL('mailto:'+mailingList+'?subject=test&body=Hello World'); and this works fine on Windows Live Mail.

Is this approach likely to work for other email clients and cross platform?

Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

peardox

  • Jr. Member
  • **
  • Posts: 70
Re: Opening a new message in default email Client
« Reply #4 on: September 14, 2019, 10:59:35 am »
I can confirm that OpenURL works on Win10, Mac OSX + Linux - I used this in an app recently so know for a fact it works as I've tested on all 3 platforms.

BUT I also just confirmed that this don't work with a mailto link if you use GMail - blank web page

There's something called a registered protocol handler (definitely in Windows, likely in Mac + Linux - I can check if anyone's dying to know). The protocol handlers take the 'scheme' and choose what to use to open that 'document'

https://forum.lazarus.freepascal.org => https = scheme //forum.lazarus.freepascal.org = document
mailto:fred@another.email => mailto = scheme fred@another.email = document

Obviously with URL encoding allowing for multiple parts to be specified in the document

In the same section of the page linked above you'll see them going on about ...

OpenDocument('manual.pdf'); 

This opens based on file association and the 'We could also do this with TProcess' bit allows for a mix + match approach - the Open... functions are wrappers around TProcess it says (somewhere)

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Opening a new message in default email Client
« Reply #5 on: September 15, 2019, 10:11:26 am »
I've heard of the difficulty with GMail before.  Is there any way round it?
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

 

TinyPortal © 2005-2018