Skip to content

Pass Email Response Templates With Text or Rich HTML Text to Outlook From a Trusted Web Site Using ActiveX Controls

by on August 6, 2014

I have successfully used this in SharePoint as well as simpler web sites. It can eliminate the need for complex form creation on the web end for administration purposes, pulling in templates from a secondary linked list.

  1. Make sure the site is added to trusted (or intranet) sites in IE
  2. Make sure settings  for those sites under the custom button allow ActiveX for the sites in the list and that prompts are disabled (optional)
  3. Create a link similar to the below format
    1. Option 1: simple text, use a custom attribute on the link/button
    2. Option 2: rich/html text, use a hidden div to hold the HTML
    3. Option 3 (not shown): have a text box that allows either of these and use that content to pass to Outlook

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
</head>

<body>

</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>

$( document ).ready(function() {

$( "a.emailbutton" ).click(function() {
//alert(‘this is working’);
try {

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

    //add some recipients
    email.Recipients.Add(‘user1@company.com’).Type = 1; //1=To
    email.Recipients.Add(‘user2@company.com’).Type = 2; //2=CC
    email.Recipients.Add(‘user2@company.com’).Type = 3; //2=BCC

    //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;

    //Get the html body of the email from custom attribute
    var passedBody = $(this).attr(’emailbody’);
    //Get the html rich text body from a div – hide using display:none;
    var passedBodyHTML = $(this).next(‘div.emailbodydiv’).html();
    //set the html body of the email
    email.HTMLBody = beforeSignature + ‘<h1>Our Custom Body</h1>’ + passedBody + passedBodyHTML; //+ signature;

//add From email
                    var mycount = accounts.Count;
                    //alert(mycount);
                    //alert(accounts.Item(2).SmtpAddress);
                    for (var i=0; i < mycount; i++) {
                        var myemail = accounts.Item(i+1);
                        //alert("The email is " + myemail + (i+1));
                        if (myemail !=’testing@mysite.com’) {continue;}
                        //alert(i+1);
                        email.SendUsingAccount = accounts.Item(i+1);
                    }

} catch(ex) {
    //something went wrong
    alert(‘There was an error opening the email application, please contact Infrastructure.’);
}

});

});</script>

Hello <a class="emailbutton" href="#" emailbody="This is my simple email body, rich text and HTML have to be encoded or escaped properly.">link</a>
<div class="emailbodydiv" style=”display:none;”>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum iaculis urna mauris, a eleifend nisi sollicitudin ut. In hac habitasse platea dictumst. Pellentesque fermentum mauris metus, eget laoreet diam scelerisque quis. Aliquam egestas lacinia justo, vitae congue libero semper ac. Mauris feugiat sit amet eros eu scelerisque. Maecenas sem nisi, varius a aliquet eu, auctor non eros. Etiam ultrices eget lectus vitae tincidunt. Quisque eget dui ornare, congue magna sed, sagittis erat. Proin ante ante, fermentum sed elit et, congue facilisis dolor. Sed a imperdiet sem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean ultricies, justo adipiscing condimentum sodales, ante augue porttitor orci, condimentum rutrum erat eros et nisl. Praesent condimentum suscipit lectus, vitae posuere lorem egestas eu.
</p>
<p>Maecenas tincidunt est et tempor blandit. In tempor enim in imperdiet luctus. Vestibulum at volutpat enim. Nunc sagittis feugiat orci sit amet sagittis. Morbi facilisis nibh vel dui interdum viverra. Vestibulum vel volutpat elit. Suspendisse tellus ante, volutpat ut elit quis, fermentum pretium mi. Aenean et quam eu ante viverra aliquet ut sed justo. Morbi ut metus eleifend, vehicula urna a, egestas lorem. Vestibulum aliquet sagittis viverra. Nunc iaculis, justo a vulputate ullamcorper, elit odio scelerisque quam, sed lacinia diam arcu at urna. In hac habitasse platea dictumst. Aliquam leo ante, euismod ut nisi sed, feugiat sodales risus. Proin a pharetra lectus.
</p>
<p>Donec vel venenatis lorem. Vestibulum condimentum eget quam sed ullamcorper. Donec at nisi ac ligula placerat feugiat id sit amet metus. Mauris aliquet nec lorem vel commodo. Praesent ut sem metus. Duis at venenatis nunc. Nulla consectetur ut libero eget posuere. Nunc pulvinar, dui in pharetra egestas, augue augue hendrerit nunc, vitae hendrerit est tellus a nibh. Fusce metus sem, ultrices a ipsum sodales, vulputate interdum nulla. Praesent ut sagittis dui. Nullam eu dui ut quam imperdiet aliquam. Vestibulum non tempor diam, quis laoreet eros.
</p>
<p>Phasellus nec diam eros. Vivamus adipiscing diam lacus, eget porttitor lorem ultrices nec. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vivamus vitae risus nec mi euismod tincidunt sed at ante. Aliquam molestie vel nisl vel porttitor. Nulla facilisi. Vivamus aliquam at sem a ullamcorper. Praesent iaculis nec purus vitae feugiat. Sed interdum augue velit, a laoreet ipsum pharetra eu. Pellentesque porta diam sapien, a viverra diam ultrices a. Quisque quis convallis tortor. Mauris condimentum scelerisque tempus. Maecenas ultrices nibh eget placerat tempus. Integer commodo lectus sit amet leo tempor, ut dictum velit rhoncus. Vivamus posuere, purus vitae gravida pharetra, tellus felis ullamcorper nunc, eu faucibus tortor lectus a quam.
</p>
<p>Integer tincidunt turpis adipiscing, aliquam velit ac, accumsan justo. Mauris imperdiet cursus lacus, vel dignissim justo bibendum a. Pellentesque arcu lorem, laoreet et justo ut, viverra tincidunt elit. Fusce nec mauris nulla. Cras dictum dolor urna. Nulla condimentum sit amet quam ac cursus. Mauris id rutrum nulla. Donec ultrices, magna id tincidunt adipiscing, nunc mauris sodales augue, eget euismod magna velit quis nisi. Nam lacinia lacinia metus vel posuere. Integer pellentesque enim vitae faucibus eleifend. Maecenas quis pharetra nulla, hendrerit condimentum libero. In molestie eget ipsum ut ornare. Curabitur accumsan nisi in orci scelerisque, eget egestas tellus varius.</div>
</p>
</div>
</html>

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: