Replace specific from address to target Address

Any Headers you wish to alter must be in the Matching Headers csv, or they will not be processed!

This example shows how to find specific from address/addresses and replace them with a specified target address (ignoring Matching Jira Users).

Test Case Example

MIME-Version: 1.0 Date: Thu, 12 Aug 2021 12:05:10 +0100 Message-ID: CACQqMReZmHMHG1XXWic6WthLBYHe223mth=ndmTNXnec_gt9XA@mail.gmail.com Subject: Testing Type-Content From: "Test User" <testuser@test.com> To: changeme@thiswontwork.com Content-Type: multipart/; boundary="0000000000005d46f805c95ab59d" --0000000000005d46f805c95ab59dContent-Type: text/plain; charset="UTF-8" What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesettingindustry. Lorem Ipsum has been the industry's standard dummy text eversince the 1500s, when an unknown printer took a galley of type andscrambled it to make a type specimen book. It has survived not only fivecenturies, but also the leap into electronic typesetting, remainingessentially unchanged. It was popularised in the 1960s with the release ofLetraset sheets containing Lorem Ipsum passages, and more recently withdesktop publishing software like Aldus PageMaker including versions ofLorem Ipsum. -- Test User --0000000000005d46f805c95ab59d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div> <h2>What is Lorem Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing andtypesetting industry. Lorem Ipsum has been the industry's standard=20dummy text ever since the 1500s, when an unknown printer took a galley=20of type and scrambled it to make a type specimen book. It has survived=20not only five centuries, but also the leap into electronic typesetting,=20remaining essentially unchanged. It was popularised in the 1960s with=20the release of Letraset sheets containing Lorem Ipsum passages, and morerecently with desktop publishing software like Aldus PageMaker=20including versions of Lorem Ipsum.</p></div> <div><br></div>-- <br><div dir==3D"ltr" class=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div =dir=3D"ltr"><div><div dir=3D"ltr"><div> <div dir=3D"ltr"><div>Test User<br></div> <div> <img src=3D"https://do=http://cs.google.com/uc?export=3Ddownload&id=3D1GyXobViPNfUTuymze-Z37cfBrjrbB0= Js&revid=3D0B7BvsKsyMMv0SzFacStKOFl3NXIvTnBaREU1RkRqR05oQ0tjPQ"><br> </d=iv></div></div></div></div></div></div></div> --0000000000005d46f805c95ab59d--

Script Example

/*Step 1: Deffine Matching Address Regexp*/ var targetPattern = Java.type("java.util.regex.Pattern").compile("[a-z0-9]+@test\.com"); /*Step 2: Get the From Address Header and validate the address*/ var fromAddressess = jemhUtils.getOriginalAddressees(headerBeans.get('from')); if(fromAddressess != null){ print('Found from addressess'); var fromAddress = fromAddressess.get(0); var from = fromAddress.getAddress(); /*Step 3: Assert the From Address is not your final target Address*/ if(from != 'final.address@test.com'){ var matcher = targetPattern.matcher(from); /*Step 4: Validate the From Addesses is a targeted Address*/ if(matcher.find()) { /*Step 5: Validate the From Addesses is not already a user*/ if(userSearchService.findUsersByEmail(from).iterator().hasNext()){ print('From address is already a Jira User'); }else{ /*Step 6: Replace the From Addesses with final target address*/ var finalAddress = jemhUtils.createInternetAddress('Final Target', 'final.address@test.com'); var updatedHeader = jemhUtils.updatePreProcHeader(finalAddress, headerBeans.get('from')); print('Target sender replaced with Final Address ' + updatedHeader); } }else{ print(fromAddress + ' is not a target email address.'); } }else{ print('From address is already the Final Target User'); } }else{ print('No From address found'); }