I was developing an Internal application and need to send mail using local Outlook profile but something that allow HTML formatted contents. I ran into a easy Javascript option to do it as follows:
Prerequisite:
Prerequisite:
- The Client User machine should have Outlook installed and should have an Outlook profile.
Note:
- This will work only in IE (Internet Explorer) browser, So it is suitable only for Internal Application.
- The Browser setting should allow ActiveX object creation or the User will be prompted to allow the access.
<html>
<script>
function SendMailVarBased(emailid, subject, htmlBody) {
try {
//get outlook and create new email
var outlook = new ActiveXObject('Outlook.Application');
var email = outlook.CreateItem(0);
//add some recipients
email.Recipients.Add(emailid).Type = 1; //1=To
//email.Recipients.Add('user2@company.com').Type = 2; //2=CC
//subject and attachments
email.Subject = 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 + htmlBody + signature;
} catch (ex) {
alert(ex.message)
}
}
SendMailVarBased('rahul8346@gmail.com', 'hi there','<html><h1>Hi there</h1></html>');
</script>
</html>
<script>
function SendMailVarBased(emailid, subject, htmlBody) {
try {
//get outlook and create new email
var outlook = new ActiveXObject('Outlook.Application');
var email = outlook.CreateItem(0);
//add some recipients
email.Recipients.Add(emailid).Type = 1; //1=To
//email.Recipients.Add('user2@company.com').Type = 2; //2=CC
//subject and attachments
email.Subject = 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 + htmlBody + signature;
} catch (ex) {
alert(ex.message)
}
}
SendMailVarBased('rahul8346@gmail.com', 'hi there','<html><h1>Hi there</h1></html>');
</script>
</html>
No comments:
Post a Comment