Skip to content

Instantly share code, notes, and snippets.

@romgrk romgrk/send_email.js
Last active May 5, 2017

Embed
What would you like to do?
/** Sends an email.
* Usage:
var message = {
server: 'smtp.office365.com',
port: 25, // Don't include the port if you are targeting office365
username: 'docrequest@lordco.com',
password: 'secret',
usessl: true, // Set to true if you are targeting office365
headers: { 'X-Custom': 'value' }
from: 'docrequest@lordco.com',
to: 'gregoirer@ca.objectiflune.com',
isHighPriority: true,
attachments: ['c:/absolute/path/to/filename.txt'],
subject: 'Statement',
body: '<b>Yo</b><br>This is a new statement'
}
sendEmail(message);
*/
function sendEmail(options) {
// Setup configuration
var schema = 'http://schemas.microsoft.com/cdo/configuration/';
var config = new ActiveXObject('CDO.Configuration');
config.Fields.Item(schema + 'sendusing') = options.sendUsing || 2;
config.Fields.Item(schema + 'smtpserver') = options.server;
if (options.port != undefined)
config.Fields.Item(schema + 'smtpserverport') = options.port;
if (options.usessl != undefined)
config.Fields.Item(schema + 'smtpusessl') = options.usessl;
if (options.username && options.password) {
config.Fields.Item(schema + 'smtpauthenticate') = 1
config.Fields.Item(schema + 'sendusername') = options.username;
config.Fields.Item(schema + 'sendpassword') = options.password;
}
config.Fields.Update();
// Setup message
var message = new ActiveXObject("CDO.Message");
message.Configuration = config;
message.From = options.from;
message.To = options.to;
message.Subject = options.subject;
message.HTMLBody = options.body;
if (options.isHighPriority) {
/* 0 = Low, 1 = Normal, 2 = High */
message.Fields.Item('urn:schemas:mailheader:X-MSMail-Priority') = 'High'
message.Fields.Item('urn:schemas:mailheader:X-Priority') = 2
message.Fields.Item('urn:schemas:httpmail:importance') = 2
}
if (options.headers) {
for (var key in options.headers) {
var value = options.headers[key];
message.Fields('urn:schemas:mailheader:' + key) = value;
}
}
if (options.attachments) {
for (var k in options.attachments) {
var attachment = options.attachments[k];
message.AddAttachment(attachment);
}
}
message.Fields.Update();
message.Send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.