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

Hi Mark,

 

I tried that approach and indeed is not the best at all. That's why I'm trying to retrieve the names to try another approach 🙂

Its_Azar
Kilo Sage

Hi there @rafas_10 

 

ou can retrieve the display name directly from the email object using email.from_name. This will give you “Example user” from an address like Example user <example.user@gmail.com>. You can then simply populate your field, for example: current.people_name = email.from_name;. If from_name is empty (some emails don’t include it), a common fallback is to parse email.from or look up the user by email address in sys_user.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.

Kind Regards,
Azar
Serivenow Rising Star
Developer @ KPMG.

Hi Azar,

 

It's retrieving 'undefined' with that. Maybe the SMTP is not passing everything correctly or system limitation or so, which is unfortunate

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'
};