How to merge multiple field values into one
February 26, 2019
Email Parser processes the emails running items (Email sources, Parsers and Actions) one after another, in a sequential way. In each step, a set of fields are created based on the results of the previous steps. For instance, an Email Source creates the field Subject and then a Parser can use this field to capture, let’s say, an invoice number.
There are fields, like the To, CC and BCC that can contain multiple values. For example, the following email contains two values in the CC field:


using System;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Collections.Specialized;
using EmailParserBackendApi.ScriptingInterface;
public class MyAction : ScriptBasedAction
{
// Implement this method with your own code. Run() will be called each time
// this Action runs
public override bool Run(MailMessage email, NameValueCollection fields)
{
string output="";
foreach(string value in fields.GetValues("CC_cleaned"))
output+=value+",\r\n";
fields.Add("Merged_CC_values", output.Trim(new char[]{',','\n','\r'}));
return (true);
}
}
This script was written in reply to this question at the forum. In the following screenshots you can see step-by-step how it is used. But, basically, we do the following:
- Taking the email addresses in the CC field, discarding the name. For example, if the CC field is “John Doe” <john@example.com> we take only john@example.com. We do this in the Parser, under the field “CC_cleaned”
- Running the script to merge the resulting email addresses into one comma separated value




