diff --git a/process/Email_lib/process.js b/process/Email_lib/process.js index a917c8b44961e2aaca14a3f5807b6e31cfb3918b..b7fc1638e96517b804b94c37b01b44fc6b3dd7dd 100644 --- a/process/Email_lib/process.js +++ b/process/Email_lib/process.js @@ -1,10 +1,10 @@ +import("system.translate"); import("system.text"); import("system.db"); import("system.util"); import("Communication_lib"); import("system.neon"); - - +import("system.mail"); function EmailUtils () {} @@ -14,7 +14,8 @@ EmailUtils.openMailTemplate = function (pToRecipients, pContactId, pTemplateId) email.setSender(pContactId); if (pTemplateId) email.setTemplate(pTemplateId); - email.openMail(); + + email.downloadEML(); } EmailUtils.openNewMail = function (pToContactId) @@ -58,12 +59,13 @@ function Email (pToRecipients, pSender, pSubject, pBody, pCcRecipients, pBccReci Email.prototype.setTemplate = function (pTemplateId) { var alias = "_____SYSTEMALIAS"; - var binaryId = db.cell(SqlCondition.begin(alias) - .andPrepare("ASYS_BINARIES.ROW_ID", pTemplateId) - .buildSql("select ID from ASYS_BINARIES", "1=2"), alias - ); + var templateDocument = db.getBinaryMetadata("DOCUMENTTEMPLATE", "DOCUMENT", pTemplateId, false, alias, null); + if (!templateDocument[0]) + return null; + var binaryId = templateDocument[0][db.BINARY_ID]; if (binaryId) - this.body = text.html2text(util.decodeBase64String(db.getBinaryContent(binaryId, alias))); + this.body = util.decodeBase64String(db.getBinaryContent(binaryId, alias)); + return null; } /** @@ -96,17 +98,61 @@ Email.prototype.getMailtoUrl = function () url.push("subject=" + this.subject); if (this.body) - url.push("body=" + 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 = "UTF8"; + var 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); + + //"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); -} \ No newline at end of file +} + +Email.prototype.downloadEML = function() +{ + neon.download(util.encodeBase64String(this.getRFCmail(), null), (this.subject || translate.text("Email Template")) + ".eml"); +} + \ No newline at end of file