Search code examples
netsuitesuitescriptsuitescript2.0

SuiteScript email.send not working in External URL suitelet


I am unable to send emails from external url version of a suitelet. Everything works fine for internal url, but whenever I use external url no email gets delivered. There is no error, and code after sending email is executed correctly. I tried this on both Production and Sandbox accounts, I execute it as Admin. Is email.send restricted for external url's ? Is there any workaround this? I am new to NetSuite.

        var currentuser = 1111;
        var recipient = '[email protected]';
        var subject = 'subject';
        var EmailBody = '<p> email content </p>';

        email.send({
            author: currentuser,
            recipients: recipient,
            subject: subject,
            body: EmailBody,
        });

Solution

  • /**
     * @NApiVersion 2.x
     * @NScriptType Suitelet
     * @NModuleScope SameAccount
     */
    define(['N/record', 'N/email', 'N/log', 'N/runtime'],
    
    function(record, email, log, runtime) {
       
        /**
         * Definition of the Suitelet script trigger point.
         *
         * @param {Object} context
         * @param {ServerRequest} context.request - Encapsulation of the incoming request
         * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
         * @Since 2015.2
         */
        function onRequest(context) {
            var req = context.request;
            var res = context.response;
            
            var userObj = runtime.getCurrentUser();
            log.debug('user is: ', JSON.stringify(userObj));
            log.debug('role', userObj.role);
    
    
            email.send({
                author: 5, 
                recipients: 5,
                subject: 'Test suitelet email',
                body: 'suitelet email body',
    
            });
    
            log.debug('Done','email sent');
            
            
        }
    
        return {
            onRequest: onRequest
        };
        
    });

    It is important to note that on test, development accounts there Email Options for routing is set to Send Email to Logged in User and it is hard-coded. On Sandbox and Release Preview accounts, in order for the emails to be sent when using External URL of the Suitelet, Send email To field should be set to specific email address under Setup > Company > Email Preferences > Email Options.

    Since this value is hard-coded on test drives and development accounts, it is not able to authenticate the session with the currently logged in user as there is no such user when email.send(options) or nlapiSendEmail is triggered.

    Please note that there is no such restriction on Production accounts, the functions will be executed and emails will be sent successfully.

    This behavior can be tested on Sandbox, Release Preview, Production and development accounts by using the sample Suitelet code. Please let me know how this goes!