-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathemailService.js
97 lines (85 loc) · 4.5 KB
/
emailService.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
'use strict';
import angular from 'angular';
angular.module('emission.services.email', ['emission.plugin.logger'])
.service('EmailHelper', function ($window, $http, Logger) {
const getEmailConfig = function () {
return new Promise(function (resolve, reject) {
window.Logger.log(window.Logger.LEVEL_INFO, "About to get email config");
var address = [];
$http.get("json/emailConfig.json").then(function (emailConfig) {
window.Logger.log(window.Logger.LEVEL_DEBUG, "emailConfigString = " + JSON.stringify(emailConfig.data));
address.push(emailConfig.data.address)
resolve(address);
}).catch(function (err) {
$http.get("json/emailConfig.json.sample").then(function (emailConfig) {
window.Logger.log(window.Logger.LEVEL_DEBUG, "default emailConfigString = " + JSON.stringify(emailConfig.data));
address.push(emailConfig.data.address)
resolve(address);
}).catch(function (err) {
window.Logger.log(window.Logger.LEVEL_ERROR, "Error while reading default email config" + err);
reject(err);
});
});
});
}
const hasAccount = function() {
return new Promise(function(resolve, reject) {
$window.cordova.plugins.email.hasAccount(function (hasAct) {
resolve(hasAct);
});
});
}
this.sendEmail = function (database) {
Promise.all([getEmailConfig(), hasAccount()]).then(function([address, hasAct]) {
var parentDir = "unknown";
// Check this only for ios, since for android, the check always fails unless
// the user grants the "GET_ACCOUNTS" dynamic permission
// without the permission, we only see the e-mission account which is not valid
//
// https://developer.android.com/reference/android/accounts/AccountManager#getAccounts()
//
// Caller targeting API level below Build.VERSION_CODES.O that
// have not been granted the Manifest.permission.GET_ACCOUNTS
// permission, will only see those accounts managed by
// AbstractAccountAuthenticators whose signature matches the
// client.
// and on android, if the account is not configured, the gmail app will be launched anyway
// on iOS, nothing will happen. So we perform the check only on iOS so that we can
// generate a reasonably relevant error message
if (ionic.Platform.isIOS() && !hasAct) {
alert(i18next.t('email-service.email-account-not-configured'));
return;
}
if (ionic.Platform.isAndroid()) {
parentDir = "app://databases";
}
if (ionic.Platform.isIOS()) {
alert(i18next.t('email-service.email-account-mail-app'));
parentDir = cordova.file.dataDirectory + "../LocalDatabase";
}
if (parentDir == "unknown") {
alert("parentDir unexpectedly = " + parentDir + "!")
}
window.Logger.log(window.Logger.LEVEL_INFO, "Going to email " + database);
parentDir = parentDir + "/" + database;
/*
window.Logger.log(window.Logger.LEVEL_INFO,
"Going to export logs to "+parentDir);
*/
alert(i18next.t('email-service.going-to-email', { parentDir: parentDir }));
var email = {
to: address,
attachments: [
parentDir
],
subject: i18next.t('email-service.email-log.subject-logs'),
body: i18next.t('email-service.email-log.body-please-fill-in-what-is-wrong')
}
$window.cordova.plugins.email.open(email, function () {
Logger.log("email app closed while sending, "+JSON.stringify(email)+" not sure if we should do anything");
// alert(i18next.t('email-service.no-email-address-configured') + err);
return;
});
});
};
});