Dynamically changing the from address based on email content
since: 3.3.103
You may have a remote system generated email (from: system@x.com) but want the from address to be something that is within the body of the email. By changing the from address JEMH can then auto create users and communicate directly to that user.
Example email
MIME-Version: 1.0
Received: by 10.223.112.12 with HTTP; Sat, 18 Jun 2011 22:42:26 -0700 (PDT)
Date: Sun, 19 Jun 2011 17:42:26 +1200
Message-ID: <BANLkTinB1mfSh+GwOXGNWoL4SyDvOpdBoQ@mail.gmail.com>
Subject: This is a starting email template, update as required
From: system@x.com
To: mailbox@localhost
Content-Type: text/plain; charset=UTF-8
some text
USER EMAIL: fred@flintsones.com
some text
more
Example script
/* Enter custom Script here (example below) -- USE LOWER CASE HEADER NAMES */
/*Step 1: retrieve current from email address*/
var senderAddressVal = headerBeans.get('from').getOriginalVal();
print('from: '+senderAddressVal);
print('message body', jemhUtils.getMessageBody(message));
/*Step 2: Find new value from within body of email*/
if (senderAddressVal !=null && senderAddressVal == "system@x.com") {
var pattern = Java.type("java.util.regex.Pattern").compile("USER EMAIL: ([a-zA-Z]+@[a-zA-Z]+\.com)");
var matcher = pattern.matcher(jemhUtils.getMessageBody(message));
/*Step 3: Check if a value is next to pattern in body*/
if(matcher.find() && matcher.group(1) != null){
var newSender = matcher.group(1);
var fromHeader = jemhUtils.addPreProcHeader('from', matcher.group(1));
/* Step 4: Replace Original from header value with new value gathered from body*/
if(fromHeader != null) {
headerBeans.replace('from', fromHeader);
print('new from value:' + headerBeans.get('from').getUpdatedVal() );
}
}
} else {
print('no sender address match: '+senderAddressVal);
}
Script preview