Skip to content

Instantly share code, notes, and snippets.

@nicocrm
Created February 16, 2011 04:09
Show Gist options
  • Save nicocrm/828850 to your computer and use it in GitHub Desktop.
Save nicocrm/828850 to your computer and use it in GitHub Desktop.
/*
* Copyright Strategic Sales Systems 2011
* Customization to write an email to the selected records on the mainview.
*/
define({
/**
* Open an email using Outlook automation, if available, falling back to mailto url otherwise.
* @param {Array|String} recipient email address (or addresses)
*/
writeMail: function (recipient, subject, body) {
if (typeof ActiveXObject != "undefined") {
try {
var outlook = new ActiveXObject("Outlook.Application");
var msg = outlook.CreateItem(0);
msg.Subject = subject;
msg.Body = body;
if (typeof recipient != "string") {
for (var i = recipient.length - 1; i >= 0; i--) {
msg.Recipients.Add(recipient[i]);
}
} else {
msg.To = recipient;
}
msg.Display();
return;
} catch (e) {
// probably they don't have the correct ActiveX setting.
// They need to have the site as trusted site AND customize the trusted site
// permissions to enable "ActiveX controls not marked as safe for scripting"
}
}
// separator is tricky. Most email clients will take either ";" or ",".
// Standard as per RFC is ",", but Outlook only accepts ";".
var url = this.addUrlComponents("mailto:", recipient, 900, ";", false, false);
url += "?subject=";
url = this.addUrlComponents(url, subject, 1024);
url += "&body=";
url = this.addUrlComponents(url, body, 2040);
location.href = url;
},
/**
* Add all strings in parts to str
* @param {String} str - current value we need to add to
* @param {Array} parts
* @param {Number} maxLength
* @param {String} (optional) separator. Note the separator is not url encoded.
* @param {bool} (optional) doEncode. Defaults to true. If false, strings won't be url encoded.
* @param {bool} (optional partialOk. Defaults to true. If false, only whole strings from the parts array will be included.
*/
addUrlComponents: function (str, parts, maxLength, separator, doEncode, partialOk) {
if (typeof parts == "string")
parts = [parts];
if (doEncode === false)
var encode = function (x) { return x };
else
var encode = function (x) { return encodeURIComponent(x) };
for (var i = 0; i < parts.length && str.length < maxLength; i++) {
var left = maxLength - str.length;
var part = parts[i];
if (i > 0 && separator)
left--;
if (left == 0 || (partialOk === false && left < part.length))
continue;
part = part.substr(0, left);
var ns = encode(part);
if (str.length + ns.length > maxLength) {
if (partialOk === false)
continue;
var right = 0;
var overflow = ns.length - left;
while (overflow > 0) {
right++;
overflow -= encode(part.charAt(part.length - right)).length;
}
if (right >= part.length)
continue;
ns = encode(part.substr(0, part.length - right));
}
if (i > 0 && separator)
str += separator;
str += ns;
}
return str;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment