-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenOutlook.js
181 lines (160 loc) · 5.5 KB
/
openOutlook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// openOutlook.js - revision 3 (2011-04-04 / [email protected])
//
// Read in the XML file given on the command line and use it to open a new
// preformatted e-mail in Outlook, ready to be sent. Supports full HTML
// content in the message body, with embedded images and other attachments.
//
// This file is placed in the public domain. Feel free to use it for anything
// you like. There is no warranty - if it breaks you get to keep both pieces.
//
// 2007-09-11 / [email protected]: Initial version
// 2009-07-07 / [email protected]: Set message body to UTF-8
// 2011-04-04 / [email protected]: Remove CDO code to hide image
// attachments, seems no longer req'd and now works with Outlook 2010
//
// Usage:
//
// openOutlook.js data.xml
//
// Where data.xml looks like:
//
// <mail>
// <to>Adam Nielsen</to>
// <to>John Smith</to>
// <cc>Someone else</cc>
// <bcc>Bob Jones</bcc>
// <subject>Test message</subject>
// <body href="msg.html"/>
// <attachment cid="cid:1" href="example.jpg">Embedded image</attachment>
// </mail>
//
// All fields are optional. The "cid" can be used with attachments to link to
// embedded images from within the message body, like <img src="cid:1" />
//
// The message body can also be specified inline:
//
// <body>Plain text message</body>
//
// And the XML file can also be specified in the strFilename variable below.
//
// Note that angle brackets still need to be escaped as < and > in the
// message body.
//
// This script is a text file, so make sure it uses DOS (CRLF) line endings or
// it will not run.
//
// This has been tested with Outlook 2007 and Outlook 2010.
//
// If you don't want to specify the XML filename on the command line, you can
// hard code it here. The reason for it saying "datafile" at the moment is
// because in our environment JavaScript code running through XULRunner
// overwrites this placeholder with the path to the XML file before launching
// the script.
var strFilename = "$DATAFILE;";
// Globals
var olMailItem = 0;
var olFormatHTML = 2;
var olByValue = 1;
var olSave = 0;
// --- BEGIN MESSAGEBOX DEFINITION ---
var MB_OK = 0;
var MB_OKCANCEL = 1;
var MB_ABORTRETRYIGNORE = 2;
var MB_YESNOCANCEL = 3;
var MB_YESNO = 4;
var MB_RETRYCANCEL = 5;
var MB_ICONERROR = 16;
var MB_ICONQUESTION = 32;
var MB_ICONEXCLAMATION = 48;
var MB_ICONINFORMATION = 64;
var MB_FOCUS_BTN0 = 0;
var MB_FOCUS_BTN1 = 256;
var MB_FOCUS_BTN2 = 512;
var MB_RET_OK = 1;
var MB_RET_CANCEL = 2;
var MB_RET_ABORT = 3;
var MB_RET_RETRY = 4;
var MB_RET_IGNORE = 5;
var MB_RET_YES = 6;
var MB_RET_NO = 7;
function MessageBox(strMessage, strTitle, iButtons)
{
var sh = WScript.CreateObject("WScript.Shell");
sh.Popup(strMessage, 0, strTitle, iButtons);
}
// --- END MESSAGEBOX DEFINITION ---
// Script parameters
if (WScript.Arguments.Count() != 1) { // no parameter
if (strFilename.substr(0, 1) == '$') { // no inline replacement
// No data file supplied
MessageBox("Usage: openOutlook data.xml", "Missing Parameter", MB_OK | MB_ICONEXCLAMATION);
WScript.Quit();
} // else continue on with inline replacement
} else {
strFilename = WScript.Arguments.Item(0);
}
var doc = new ActiveXObject("msxml2.DOMDocument.3.0");
doc.async = false;
doc.resolveExternals = false;
doc.validateOnParse = false;
if (!doc.load(strFilename)) {
MessageBox("Unable to load " + strFilename +
"\n\nReason: " + doc.parseError.reason, "Error", MB_OK | MB_ICONERROR);
WScript.Quit();
}
function joinNodes(nodeList, delim)
{
if (nodeList.length == 0) return "";
var str = nodeList[0].text;
for (var i = 1; i < nodeList.length; i++) {
str += delim + nodeList[i].text;
}
return str;
}
var ol = WScript.CreateObject("Outlook.Application");
var msg = ol.CreateItem(olMailItem);
msg.InternetCodePage = 65001; // UTF-8
var nodeTo = doc.selectNodes("//mail/to");
if (nodeTo) msg.To = joinNodes(nodeTo, "; "); // Semicolon-delimited list of recipients
var nodeCC = doc.selectNodes("//mail/cc");
if (nodeCC) msg.CC = joinNodes(nodeCC, "; ");
var nodeBCC = doc.selectNodes("//mail/bcc");
if (nodeBCC) msg.BCC = joinNodes(nodeBCC, "; ");
var nodeSubject = doc.selectSingleNode("//mail/subject");
if (nodeSubject) msg.Subject = nodeSubject.text;
var nodeAttachments = doc.selectNodes("//mail/attachment");
if (nodeAttachments.length > 0) {
var strCIDs = new Array();
for (var i = 0; i < nodeAttachments.length; i++) {
var strHRef = nodeAttachments[i].getAttribute("href");
if (strHRef == null) {
MessageBox("<attachment/> tag has no href attribute!", "XML Error", MB_OK | MB_ICONERROR);
WScript.Quit();
}
var att = msg.Attachments.Add(strHRef, olByValue, 1, nodeAttachments[i].text);
att = null; // necessary! (for garbage collection)
strCIDs[i] = nodeAttachments[i].getAttribute("cid");
}
// We have to release the attachment objects fully, otherwise the changes won't be saved
CollectGarbage(); // release all the objects we've assigned null to
}
var nodeBody = doc.selectSingleNode("//mail/body");
if (nodeBody) {
if (nodeBody.text != "") msg.Body = nodeBody.text;
else {
var strBodyRef = nodeBody.getAttribute("href");
if (strBodyRef != "") {
// The body is stored in an external file, so load that
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8;
var f = fso.OpenTextFile(strBodyRef, FOR_READING); // US-ASCII
var strHTML = f.ReadAll();
f.close();
msg.BodyFormat = olFormatHTML;
msg.HTMLBody = strHTML; // "<code>" + strBodyRef + "</code>";
}
}
}
msg.Close(olSave);
msg.Display();