forked from steve-jansen/cordova-ios-emailcomposer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailComposer.js
242 lines (218 loc) · 14 KB
/
EmailComposer.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* @static
*
* A Cordova v2.0.0 compatible plugin for iOS devices to
* display the Mail.app new message composer view via the
* iOS MFMailComposeViewController type in the Message UI framework.
*/
var EmailComposer = {
/**
* Displays the Mail.app composer view using an optional array of arguments for the
* view such as recipients, subject, body, and attachments.
*
* **NOTE: ** while safe to call this method multiple times, subsequent invokations of the
* Mail.app composer view will be ignored by iOS until the first view is closed by the user.
* Consider using showEmailComposerWithCB to serialize display of multiple composer windows.
*
* @param {Object} [map] An argument map of details about the email message. Optional.
* @param {String} map.to The to recipients line, comma separated. Optional.
* @param {String} map.cc The carbon copy recipients line, comma separated. Optional.
* @param {String} map.bcc The blind carbon copy recipients line, comma separated. Optional.
* @param {String} map.from The profile email address to use for sending. Optional.
* @param {String} map.subject The subject line. Optional.
* @param {String} map.body The email body. Optional.
* @param {Boolean} map.isHtml Flag if the email body is HTML markup or plain text. Defaults to false.
* @param {Object[]} attachments An array of arguments maps describing file attachments for the message. Optional.
* @param {String} attachments.mimeType The well known mime type of the file attachment to create. Required when the data property is non-null. Ignored when the data property is null.
* @param {String} attachments.name The filename to display for the attachment in the mail message.
* Not related to the actual filename on disk, if any. Required for each attachment.
* @param {String} attachments.data File contents serialized as a string. Allows creation of attachments in memory. Optional.
* @param {Number} attachments.encoding The encoding type for the data property. Required when the data property is non-null. Must be a value in `EmailComposer.EncodingType`.
* @param {String} attachments.filePath The file system path for a file attach to a message.
* Ignored when the data property is non-null.
* The name property and the filePath property may be different values.
* @param {function} [onSuccess] Optional; the callback to notify upon successful completion of the mail composer view;
* sends a single parameter of type ComposeResult
* @param {function} [onError] Optional; the callback to notify upon error in the mail composer view;
* sends a single parameter of type string containing the CDVPluginResult value
*
* @returns {object}
* Returns the actual argument map passed to the native iOS plugin code. Useful for instrumenting what
* would have been sent to the iOS mail composer when developing in a desktop browser.
*
* @example
* EmailComposer.show({
* to: '[email protected]',
* cc: '[email protected]',
* bcc: '[email protected]',
* subject: 'Example email message',
* body: '<h1>Hello, world!</h1>',
* isHtml: true,
* attachments: [
* // attach a HTML file using a UTF-8 encoded string
* {
* mimeType: 'text/html',
* encoding: "UTF8",
* data: '<html><body><h1>Hello, World!</h1></body></html>',
* name: 'demo.html'
* },
* // attach a base-64 encoding of http://incubator.apache.org/cordova/images/cordova_128.png
* {
* mimeType: 'text/png',
* encoding: "Base64",
* data: 'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAANz0lEQVRoBe1Za4ycZRU+c5/Z3dnL7G4XarG2ikAIWhcwTQskqAEDTb3USIz6w4Qf9g8/jaihEf+AETUGAzGa0KhQ0WqxYJRoS4KtFChQ6W63e529dGdvszM79/v4PO835+u3s9vtEjX+8ZR33tt5z3vu7/kWkf/D/1YDrvdy/f79+z/74IMPfj0Wi83W63VhU1hvfqU9Xde+s7Oza3BwcOCxxx77jq5ttt+0AKFQqGVoaGisp6fnGhIn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3mmZFYP1gIwQlBgJqUV4mBKTGP8S4EejhZyMq8BYXgO6txED3NO7VRDeSWGoOFqGTTW/RiBeohfRjbSZdIq5atrJtAY+tm1QGvYCBsr4RnvN+DxDQZxNXYbMa9NzdSWuPTc41qZzZ8/xfxJ4lwqrdDnXNR3rnDj2+F8wF0y090NkiQAAAABJRU5ErkJggg==)',
* name: 'cordova.png'
* }
* // attach a file using a hypothetical file path
* //,{ filePath: './path/to/your-file.jpg' }
* ],
* onSuccess: function (winParam) {
* console.log('EmailComposer onSuccess - return code ' + winParam.toString());
* },
* onError: function (error) {
* console.log('EmailComposer onError - ' + error.toString()); }
* }
* });
*/
show: function show(map) {
var args,
onSuccessCallback,
onErrorCallback;
if (map != null) {
args = EmailComposer.sanitize(map);
onSuccessCallback = (typeof(map.onSuccess) === "function") ? map.onSuccess : undefined;
onErrorCallback = (typeof(map.onError) === "function") ? map.onError : undefined;
}
if (cordova != null && typeof(cordova.exec) === "function") {
cordova.exec(onSuccessCallback, onErrorCallback, "EmailComposer", "show", [args]);
}
return args;
},
/**
* @static
* @private
*
* Sanitizes the argument map before passing to the iOS plugin. This also ensures
* the expected argument keys exist for Objective-C key-value-coding.
*
* @param {Arguments} args an arguments instance to convert to an EmailMessage instance
* @returns {object}
*/
sanitize: function normalize(map) {
var newMap = {},
newAttachments = [],
attachment,
i,
length;
if (null == map)
return null;
newMap = {
to: map.to,
cc: map.cc,
bcc: map.bcc,
from: map.from,
subject: map.subject,
body: map.body,
isHtml: map.isHtml,
attachments: JSON.stringify([])
};
if (map.attachments && Array.isArray(map.attachments) && map.attachments.length > 0) {
for(i = 0, length = map.attachments.length; i < length; i++) {
attachment = map.attachments[i];
newAttachments.push({
mimeType: attachment.mimeType,
name: attachment.name,
data: attachment.data,
encoding: EmailComposer.translateEncoding(attachment.encoding),
filePath: attachment.filePath
});
}
newMap.attachments = JSON.stringify(newAttachments);
}
return newMap;
},
/**
* @static
* @private
*
* Transforms string arguments for the encoding into a `EncodingType` value
*
* @param a number or string value representing the encoding
* @returns {number}
*/
translateEncoding: function(encoding) {
var value;
if (!encoding) {
return encoding;
}
if (typeof(encoding) === "number") {
switch(encoding) {
case EmailComposer.EncodingType.Base64:
case EmailComposer.EncodingType.ASCII:
case EmailComposer.EncodingType.UTF8:
case EmailComposer.EncodingType.UTF16:
value = encoding;
break;
default:
value = null;
break;
}
}
else if (typeof(encoding) === "string") {
switch(encoding.trim().toLowerCase()) {
case "base64":
value = EmailComposer.EncodingType.Base64;
break;
case "ascii":
value = EmailComposer.EncodingType.ASCII;
break;
case "utf-8":
case "utf8":
value = EmailComposer.EncodingType.UTF8;
break;
case "utf-16":
case "utf16":
case "unicode":
value = EmailComposer.EncodingType.UTF16;
break;
default:
value = null;
break;
}
}
return value;
},
/**
* An enum of possible return codes passed as the only argument to the EmailMessage#onSucess callback.
* All values map to the iOS SDK MFMailComposeResult enum values */
ComposeResult: Object.freeze({
/** Per Apple's iOS SDK, "the email message was saved in the user’s Drafts folder." */
Cancelled: 0,
/** Per Apple's iOS SDK, "the user pressed the cancel button in the EmailComposer view and elected to save the message as a draft." */
Saved: 1,
/** Per Apple's iOS SDK, "the email message was queued in the user’s outbox. It is ready to send the next time the user connects to email." */
Sent: 2,
/** Per Apple's iOS SDK, "the email message was not saved or queued, possibly due to an error." */
Failed: 3,
/** An unknown MFMailComposeResult was returned */
Unknown: 4
}),
/**
* An enum of possible encoding types for window.plugins.emailComposer.EmailAttachment#data.
* All values except Base64 map to the iOS SDK NSStringEncoding enum values */
EncodingType: Object.freeze({
/** A base64 encoded string representation of a memory stream. Useful for attaching binary files, such as image files */
Base64: 0,
/** A string using ASCII text encoding. Useful for attaching ASCII text files. */
ASCII: 1,
/** A string using ASCII text encoding. Useful for attaching UTF-8 text files, such as most HTML documents created in memory. */
UTF8: 4,
/** A string using ASCII text encoding. Useful for attaching UTF-16 text files, such as most HTML documents created in memory.
* Equivalent to Unicode encoding on iOS devices*/
UTF16: 10
})
};
// detect if we are wrapped in a cordova.define call
if (module) {
module.exports = EmailComposer;
}