Awaken Scripting Social Media Integration Guide

Please email helpdesk@awaken.io for omissions or errors.
×
Menu

Managing Emails with JavaScript

 

Script.Email

These functions allow the management of emails in a Workflow popped from the Social Dashboard. All of the functions act upon or retrieve data from the currently popped Email.
 
All of the functions feature an optional "callback" function that can be included to execute further code when the function has either successfully completed or failed. These won't be specifically mentioned in each function, but are always the final parameter.
 
The functions below are grouped into three rough classes: Manage, Get..., and Search.
 
 

Manage

These functions allow the "typical" actions that are conducted upon either the currently popped email, or a specified email.
 
Name
Description
Code
Script.Email.Delete
Deletes the currently popped email from the source mailbox. The email is completely deleted, and cannot be recovered.
Script.Email.Delete(function(response){
     if(response.Success) {
          Script.Toast.Success("Success","Email has been deleted");
     }
     else {
          Script.Toast.Error("Failure", response.ErrorMessage);
     }
});
Script.Email.DeleteItem
Deletes the specified email from the specified mailbox. The email is completely deleted, and cannot be recovered.
 
The first parameter is the ID of the email to be deleted.
The second parameter is the connector to be used to access the email to be deleted.
Script.Email.DeleteItem("abc.123.example.com", "Example Connector", function(response){
     if(response.Success) {
          Script.Toast.Success("Success","Email has been deleted");
     }
     else {
          Script.Toast.Error("Failure", response.ErrorMessage);
     }
});
Script.Email.Forward
Forwards the currently popped email from the source inbox.
 
The first parameter is the recipient(s) for the forwarded email.
The second parameter is to optionally specify the Email (Outgoing) connector to be used. If none is specified or this is left blank, then it will use the main application settings.
The third parameter is to optionally specify the replyFrom address to be used. If none is specified or this is left blank, then it will use the chosen connector or main application's settings.
Script.Email.Forward("test@example.com", "Example Email (Outgoing)", "", function(response){
     if(response.Success) {
          Script.Toast.Success("Success","Email has been sent");
     }
     else {
          Script.Toast.Error("Failure", response.ErrorMessage);
     }
});
Script.Email.MarkAsRead
Marks the currently popped email from the source inbox as read.
Script.Email.MarkAsRead(function(response){
     if(response.Success) {
          Script.Toast.Success("Success","Email has been marked as read");
     }
     else {
          Script.Toast.Error("Failure", response.ErrorMessage);
     }
});
Script.Email.MarkItemAsRead
Marks the specified email from the specified mailbox as read.
 
The first parameter is the ID of the email to be marked as read.
The second parameter is the connector to be used to access the email to be marked as read.
Script.Email.MarkItemAsRead("abc.123.example.com", "Example Connector", function(response){
     if(response.Success) {
          Script.Toast.Success("Success","Email has been marked as read");
     }
     else {
          Script.Toast.Error("Failure", response.ErrorMessage);
     }
});
Script.Email.MarkAsUnread
Marks the currently popped email from the source inbox as unread.
Script.Email.MarkAsUnread(function(response){
     if(response.Success) {
          Script.Toast.Success("Success","Email has been marked as unread");
     }
     else {
          Script.Toast.Error("Failure", response.ErrorMessage);
     }
});
Script.Email.MarkItemAsUnread
Marks the specified email from the specified mailbox as unread.
 
The first parameter is the ID of the email to be marked as unread.
The second parameter is the connector to be used to access the email to be marked as unread.
Script.Email.MarkItemAsUnread("abc.123.example.com", "Example Connector", function(response){
     if(response.Success) {
          Script.Toast.Success("Success","Email has been marked as unread");
     }
     else {
          Script.Toast.Error("Failure", response.ErrorMessage);
     }
});
Script.Email.Send
Allows sending an email via JavaScript.
 
The function is documented in this article.
Please see the main article for a code example.
 
 

Get...

These functions allow the retrieval of particular data relating to the currently popped email.
 
