Forward an inbound email during certain hours

JHufford
Mega Guru

We have a group that receives tasks via email only. They will not be available during certain times and have an external contractor handling work during that time. The external contractor will not be using ServiceNow. 

 

We would like to have inbound emails forward to the external email address during those times. 

 

I have set up an inbound action set to intercept the emails directed to ServiceNow for that team and it does work to prevent emails from creating tasks during this period

JHufford_0-1740066014055.png

In the Actions i have the following script suggested in another thread

var email = new GlideEmailOutbound();
email.setSubject(current.email_subject);
email.setBody(current.email_body.text);
email.addRecipient('example@example.com');
email.send();

 

This does not appear to be sending anything to the destination address. The email log shows that the Inbound Action is being triggered correctly, however no messages appear to be created for sending. 
Any suggestions would be appreciated.

 

1 ACCEPTED SOLUTION

JHufford
Mega Guru

Thank you to @Brian Lancaster. Your feedback has been very helpful on this project. 
I fed this through Claude AI as well and came up with a nearly working script. With some massaging i got the following script that tests working. 

(function runMailScript(current, email) {
    try {
        // Get the original email details
        var subject = email.subject;
        var body = email.body_text;
        var recipient = 'externalemail@example.com';
        
        // Create the email object with required properties
        var emailObj = new GlideRecord('sys_email');
        emailObj.initialize();
        emailObj.type = 'send-ready';
        emailObj.recipients = recipient;
        emailObj.subject = subject;
        emailObj.body = body;
        emailObj.direct = true;
        emailObj.headers = email.headers; // Preserve original headers
        
        // If there's HTML content, include it and set content_type to "text/html"
        if (email.body_html) {
            emailObj.body_html = email.body_html;
            emailObj.content_type = "text/html";
        }
        
        // Insert the email record
        var sysEmailId = emailObj.insert();
        gs.log('Created email record: ' + sysEmailId, 'EmailForward');
        
        // Force immediate send
        var gr = new GlideRecord('sys_email');
        if (gr.get(sysEmailId)) {
            gr.state = 'ready';
            gr.update();
        }

        // Don't stop other inbound actions
        current.stop_processing = false;
        return true;
    } catch (ex) {
        gs.error('Error in Research Service Forwarding script: ' + ex, 'EmailForward');
        return false;
    }
})(current, email);

View solution in original post

10 REPLIES 10

JHufford
Mega Guru

Thank you to @Brian Lancaster. Your feedback has been very helpful on this project. 
I fed this through Claude AI as well and came up with a nearly working script. With some massaging i got the following script that tests working. 

(function runMailScript(current, email) {
    try {
        // Get the original email details
        var subject = email.subject;
        var body = email.body_text;
        var recipient = 'externalemail@example.com';
        
        // Create the email object with required properties
        var emailObj = new GlideRecord('sys_email');
        emailObj.initialize();
        emailObj.type = 'send-ready';
        emailObj.recipients = recipient;
        emailObj.subject = subject;
        emailObj.body = body;
        emailObj.direct = true;
        emailObj.headers = email.headers; // Preserve original headers
        
        // If there's HTML content, include it and set content_type to "text/html"
        if (email.body_html) {
            emailObj.body_html = email.body_html;
            emailObj.content_type = "text/html";
        }
        
        // Insert the email record
        var sysEmailId = emailObj.insert();
        gs.log('Created email record: ' + sysEmailId, 'EmailForward');
        
        // Force immediate send
        var gr = new GlideRecord('sys_email');
        if (gr.get(sysEmailId)) {
            gr.state = 'ready';
            gr.update();
        }

        // Don't stop other inbound actions
        current.stop_processing = false;
        return true;
    } catch (ex) {
        gs.error('Error in Research Service Forwarding script: ' + ex, 'EmailForward');
        return false;
    }
})(current, email);