Jump to content
Sign in to follow this  

Create mail message with default mailto: handler

Recommended Posts


I searched the forum for a script that would create an e-mail message in the default mailto: handler, but could only find one that opened the default e-mail client, which is sometimes a different program. I wanted to have a script that would take an e-mail address and create a message in the mail client that opens on my system when I click a mailto: link.

So I modified the existing code I found in another thread, by Brett Francis, and came up with this. It works in Thunderbird; I think it should work in Outlook. I don't know about any other mail client. I'd like to find a generic way to extend it to work with a subject line as a parameter, but I don't think that can be done in a way that works in any client.

I hope this code might be useful to someone else, and will be grateful for any improvements:

$to = "someone@somewhere.com"

; Function Name:    _LaunchDefaultMailtoHandler()
; Description:      Open default handler for Mailto url
; Parameter(s):     $to = a valid e-mail address (someone@somewhere.com)
; Requirement(s):   None
; Return Value(s):  On Success - Launch default mailto: handler with message to address
;                   On Failure - Returns:
;                1-Reading of the registry Failed
;                2-Getting the Path Of the Default Mail Client Failed
;                3-Run Failed
; Author(s):    Brett Francis <francisb[at]student[dot]jpc[dot]qld[dot]edu[dot]au>
;       Modified for mailto: handler by Edward Mendelson
; Thanks:   GaryFrost
;       Zedna
;       MrCreator
; Note(s):  Tested only with Thunderbird

Func _LaunchDefaultMailtoHandler($to)
    Local $qt = Chr(34)
    Local $space = " "
    Local $Old_Opt_EES = Opt("ExpandEnvStrings", 1)
    Local $iRetError = 0, $iRetValue = 0
    Local $ReadDefMailClient, $ReadCommand, $ReadExe

    $ReadCommand = RegRead("HKCR\mailto\shell\open\command", "")
    If @error Or $ReadCommand = "" Then $iRetError = 1

    If $iRetError = 0 Then
        $ReadExe = $ReadCommand
        If StringRegExp($ReadExe, '"(.*)"') Then _
                $ReadExe = StringMid($ReadExe, 2, StringInStr($ReadExe, '"', 0, 2) - 2)
        If StringInStr($ReadExe, '/') Then _
                $ReadExe = StringLeft($ReadExe, StringInStr($ReadExe, '/', 0, -1))

    If StringRight($ReadCommand, 5) = (" " & $qt & "%1" & $qt) Then _
        $ReadCommand = StringTrimRight($ReadCommand, 5)

    If $iRetError = 0 Then
        $ReadExe = StringStripWS($ReadExe, 3)
        If Not FileExists($ReadExe) Then $iRetError = 2

    If $iRetError = 0 Then
        $tostring = ""
        If Not $to = "" Then $tostring = $space & "mailto:" & $to
        Local $iRetValue = Run($ReadCommand & $tostring)
        If @error Then $iRetError = 3

    Opt("ExpandEnvStrings", $Old_Opt_EES)
    Return SetError($iRetError, 0, $iRetValue)
EndFunc   ;==>_LaunchDefaultMailtoHandler
Edited by emendelson

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.