Open new email in outlook with html body from IE web site

15,288

This can be achieved using JavaScript in IE if the site is a Trusted Site and ActiveX objects are enabled. I have had this script work as far back as IE6 and tested up to IE10 I am unsure about its support in IE11.

An important point about the script below is that you must call Display on the email before trying to extract the signature from it or trying to set its HTMLBody otherwise you will lose the signature information.

try {

    //get outlook and create new email
    var outlook = new ActiveXObject('Outlook.Application');
    var email = outlook.CreateItem(0);

    //add some recipients
    email.Recipients.Add('[email protected]').Type = 1; //1=To
    email.Recipients.Add('[email protected]').Type = 2; //2=CC

    //subject and attachments
    email.Subject = 'A Subject';
    //email.Attachments.Add('URL_TO_FILE', 1); //1=Add by value so outlook downloads the file from the url

    // display the email (this will make the signature load so it can be extracted)
    email.Display();

    //use a regular expression to extract the html before and after the signature
    var signatureExtractionExpression = new RegExp('/[^~]*(<BODY[^>]*>)([^~]*</BODY>)[^~]*/', 'i');
    signatureExtractionExpression.exec(email.HTMLBody);
    var beforeSignature = RegExp.$1;
    var signature = RegExp.$2;

    //set the html body of the email
    email.HTMLBody = beforeSignature + '<h1>Our Custom Body</h1>' + signature;

} catch(ex) {
    //something went wrong
}
Share:
15,288

Related videos on Youtube

David Ewen
Author by

David Ewen

I am currently the Senior Solutions Architect at APE Mobile specializing in applying AI technologies to the construction site. I am also the Founder of Guava Development a Software Services Company located in Perth, Western Australia dedicated to improving productivity and reducing costs through the targeted and innovative application of software assisted workflows and packages. I have been working in the industry for 15 years. My day job usually involves programming with C# but I have been known to mess around with just about everything.

Updated on September 14, 2022

Comments

  • David Ewen
    David Ewen over 1 year

    I am working in a corporate environment on an internal use web application and have a requirement to generate an email in the users Outlook retaining their signature so that they can then modify if required and send it themselves.

    All users are on IE8+ and the site is a part of Trusted Sites with ActiveX objects enabled so I was hoping to use outlook automation to achieve this.

    Here is a quick summary of my requirements to differentiate this from existing questions.

    • Only needs to support IE8+ and Outlook
    • HTML body formatting support
    • Attachment support
    • Must retain the users configured signature
  • Yulian
    Yulian about 10 years
    It worked for me too. I only had to enable the 'Initialize and script ActiveX control not marked as safe.' option in the IE security options.
  • Yasin Yörük
    Yasin Yörük over 7 years
    giving an error. Outlook not starting If outlook is running before the button clicked, it giving error too. Two instance of outlook is not running at the same time. So I don't know how to open compose mail window.
  • Martin O Leary
    Martin O Leary about 7 years
    hey guys, is there any way to include an image in the body of the email using this method? I am using this in IE11 to create a report and currently require people to copy and paste an image into the body - can this be done through just javascript?