Inbound email notifications - retrieve mailer name

rafas_10
Kilo Sage

Hi everyone,

 

Currently I have an inbound action created which I want to use to retrieve the name of the sender.

For example, when you receive an email it displays as 'Example user <example.user@gmail.com>.

 

How can I retrieve the 'Example user'  in my inbound to populate the field 'people_name' for example?

 

Appreciate the help!

1 ACCEPTED SOLUTION

rafas_10
Kilo Sage

Hi everyone,

I've got a way to workaround on this topic by fetching directly on the 'headers'.  Here is the script for everyone in need:

var InboundEmailUtils = Class.create();
InboundEmailUtils.prototype = {

    initialize: function() {},
    getGuestNameFromEmail: function(email) {

        // Default return value
        var guestName = "";

        // Safety check to avoid null pointer errors
        if (!email || !email.headers) {
            return "";
        }

        // Full raw email headers as one big string
        var rawHeaders = email.headers;

        /**
         * Extract ALL lines that start with "From:"
         * - ^From:   → must start at beginning of line
         * - g        → global (find all occurrences)
         * - m        → multiline mode (^ applies per line)
         * - i        → case-insensitive
         */
        var matches = rawHeaders.match(/^From:\s*([^\r\n]+)/gmi);

        // If at least one "From:" header exists
        if (matches && matches.length > 0) {

            /**
             * Always use the LAST "From:" header
             * - Forwarded / processed emails may add multiple From lines
             * - The last one represents the real human sender
             */
            var lastFromLine = matches[matches.length - 1];

            // Remove the "From:" prefix and trim spaces
            guestName = lastFromLine.replace(/^From:\s*/i, "").trim();


            // Remove email address if present
            if (guestName.indexOf("<") > -1) {
                guestName = guestName.split("<")[0].trim();
            }

            // Decode quoted-printable encoded names 
            if (guestName.indexOf("=?") === 0) {

                guestName = guestName
                    // Remove encoding prefix (ISO or UTF-8)
                    .replace(/=\?iso-8859-1\?Q\?/i, "")
                    .replace(/=\?utf-8\?Q\?/i, "")
                    // Remove encoding suffix
                    .replace(/\?=/g, "")
                    // Quoted-printable uses "_" instead of spaces
                    .replace(/_/g, " ")
                    // Convert hex characters (=E9 → é)
                    .replace(/=([A-F0-9]{2})/gi, function(_, hex) {
                        return String.fromCharCode(parseInt(hex, 16));
                    })
                    .trim();
            }
        }

        return guestName;
    },

    type: 'InboundEmailUtils'
};

View solution in original post

8 REPLIES 8

Dr Atul - LNG
Tera Patron

Hi @rafas_10 I assume that every incoming email is associated with a user in the instance. You can use the email ID to search in the User table and retrieve the corresponding user name

 

https://www.servicenow.com/community/itsm-forum/how-to-set-the-caller-id-from-email-inbound-action/m...

 

 

if (email.origemail == '[email_address]') {
        var arrSubject = email.subject.split(' : '); //need to include the 
        var gr = new GlideRecord('sys_user');
        gr.addQuery('user_name', arrSubject[1]);
        gr.query();
        if (gr.next()) {
            current.caller_id = gr.sys_id
        }
    } else {
        current.caller_id = gs.getUserID();
    }

 

Hi @Dr Atul - LNG ,

 

Actually this emails come from users that are not within our system. So they send an email to a specific mailbox and that mailbox redirects the email to us and creates an HR Case.

 

I can retrieve the email.from but not the original sender name.

 

Any ideia?

You can get the string in front of the '@' by using regex, but that doesn't tell you anything. Emails coming in from users that aren't in your system, can be literally anything. What if the email is from a user with a business and uses the info@mybusiness.com address? You get 'Info'. People have weird email addresses for personal use. Do you want 'iamacatperson' in your HR case?