Newer
Older
import("system.logging");
import("system.translate");
import("system.text");
import("system.db");
import("system.util");
import("Communication_lib");
import("DocumentTemplate_lib");
import("system.neon");
import("system.mail");
function EmailUtils () {}
/**
* creates a new E-Mail-Object and ask for a download of a eml where all fields are prefilled
* The eml can be open with a mailclient and sent via the mailclient. Each mailclient has a different behaviour:
* In Outlook the mail is automatically opened in draft-mode
* In Thunderbird the mail is opened in view mode and you've to manually "edit as new"
*
* @param {String|Array} pToRecipients mailaddresses of the recipients, can either be a 1D-Array with several addresses or a string with one address
* @param {String} pSenderContactId contactId of the sender. the standard mailadress of the contact is used as sender-address
* @param {String} [pTemplateId] if a document-template shall be used, give the templateId here
* @param {String} [pRecipientContactId] contactId of the recipient, required to fill placeholders
*/
EmailUtils.openMailTemplate = function (pToRecipients, pSenderContactId, pTemplateId, pRecipientContactId)
{
var email = new Email(pToRecipients);
if (pTemplateId)
email.setTemplate(pTemplateId, pRecipientContactId);
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
}
/**
* opens a view where a new mail can be sent. In the view the use CAN select a DocumentTemplate if needed
*
* @param {String} pToContactId contactId with contacts to filter the communication-addresses
*/
EmailUtils.openNewMail = function (pToContactId)
{
var params = {
"ContactId_param" : pToContactId
};
neon.openContext("Email", "EmailEdit_view", null, neon.OPERATINGSTATE_NEW, params);
}
/**
* object for handling emails
*
* @param {String|Array} [pToRecipients=[]] recipient email address or array of recipient email addresses
* @param {String} [pSender=null] email address of the sender
* @param {String} [pSubject=null] subject
* @param {String} [pBody=null] mail body
* @param {Array} [pCcRecipients=[]] array of recipient cc addresses
* @param {Array} [pBccRecipients=[]] array of recipient bcc addresses
*
* @class
*/
function Email (pToRecipients, pSender, pSubject, pBody, pCcRecipients, pBccRecipients)
{
if (pToRecipients && typeof(pToRecipients) == "string")
pToRecipients = [pToRecipients];
this.sender = pSender;
this.subject = pSubject;
this.body = pBody;
this.toRecipients = pToRecipients || [];
this.ccRecipients = pCcRecipients || [];
this.bccRecipients = pBccRecipients || [];
}
/**
* loads a document template into the mail body
*
* @param {String} pTemplateId the id of the template
* @param {String} pContactId the id of the template
*/
Email.prototype.setTemplate = function (pTemplateId, pContactId)
{
var template = DocumentTemplate.loadTemplate(pTemplateId);
var email = template.getReplacedEmailsByContactIds([pContactId])[pContactId];
this.sender = email.sender;
this.body = email.body;
this.subject = email.subject;
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
}
/**
* sets the sender of the mail
*
* @param {String} pContactId the contactId of the sender
*/
Email.prototype.setSender = function (pContactId)
{
this.sender = CommUtil.getStandardMail(pContactId);
}
/**
* generates a 'mailto:' URL from the email object
*/
Email.prototype.getMailtoUrl = function ()
{
var url = [];
if (this.toRecipients.length)
url.push("to=" + this.toRecipients.join());
if (this.ccRecipients.length)
url.push("cc=" + this.ccRecipients.join());
if (this.bccRecipients.length)
url.push("bcc=" + this.bccRecipients.join());
if (this.subject)
url.push("subject=" + this.subject);
if (this.body)
url.push("body=" + text.html2text(this.body));
url = "mailto:?" + url.join("&");
return encodeURI(url);
}
/**
* generates a eml-element from the email object
*/
Email.prototype.getRFCmail = function ()
{
var ENCODING = "UTF-8";
var mailId;
try
{
mailId = mail.newMail();
}
catch(ex)
{
//TODO: fix this dirty workaround [waiting for #1038963], since newMail causes an error on the first call after a user logged in
logging.log(ex);
util.sleep(1500);
mailId = mail.newMail();
}
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
if (this.toRecipients.length)
mail.addRecipients(mailId, mail.RECIPIENT_TO, this.toRecipients);
if (this.ccRecipients.length)
mail.addRecipients(mailId, mail.RECIPIENT_CC, this.ccRecipients);
if (this.bccRecipients.length)
mail.addRecipients(mailId, mail.RECIPIENT_BCC, this.bccRecipients);
if (this.subject)
mail.setSubject(mailId, this.subject, ENCODING);
if (this.body)
mail.addText(mailId, this.body, "text/html", ENCODING, null);
else
mail.addText(mailId, "", "text/html", ENCODING, null);
//"X-Unsent" is a very badly, non-standardised header to gently ask the mail client that the mail should open in a compose-mode
//this is mainly done for Microsoft Outlook for Windows.
//Thunderbird has a dinosaur-request (it's from the year 2002) to also support this: https://bugzilla.mozilla.org/show_bug.cgi?id=166541
mail.addHeader(mailId, "X-Unsent", "1");
//accoding to this entry: https://stackoverflow.com/questions/11330628/os-x-mail-open-eml-files-in-compose-mode/33224913
//something similar exists for OS X Mail
//X-Uniform-Type-Identifier: com.apple.mail-draft
//this could be added later if needed
var mailObj = mail.getCachedMail(mailId);
return mail.toRFC(mailObj);
}
/**
* opens the email
*/
Email.prototype.openMail = function ()
{
neon.openUrl(this.getMailtoUrl(), false);
}
/**
* ask for a download of the email
*/
Email.prototype.downloadEML = function()
{
neon.download(util.encodeBase64String(this.getRFCmail(), null), (this.subject || translate.text("Email Template")) + ".eml");
}
/**
* sends the email object
*
* @return {boolean} true, if the mail was sent sucessfully
*/
Email.prototype.send = function ()
{
var ENCODING = "UTF-8";
var mailId;
try
{
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
try
{
mailId = mail.newMail();
}
catch(ex)
{
//TODO: fix this dirty workaround [waiting for #1038963], since newMail causes an error on the first call after a user logged in
logging.log(ex);
util.sleep(1500);
mailId = mail.newMail();
}
if (this.toRecipients.length)
mail.addRecipients(mailId, mail.RECIPIENT_TO, this.toRecipients);
if (this.ccRecipients.length)
mail.addRecipients(mailId, mail.RECIPIENT_CC, this.ccRecipients);
if (this.bccRecipients.length)
mail.addRecipients(mailId, mail.RECIPIENT_BCC, this.bccRecipients);
if (this.subject)
mail.setSubject(mailId, this.subject, ENCODING);
if (this.body)
mail.addText(mailId, this.body, "text/html", ENCODING, null);
else
mail.addText(mailId, "", "text/html", ENCODING, null);