Accessing email data send from email plugin inside Suitelet script


The data can be send to a suitelet from a email capture plugin implementation javascript file using ‘POST’ method.
If the suitelet is triggered in ‘POST’ method, we can access this data.
The data transferred using ‘POST’ method will be inside ‘scriptContext.request.body’. Since this is transferred as JSON file, the first step on using the data
is to parse the JSON object. Once the JSON object is parsed we can access the data as an object. Refer the suitelet code fragment below:

const onRequest = (scriptContext) => {
    let mailSender, mailRecipient, ccEmail="", mailSubject, mailBody, emailData, mailAttachObjArray =[];
    let fileIdArray = [], emailFlag = 0;
    try {
        if (scriptContext.request.method === 'POST') {
            emailData = JSON.parse(scriptContext.request.body);
            //log.debug("Email object",emailData);
            mailSender = emailData.from["email"];// from has two object elements name and email
            log.debug("sender is ", mailSender);
            let tempArray = emailData.to;
            mailRecipient = tempArray.slice(0,5);// maximum number of recipients or 'to' addresses is 5
            log.debug("Recipient mail ids", mailRecipient);
            let newTemp = emailData.cc.slice(0,4);//maximum 4 cc are taken

            for(let z=0; z<newTemp.length; z++){
                if(z>0 && z!==((newTemp.length)-1)){
                    ccEmail = ccEmail +",";
                }
                ccEmail = ccEmail + (emailData.cc[z]["email"]);
            }
            log.debug("cc mail id", ccEmail);

            //ccEmail1 = emailData.cc["email"];
            //log.debug({title:"CC email",details: ccEmail1})
            mailSubject = emailData.subject;
            mailBody = emailData.body;
            log.debug("mailBody", mailBody);
            mailAttachObjArray = emailData.attachObjArray;//Array with name, type and content of each attachment as object
            let attachmentCount = mailAttachObjArray.length;
}
//more code
}


To see the email plugin associated with this suitelet and know more details check the article link:

http://jobinandjismi.in/email-capture-plugin-implementation/

Leave a comment

Your email address will not be published. Required fields are marked *