Compose New Message

Open New Message Window

First create a new URI in "mailto" format; then use openComposeWindowWithURI to open a New Mail window.

Code Snippet

var sURL="<add%20your%20own%20subject%20line>";

  var msgComposeService=

  // make the URI
  var ioService =

  aURI = ioService.newURI(sURL, null, null);

  // open new message
  msgComposeService.OpenComposeWindowWithURI (null, aURI);

Modify Recipient List

Adding recipients:

awAddRecipients(gMsgCompose.compFields, recipientType, emails); 

Removing recipients:

awRemoveRecipients(gMsgCompose.compFields, recipientType, emails); 

where recipientType can be one of "addr_to", "addr_cc", "addr_bcc", "addr_reply"; emails is a string containing email addresses you want to add or remove, separated by comma.

Modify Header

Use nsIMsgCompFields.

Add Header

gMsgCompose.compFields.otherRandomHeaders += "X-Header-1: roflmao\n";

Modify Message Body

To modify the message body use the editor object of the current composition window. The convenience function to get this editor is GetCurrentEditor(). The documentation for the editor class is at nsIEditor. The Editor is interacted with slightly different ways depending on whether the message is "rich text" HTML or plain text. To determine the mode of the editor call the convenience function GetCurrentEditorType(). The mode will be either "text" or "textmail" for plain text emails and either "html" or "htmlmail" for mail composed in HTML.

Intercept Outgoing Message

After assembling all headers but before collecting the message body the composition window will emit a compose-send-message event. Note the event structure has no useful information attached. You can register a handler for this event as follows:

compose_window.addEventListener( "compose-send-message", your_send_event_handler, true );

Depending on your scope you may get to compose window in different ways. If you make a compose window overlay the compose window will simply be window in a global scope.


An example of intercepting a send event, altering the subject, altering headers, and altering the body.
function send_event_handler( evt ) {
  var msgcomposeWindow = document.getElementById( "msgcomposeWindow" );
  var msg_type = msgcomposeWindow.getAttribute( "msgtype" );

  // do not continue unless this is an actual send event
  if( !(msg_type == nsIMsgCompDeliverMode.Now || msg_type == nsIMsgCompDeliverMode.Later) )

  // alter subject
  // you should save changes to both message composition fields and subject widget
  gMsgCompose.compFields.subject += " - foo";
  document.getElementById("msgSubject").value = gMsgCompose.compFields.subject;
  // alter other composition fields/headers
  gMsgCompose.compFields.priority = "3";
  if( gMsgCompose.compFields.otherRandomHeaders != "" )
    gMsgCompose.compFields.otherRandomHeaders += "\n";
  gMsgCompose.compFields.otherRandomHeaders += "X-Suspected-spam: 82\n";
  // alter body
  try {
    var editor = GetCurrentEditor();
    var editor_type = GetCurrentEditorType();
    editor.beginningOfDocument(); // seek to beginning
    if( editor_type == "textmail" || editor_type == "text" ) {
      editor.insertText( "foo" );
    } else {
      editor.insertHTML( "<p>foo</p>" );
  } catch(ex) {
    return false;

// could use document.getElementById("msgcomposeWindow") instead of window
window.addEventListener( "compose-send-message", send_event_handler, true );


Get Access to the Compose Window before the Editing is Started

The best way to get access to the compose window after it is loaded but before editing has started, is to rely on ComposeFieldsReady.

Create your own state listener e.g.

var myStateListener = {
  NotifyComposeFieldsReady: function() {
  ... do what you want here ...;

  NotifyComposeBodyReady: function() {

  ComposeProcessDone: function(aResult) {

  SaveInFolderDone: function(folderURI) {


Then listen for the compose-window-init event and then register your listener there:


Tags (1)

Edit tags

Attachments (0)


Attach file