The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Set From address dynamically in email client template

PrashanthB80186
Tera Contributor

Hello I am trying to configure am email client template where sender address 'From' has to be dynamically populated based on the Assigned to field on a case table. Assigned to a specific user is a reference field and Email value has to be fetched from the user table. I have tried many ways configuring the script include and calling it from email client template (sender configuration) but nothing seems to work out. Can someone provide any workaround on how to achieve this? Thank you

4 REPLIES 4

Hayo Lubbers
Kilo Sage

Hi @PrashanthB80186 ,

 

You can add a mailscript which sets the from address.

Within the mailscript you do your magic by retrieviing the desired from address and setting it.

 

Example:

email.setFrom(current.caller_id.email);
email.setReplyTo("joe.employee@yourcompany.com");

 

More information:

https://www.servicenow.com/docs/bundle/xanadu-platform-administration/page/script/server-scripting/r...

 

Hope this helps. If it does, please mark as helpful or as solution.

 

Regards,

Hayo

How to call this email script from email email client template?

Hi @PrashanthB80186 

 

You can add a ${mail_script:script name} embedded script tag to the body of the email notification or template, replacing script name with the name of the script you created. This makes it easy to use the same scripts in multiple email notifications or templates.

 

You can add the script everywhere in the mail.

Since this is regarding the from, I would add it on the top-line, so you quickly see if the script is used.

 

More information can be found here: https://www.servicenow.com/docs/bundle/xanadu-platform-administration/page/script/server-scripting/c...

 

An example can be found in the ootb mails, like the "Admin Center Content Shared" email.

 

HayoLubbers_3-1731396371812.png

 

Mail script with email.setFrom:

 

HayoLubbers_2-1731396303283.png

 

 

 

Regards,

Hayo

Tai Vu
Kilo Patron
Kilo Patron

Hi @PrashanthB80186 

At the Send Configuration section on the form of Email Client Template, you can select the From Generation Type method to determine how the Sender (From address) in the email client message is generated.

 

Use this method only if you want a different From address than the one defined in your SMTP email account.

  • None: The From address isn’t generated in the email client message.
  • SMTP Email Account: Use the From address of the SMTP email account for the instance as the sender.
  • Select From List: Choose from a list of allowable From addresses defined in the Email Client From Address [sys_email_client_from_address] table.
  • Script: Run a GlideRecord query on the Email Client From Address [sys_email_client_from_address] table.
    For example, the following script sets the From address based on the location of the incident caller:
     

 

(function (fromAddressQuery, targetRecord) {
    // targetRecord is incident for this template
    var location = targetRecord.caller_id.country;

    if (location == 'us')
        fromAddressQuery.addQuery('email_address', 'servicedesk.us@example.com');
    else if (location == 'japan')
        fromAddressQuery.addQuery('email_address', 'servicedesk.jp@example.com');
    else if (location == 'uk')
        fromAddressQuery.addQuery('email_address', 'servicedesk.uk@example.com');

})(fromAddressQuery, targetRecord);

 

 
  • Text: Enter the email From address to be used in the client.

Ref: Create an email client template > Sender Configuration

 

Timi_0-1731314724694.png

 

Cheers,

Tai Vu