Accessing email object variables

  • Release version: Xanadu
  • Updated August 1, 2024
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of Accessing email object variables

    Inbound email action scripts in ServiceNow provide access to various components of incoming emails through theemailobject variables and the global variablesysemail. These variables enable you to extract and manipulate email data such as sender, recipients, subject, body content, and metadata to automate email processing effectively.

    Show full answer Show less

    Key Features

    • email.to, email.direct, email.copied: Retrieve comma-separated email addresses from the To: and Cc: fields, with differentiation between direct recipients and copied recipients.
    • email.bodytext, email.bodyhtml: Access the email body as either plain text or HTML, facilitating flexible content parsing.
    • email.from, email.fromsysid, email.fromAddress, email.origemail: Identify the sender’s email address or user Sys ID, with options to extract from headers depending on system properties.
    • email.subject: Get the email subject as plain text.
    • email.recipients, email.recipientsarray: Obtain recipients as a comma-separated string or as an array for iterative processing within scripts.
    • email.contenttype, email.headers, email.importance: Access MIME content type, raw email headers, and sender’s importance level, aiding in message classification and handling.
    • sysemail variable: Access the entire sysemail record that triggered the inbound action, allowing reference to email metadata fields such as uid, sysid, and contenttype.

    Practical Usage

    ServiceNow customers can leverage these variables within inbound email action scripts to automate workflows based on email content and metadata. For example, processing different recipients using email.recipientsarray enables conditional logic per recipient. Extracting the sender's Sys ID facilitates user-specific actions, and analyzing email headers supports advanced routing or filtering.

    It is important to ensure inbound emails follow RFC 2822 standards for correct parsing of multiple addresses, as ServiceNow expects commas (not semicolons) to separate email addresses in groups. Also, the system property glide.email.inboundaction.extractfromheader controls how sender addresses are extracted from headers.

    An inbound email action script contains the email object to access various pieces of an inbound email through variables. You can use the global variable sys_email with inbound email actions.

    Table 1. Accessing email objects with variables
    Variable Contents
    email.to Contains a comma-separated list of email addresses in the To: and Cc: boxes.
    email.direct Contains a comma-separated list of email addresses in the To: box.
    email.copied Contains a comma-separated list of email addresses in the Cc: box.
    email.body_text Contains the body of the email as a plain text string.
    email.body_html Contains the body of the email as an HTML string.
    email.from Contains an email address that depends on the following conditions:
    • If the address listed in the email Headers field matches an existing user's Email address, this variable contains the user's email address.
    • If the address listed in the email Headers field does not match an existing user's Email address, this variable contains the address listed in the email Headers field.
    email.from_sys_id Contains the Sys ID of the user who sent the email to the instance.
    email.fromAddress

    If system property glide.email.inbound_action.extract_from_header property is set to true, origemail is computed from the headers. The default value is false if the property does not exist.

    email.origemail Contains the address of the email sender as listed in the email Headers field.
    email.subject Contains the subject of the email as a plain text string.
    email.recipients Contains a comma-separated list of recipient addresses as a plain text string, in the To: box.
    email.recipients_array Contains the recipient addresses as an array.
    email.content_type Contains the MIME content type of the email (for example,text/plain; charset="us-ascii" or text/html; charset="us-ascii").
    email.headers Contains details about the sender, route, and receiver as a plain text string in the format of the sending email client.
    email.importance Contains an indication from the sender about how important a message is. The value can be High, Low, or empty.
    Note:
    The instance follows RFC 2822 (Internet Message Format), which requires multiple email addresses in a group to be separated by commas, not semicolons. The instance can set the values of the email.to, email.direct, and email.copied variables only if emails addressed to groups follow the expected RFC format.

    Inbound email.recipient variables

    The recipients variables (email.recipients, email.recipients-array) allow processing of inbound email based on the email recipients. For example, you can create a script to process email based on the array values:
    var rarray  = email.recipients_array ; for ( var i  = 0 ; i  < rarray.length ; i ++ ) { var recipient  = rarray [i ] ; // do something with it } 

    The sys_email variable

    This variable lets you access the received sys_email record that triggered the inbound email action. It can be used to reference fields on the email record, such as uid, sys_id, content_type, and so on.