Name
Description
Code
Script.Email.GetAttachmentData
Returns a list containing the URL and name for each attachment for the currently popped email. By default this is returned as a JSON string containing an array of objects.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a JSON string.
var attachmentData = JSON.parse(Script.Email.GetAttachmentData());
Script.Email.GetAttachmentNames
Returns a list containing the name for each attachment for the currently popped email. By default this is returned as a string containing a separated list of names.
 
The first parameter is an optional separator to be used when assembling the list. The default separator is a comma.
The second parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var attachmentNames = Script.Email.GetAttachmentNames("|");
Script.Email.GetAttachmentURLs
Returns a list containing the URL for each attachment for the currently popped email. By default this is returned as a string containing a separated list of URLs.
 
The first parameter is an optional separator to be used when assembling the list. The default separator is a comma.
The second parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var attachmentURLs = Script.Email.GetAttachmentURLs("|");
Script.Email.GetBody
Returns a string containing the HTML body for the currently popped email.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var bodyHtml = Script.Email.GetBody();
Script.Email.GetCCAddresses
Returns a string containing the email addresses that the currently popped email was CC to, formatted as a string of semi-colon separated addresses.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var ccAddresses = Script.Email.GetCCAddresses();
Script.Email.GetFromAddress
Returns a string containing the email address that the currently popped email was sent from.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var fromAddress = Script.Email.GetFromAddress();
Script.Email.GetFromName
Returns a string containing the display name of the account that the currently popped email was sent from.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var fromName = Script.Email.GetFromName();
Script.Email.GetID
Returns a string containing the unique message ID for the currently popped email.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var emailId = Script.Email.GetID();
Script.Email.GetReplyToAddress
Returns a string containing the provided Reply To address for the currently popped email.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var replyToAddress = Script.Email.GetReplyToAddress();
Script.Email.GetSubject
Returns a string containing the subject line for the currently popped email.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var subject = Script.Email.GetSubject();
Script.Email.GetToAddresses
Returns a string containing the email addresses that the currently popped email was sent to, formatted as a string of semi-colon separated addresses.
 
The first parameter is an optional boolean of whether to return a complex JavaScript object rather than a string.
var toAddresses = Script.Email.GetToAddresses();
 
 

Search

These functions allow searching of any specified mailbox, and will return a list of emails that match the specified filters. The mailbox to be searched is specified by the Connector Name specified in Scripting, and will respect any read-state filtering configured in that Connector.
 
In each of the Search... functions (excluding SearchEmails), the first two parameters are always the Connector Name of the mailbox to be searched, and the maximum number of emails that should be returned.
 
The returned list is formed of a JavaScript array of JavaScript objects formatted the same as described in Email Data, each of which is an email that matches the specified search filters. In all cases, the returned emails will be the most recent emails in the mailbox that meet the provided filters.
 
Name
Description
Code
Script.Email.SearchEmailsByBody
Returns any emails containing the specified text in their body.
 
The first parameter is the Connector Name.
The second parameter is the maximum number of emails to return.
The third parameter is the string that needs to exist in an email's body.
Script.Email.SearchEmailsByBody("Example Connector", 10, "problem", function(response){
     if(response.Success) {
          // Do something with the response.Data object
     }
     else {
          Script.Toast.Error("Failure: couldn't return list of emails", response.ErrorMessage);
     }
});
Script.Email.SearchEmailsByCcAddress
Returns any emails containing the specified text in their CC list.
 
The first parameter is the Connector Name.
The second parameter is the maximum number of emails to return.
The third parameter is the string that needs to exist in an email's CC list.
Script.Email.SearchEmailsByCcAddress("Example Connector", 10, "escalations@example.com", function(response){
     if(response.Success) {
          // Do something with the response.Data object
     }
     else {
          Script.Toast.Error("Failure: couldn't return list of emails", response.ErrorMessage);
     }
});
Script.Email.SearchEmailsByDate
Returns any emails from between two dates. Dates must either be provided as a JavaScript Date object, or a string in the form "YYYY-MM-DD". Provided dates are inclusive of the entire day specified.
 
