Extracting email address from error string

harishbabua8
Giga Contributor

Hello Everyone,

I need help in extracting email addresses from error string. The error string will look like this.

 

450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found

 

I tried different RegEx but no luck. Any help is appreciated.

3 ACCEPTED SOLUTIONS

AnveshKumar M
Tera Sage
Tera Sage

Hi @harishbabua8 

 

You can use the following script to extract the email address.

 

var err_str = "450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found";
var invalid_recipient = "";
if (err_str_ln.indexOf("<") != -1 && err_str_ln.indexOf(">") != -1) {
   invalid_recipient = err_str.split('<')[1].split('>')[0];
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

 

Thanks,
Anvesh

View solution in original post

Yes,

This solution will work for you to extract all emails enclosed in < and >:

 

// Sample text containing values between < and >
var text = '450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found anotherEmail <example2@text.com>';

// Regular expression to extract values between < and >
var regex = /<([^>]*)>/g;

// Array to store extracted values
var extractedValues = [];

// Extract values using regex
var match;
while ((match = regex.exec(text)) !== null) {
  extractedValues.push(match[1]);
}

// Log the extracted values
gs.info('Extracted values:'+ extractedValues);

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

@harishbabua8 

You can try the following script then.

 

var err_arr = err_str.split("\n");
var invalid_recipients = [];

for (key in err_arr) {
   var err_str_ln = err_arr[key];
   if (err_str_ln.indexOf("<") != -1) {
      invalid_recipients.push(err_str_ln.split('<')[1].split('>')[0]);
   }
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

8 REPLIES 8

AnveshKumar M
Tera Sage
Tera Sage

Hi @harishbabua8 

 

You can use the following script to extract the email address.

 

var err_str = "450 4.1.2 <eample@example.com>: Recipient address rejected: domain not found";
var invalid_recipient = "";
if (err_str_ln.indexOf("<") != -1 && err_str_ln.indexOf(">") != -1) {
   invalid_recipient = err_str.split('<')[1].split('>')[0];
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

 

Thanks,
Anvesh

harishbabua8
Giga Contributor

Thanks @Anil Lande @AnveshKumar M @Sohail Khilji 

 

I observed that some times there are multiple lines of them in the error string, is it possible to extract all of them?

@harishbabua8 

You can try the following script then.

 

var err_arr = err_str.split("\n");
var invalid_recipients = [];

for (key in err_arr) {
   var err_str_ln = err_arr[key];
   if (err_str_ln.indexOf("<") != -1) {
      invalid_recipients.push(err_str_ln.split('<')[1].split('>')[0]);
   }
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

Aniket Chavan
Tera Sage
Tera Sage

Hello @harishbabua8,

Please take a look at the script below in which I have used the regex,

// Your error string with multiple lines
var errorString = "450 4.1.2 <example1@example.com>: Recipient address rejected: domain not found\nSome other text\n450 4.1.2 <example2@example.com>: Another error message";

// Regular expression to extract all email addresses
var regex = /<([^>]+)>/g;
var matches = errorString.match(regex);

// Check if there are matches and retrieve the email addresses
if (matches && matches.length > 0) {
    for (var i = 0; i < matches.length; i++) {
        var emailAddress = matches[i].substring(1, matches[i].length - 1);
        gs.info("Extracted Email Address using Regex: " + emailAddress);
    }
} else {
    gs.info("No email addresses found using Regex");
}

 

 

Feel free to give it a try and if the regex doesn't work as expected, I've also provided an alternative method using string manipulation functions.

 

 

// Your error string with multiple lines
var errorString = "450 4.1.2 <example1@example.com>: Recipient address rejected: domain not found\nSome other text\n450 4.1.2 <example2@example.com>: Another error message";

// Find all positions of '<' and '>'
var regex = /<([^>]+)>/g;
var matches = errorString.match(regex);

// Check if there are matches and retrieve the email addresses
if (matches && matches.length > 0) {
    for (var i = 0; i < matches.length; i++) {
        var startIndex = matches[i].indexOf('<');
        var endIndex = matches[i].indexOf('>');
        if (startIndex !== -1 && endIndex !== -1) {
            var emailAddress = matches[i].substring(startIndex + 1, endIndex);
            gs.info("Extracted Email Address using String Manipulation: " + emailAddress);
        }
    }
} else {
    gs.info("No email addresses found using String Manipulation");
}

Let me know if you encounter any issues while testing it out.

 

Please mark my answer as helpful and the solution accepted if it serves the purpose.

Regards,

Aniket.