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

Brian Lancaster
Tera Sage

Can you put some log statements between each line of code for the section trying to send an email. This way we can determine where is stops functioning. I'm looking at the developer site for GlideEmailOutbound but it is not displaying any function for send or addRecipient. They could be just undocumented but it does have addAddress, Example below.

 

email.addAddress('bcc', 'joe.employee@something.com', 'dudley rocks');

 

 

Maybe this is it.....

Source: EMAIL.214691d647731a10b1576d0fe16d43c9

02-20-2025 10:37:34

Email set to ignored because of disallowed from address, email_address_filter_reason = Disallowed address

Possibly not. That appears to be an error related to inbound emails being blocked. 

 

Other logs: 

Source: Script

Created 02-20-2025 10:37:39

Added recipient: me@gmail.com

 

Source: Script

Created 02-20-2025 10:37:39

Set email subject: undefined

 

Source: Script

Created 02-20-2025 10:37:39

Set email body: undefined

 

Source: Script

Created 02-20-2025 10:37:39

Email sent

 

Maybe you need to add email.setFrom('joe.employee@something.com'); but put the email address you instance normally uses.

Added that and I see it processing but i never see any email being created to the recipient address. I'm not even sure if this is possible.