Version 3 of Use Windows default mail client

Updated 2006-01-17 09:16:57

Windows XP has a clear notion of a default email client: it's shown at the top of the Start menu for example. You can use this to compose emails for the user to send, without knowing any of the details of the user's SMTP server configuration. The idea is to create a mailto: URL, and to hand the URL to the system to open. The user will see his or her familiar composition window appear. The rest is out of your hands: it is up to the user if he or she wishes to edit the message further and to send it.

  #-- Generate email
  set address "[email protected]"
  set subject "Use Windows default mail client"
  set body "Hello,\n\n"
  append body "Please think kindly of me"
  append body " for this small tip.\n\n"
  append body "Good luck!"

  #-- Create using default client
  eval exec [auto_execok start] mailto:$address?subject=[urlEncode $subject]^&body=[urlEncode $body]

There are a few points to note:

1. The procedure to encode the URL is almost lifted from the ncgi package in Tcllib but is modified so as to replace spaces with "%20", not with "+".

 proc urlEncode {string} {
  # 1 leave alphanumerics characters alone
  # 2 Convert every other character to an array lookup
  # 3 Escape constructs that are "special" to the tcl parser
  # 4 "subst" the result, doing all the array substitutions

  for {set i 1} {$i <= 256} {incr i} {
    set c [format %c $i]
    if {![string match \[a-zA-Z0-9\] $c]} {
      set map($c) %[format %.2X $i]
    }
  }

  # These are handled specially
  array set map {
    " " %20   \n %0D%0A
  }

  regsub -all -- \[^a-zA-Z0-9\] $string {$map(&)} string
  # This quotes cases like $map([) or $map($) => $map(\[) ...
  regsub -all -- {[][{})\\]\)} $string {\\&} string
  return [subst -nocommand $string]
 }

2. You can add "cc" and (usually) "bcc" recipients to the URL, in the part after the "?", separated by "&" from one another.

3. On the subject of the "&", note that the Windows XP command language requires it be escaped using the "^" character.

4. Also in Tcllib is the uri package. This contains tools nearly to construct the URL, but for the mailto: type, it is only familiar with the host and user parts, and not cc, bcc, subject and body.

Alastair Davies - 17 Jan 2006


Category Windows