The first parameter is the Connector Name.
The second parameter is the maximum number of emails to return.
The third parameter is the date the emails must have been received after.
The fourth parameter is the date the emails must have been received before.
Script.Email.SearchEmailsByDate("Example Connector", 10, "2018-10-01", "2018-11-01", function(response){
     if(response.Success) {
          // Do something with the response.Data object
     }
     else {
          Script.Toast.Error("Failure: couldn't return list of emails", response.ErrorMessage);
     }
});
Script.Email.SearchEmailsByFromAddress
Returns any emails containing the specified text in their from list.
 
The first parameter is the Connector Name.
The second parameter is the maximum number of emails to return.
The third parameter is the string that needs to exist in an email's from list.
Script.Email.SearchEmailsByFromAddress("Example Connector", 10, "currentcustomer@example.com", function(response){
     if(response.Success) {
          // Do something with the response.Data object
     }
     else {
          Script.Toast.Error("Failure: couldn't return list of emails", response.ErrorMessage);
     }
});
Script.Email.SearchEmailsByPhrase
Allows a more complex match for an email that has the specified text in one or more fields.
 
The first parameter is the Connector Name.
The second parameter is the maximum number of emails to return.
The third parameter is the string that needs to exist in the locations specified as part of the fourth parameter.
The fourth parameter is an array of string(s) specifying which field(s) the specified text needs to exist. Valid options are "subject", "from", "cc", and "body".
The fifth parameter is a string of whether the specified text needs to match "all" or "any" of the fields specified in the fourth parameter. Default is "all".
Script.Email.SearchEmailsByPhrase("Example Connector", 10, "problem", ["subject", "body"], "any", function(response){
     if(response.Success) {
          // Do something with the response.Data object
     }
     else {
          Script.Toast.Error("Failure: couldn't return list of emails", response.ErrorMessage);
     }
});
Script.Email.SearchEmailsBySubject
Returns any emails containing the specified text in their subject line.
 
The first parameter is the Connector Name.
The second parameter is the maximum number of emails to return.
The third parameter is the string that needs to exist in an email's subject line.
Script.Email.SearchEmailsBySubject("Example Connector", 10, "problem", function(response){
     if(response.Success) {
          // Do something with the response.Data object
     }
     else {
          Script.Toast.Error("Failure: couldn't return list of emails", response.ErrorMessage);
     }
});
Script.Email.SearchEmails
Accepts a single JavaScript object as a parameter, with that object containing any keys and values that you want to use in filtering the mailbox. Dates must either be provided as a JavaScript Date object, or a string in the form "YYYY-MM-DD". Provided dates are inclusive of the entire day specified.
 
connectorName: A string of the name of the Email (Incoming) Connector to be used.
callback: The required callback function.
count (Optional): The maximum number of emails to be returned. Default is 10.
match (Optional): A string of whether the returned emails need to match "all" or "any" of the specified parameters. Even when in "any", the receivedAfter and receivedBefore settings will be respected. Default is "all".
property (Optional): An array of string(s), specifying which field(s) the phrase parameter should filter if provided. Valid options are "subject", "from", "cc", and "body".
phrase (Optional): A string of the phrase that needs to be matched for the properties specified in the property parameter.
subject (Optional): A string to be matched in the returned email's subject line.
fromAddress (Optional): A string to be matched in the returned email's "From" field.
ccAddress (Optional): A string to be matched in the returned email's "CC" field.
body (Optional): A string to be matched in the returned email's body.
receivedAfter (Optional): The date that the email must have been received after to be included.
receivedBefore (Optional): The date that the email must have been received before to be included.
Script.Email.SearchEmails({
     connectorName: "Example Connector",
     count: 20,
     match: "any",
     property: ["subject", "body"],
     phrase: "problem",
     receivedAfter: "2018-01-01",
     receivedBefore: "2018-11-01",
     callback: function(response){
          if(response.Success) {
               // Do something with the response.Data object
          }
          else {
               Script.Toast.Error("Failure: couldn't return list of emails", response.ErrorMessage);
          }
     }
});