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

  1. var sURL="mailto:user@domain.com?subject=<add%20your%20own%20subject%20line>";  
  2.   
  3.   var msgComposeService=  
  4.     Components.classes["@mozilla.org/messengercompose;1"]  
  5.     .getService(Components.interfaces.nsIMsgComposeService);  
  6.   
  7.   // make the URI  
  8.   var ioService =  
  9.     Components.classes["@mozilla.org/network/io-service;1"]  
  10.       .getService(Components.interfaces.nsIIOService);  
  11.   
  12.   aURI = ioService.newURI(sURL, nullnull);  
  13.   
  14.   // open new message  
  15.   msgComposeService.OpenComposeWindowWithURI (null, aURI);  

Modify Recipient List

Adding recipients:

  1. awAddRecipients(gMsgCompose.compFields, recipientType, emails);   

Removing recipients:

  1. 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

  1. 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:

  1. 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.

Examples

An example of intercepting a send event, altering the subject, altering headers, and altering the body.
  1. function send_event_handler( evt ) {  
  2.   var msgcomposeWindow = document.getElementById( "msgcomposeWindow" );  
  3.   var msg_type = msgcomposeWindow.getAttribute( "msgtype" );  
  4.   
  5.   // do not continue unless this is an actual send event  
  6.   if( !(msg_type == nsIMsgCompDeliverMode.Now || msg_type == nsIMsgCompDeliverMode.Later) )  
  7.     return;  
  8.   
  9.   // alter subject  
  10.   // you should save changes to both message composition fields and subject widget  
  11.   gMsgCompose.compFields.subject += " - foo";  
  12.   document.getElementById("msgSubject").value = gMsgCompose.compFields.subject;  
  13.     
  14.   // alter other composition fields/headers  
  15.   gMsgCompose.compFields.priority = "3";  
  16.   if( gMsgCompose.compFields.otherRandomHeaders != "" )  
  17.     gMsgCompose.compFields.otherRandomHeaders += "\n";  
  18.   gMsgCompose.compFields.otherRandomHeaders += "X-Suspected-spam: 82\n";  
  19.     
  20.   // alter body  
  21.   try {  
  22.     var editor = GetCurrentEditor();  
  23.     var editor_type = GetCurrentEditorType();  
  24.     editor.beginTransaction();  
  25.     editor.beginningOfDocument(); // seek to beginning  
  26.     if( editor_type == "textmail" || editor_type == "text" ) {  
  27.       editor.insertText( "foo" );  
  28.       editor.insertLineBreak();  
  29.     } else {  
  30.       editor.insertHTML( "<p>foo</p>" );  
  31.     }  
  32.     editor.endTransaction();  
  33.   } catch(ex) {  
  34.     Components.utils.reportError(ex);  
  35.     return false;  
  36.   }  
  37. }  
  38.   
  39. // could use document.getElementById("msgcomposeWindow") instead of window  
  40. 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:

gMsgCompose.RegisterStateListener(myStateListener);

Document Tags and Contributors

 Contributors to this page: groovecoder, jezdez, teoli, fscholz
 Last updated by: groovecoder,