How to generate a From: header based on the To: address

This page describes a method for automatically generating a From: header in your outgoing email based on the addressees of the message. This is useful, for example, to generate a work address for mail to other work addresses while using a personal address for other email.

Most of the code here was written by Chris Keane.

This requires exmh 2.5 or later to function reliably. This code is a hook to the Simple Editor mail composer so users of alternate editors will find it difficult to use.

This is something of a "kit" and will require some editing to do what you want. It is left as an exercise for someone more motivated to make a nice preferences panel entry for this.

What does this do, exactly

The code below hooks Sedit during the message sending process. If the outgoing message doesn't have a From: header then the recipients are listed using the whom command. The recipients are checked against a domain name that you insert into the code (the "work domain"). If any recipients are in the "work domain" then a proc is called to insert a From header for your "work" address. If none match the "work domain" then a different proc is called to insert a From header for your personal address.

In addition there is a short proc at the end of the code that sets keys for Sedit to allow you to force a particular From: header into the message you're composing.

The steps to installing this are:

  1. Add some code to your exmh personal library
  2. Regenerate the tclIndex for your exmh personal library
  3. Remove any existing From: header from your various components files
  4. Restart exmh

Add some code to your exmh personal library

Insert the following code into your user.tcl in your exmh personal library directory filling in your information in the obvious places

 proc FixFromHeaderWork {draft t} {
    global sedit
    Exmh_Debug "FixFromHeaderWork"
    SeditSetHeader $t from {"Your Real Name" <you@yourwork.com>}
    SeditDirty $t
 }

 proc FixFromHeaderNotWork {draft t} {
    global sedit
    Exmh_Debug "FixFromHeaderNotWork"
    SeditSetHeader $t from {"Your Real Name" <you@notwork.org>}
    SeditSetHeader $t x-image-url {http://path.to.your/48x48face.gif}
    SeditDirty $t
 }

 proc Hook_SeditSendFixFromHeader {draft t} {

    # Force a save so "whom" has something to read
    Sedit_CheckPoint
    set start [$t search -nocase -regexp ^from: 1.0 header]
    # If not found then tweak one in
    if {[string compare "" $start] == 0} {
        # Check for Work recipients
        if {![catch {eval exec whom $draft} rcpts]} {
            # correct the output of some dubious
            # versions of `whom' in MH.
            regsub -all " at " $rcpts "@" rcpts
            # and now check to see whether the
            # yourwork.com domain is in there
            if {[regexp -nocase "yourwork\.com(\n|\$)" $rcpts]} {
                FixFromHeaderWork $draft $t
            } else {
                FixFromHeaderNotWork $draft $t
            }
        }
    }

    # if we've modified anything, we'll need to save the draft again
    if [SeditIsDirty $t] {
        set tmp_fmt ""
        catch {set tmp_fmt $sedit($t,format)}
        set sedit($t,format) Never
        SeditSave $draft $t {} 0
        set sedit($t,format) $tmp_fmt
    }
 }

 #This is optional and will give you keys to force the header
 proc Hook_SeditInit_FromKeys {d t} {
    bind $t <Meta-w> "FixFromHeaderNotWork $d $t"
    bind $t <Meta-W> "FixFromHeaderWork $d $t"
 }

Regenerate the tclIndex for your exmh personal library

Instructions can be found on the exmh personal library page

Remove any existing From: header from your various components files

If you have created custom components files (mail message templates for exmh) and have added the From: header you must remove it. The code is written to not alter an existing From: header. The affected files are any of:

Restart exmh and test

If you haven't already by this point you should exit exmh and start it back up again. This is necessary so that the new procs will be loaded.


Updated on 23 Sep 2004, 10:48 GMT
Search - Recent Changes - Reference - Index - Go to Beedub's Wiki - Help