Script To Send Email

17 views
Skip to first unread message

Thomas Passin

unread,
Nov 24, 2025, 1:34:17 PM (5 days ago) Nov 24
to leo-editor
Here's a script that lets you send the body of the selected node to email. The body needs to contain the text "email: <address>", where "<address>" is the email address to send the node to.  If there is a text selection, it will be searched for the address.  If not, the entire body will be searched. The script works for both Windows and Linux.  It ought to work on MacOS but I can't try that. The message is handled by the system default email handler. 

I have added this script to my "Local" custom menu defined in myLeoSettings.leo. Here it is:

@language python
"""Extract email address and launch email program on it.

The address is expected to be prefaced like this:
   
[optional text first] email: joe...@example.com [optional text after]

If there is a selection, the selected text is searched for an email
address.  Otherwise, the entire body is searched.  The system's
default email program is opened.
"""

import re
from sys import platform
import os
from subprocess import call

EMAILre = r'.*email:[ ]*([^@]+@[^@ \t\n]+)'  # inline

def open_file(filename):
    try:
        os.startfile(filename)  # Windows only
    except Exception as e:
        print(e)
        opener = "open" if platform == "darwin" else "xdg-open"
        call([opener, filename])

# List of line(s) containing cursor or selection
cursor_lines = c.getBodyLines()[1]
line = cursor_lines[0] if cursor_lines else ''

matched = re.match(EMAILre, line)
target = matched[1] if matched else None

if target:
    target = 'mailto:' + target.strip()
    open_file(target)
else:
    g.es('no address found')

jkn

unread,
Nov 24, 2025, 6:19:12 PM (5 days ago) Nov 24
to leo-editor
Nice - I have been meaning to write something myself like this myself, so thanks Thomas

    Jon N

jkn

unread,
Nov 24, 2025, 6:21:33 PM (5 days ago) Nov 24
to leo-editor
one reason I never got around to it is that in my setup at least, double-clicking on a link:

    mailto://myad...@mydomain.com

causes the browser to open my mail client similarly. So that is something similar...

    J^n

Thomas Passin

unread,
Nov 24, 2025, 11:23:00 PM (5 days ago) Nov 24
to leo-editor
Ha! I didn't think of trying that.  Control-click worked for me.  Tres cool. Double clicking didn't do anything; CTRL-Click opened my email client right directly.

jkn

unread,
Nov 25, 2025, 3:52:29 AM (5 days ago) Nov 25
to leo-editor
I meant CTRL-click, sorry.

Yes, it is nice that the 'mailto//' ... schema(?) works as well.

I have previously mentioned that the treatment of 'file://' is a bit iffy ... it does not work as expected in all cases (for me, under Linux). It would be nice to sort things out so that I could past a file link (preferably with spaces and non-ASCII characters) into Leo and have a CTRL-click do 'xdg-open' appropriately. I started looking into where the current limitations/non-conformities lie but it was a bit too much of a twisty maze of passages for me at the time.

    J^n
Reply all
Reply to author
Forward
0 new messages