Expand my Community achievements bar.

Announcing the launch of new sub-community for Campaign Web UI to cater specifically to the needs of Campaign Web UI users!
SOLVED

ACC - Listheader unsubscribe

Avatar

Level 2

How do I create a listheader unsubsribe based on a typology rules?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @jkkkkkkkkkkkkkkkkk 

 

I believe you are trying to use the typology rule to automatically add a list-unsubscribe header to the deliveries.

 

You can use this code to do that:

function addHeader(headers, header, value)  {
  
  var headerLine = header + ": " + value;
  var regExp = new RegExp(header + ":(.*)$", "i")
  var headerLines = headers.split("\n");
  for (var i=0; i < headerLines.length; i++) {
    var match = headerLines[i].match(regExp)
    if ( match != null ) {
      // replace the existing header line
      headerLines[i] = headerLine;
      return headerLines.join("\n");
    }
  }
  
  // add a new header line
  headerLines.push(headerLine);
  return headerLines.join("\n");
}

function getHeader(headers, header) {
  
  var regExp = new RegExp(header + ":(.*)$", "i")
  var headerLines = headers.split("\n");
  for each (line in headerLines) {
    var match = line.match(regExp)
    if ( match != null ) {
      return match[1].replace(/^\s*/, "");
    }
  }
  
  return "";
}

var header = getHeader(delivery.mailParameters.headers, "List-Unsubscribe");
if ( header == "" ) {
  // List-Unsubscribe header needs to be added
  delivery.mailParameters.headers = addHeader(delivery.mailParameters.headers, "List-Unsubscribe", "<mailto:<%@ include option='NmsEmail_DefaultErrorAddr' %>?subject=unsubscribe<%= escape(message.mimeMessageId) %>>");
}
else if ( header.match(/message\.mimeMessageId/) == null ) {
  // message ID need to be added for gmail
  header = header.replace(/subject=unsubscribe/, "subject=unsubscribe<%= escape(message.mimeMessageId) %>")
  delivery.mailParameters.headers = addHeader(delivery.mailParameters.headers, "List-Unsubscribe", header);
}

 

 


     Manoj
     Find me on LinkedIn

View solution in original post

13 Replies

Avatar

Community Advisor

Hello @jkkkkkkkkkkkkkkkkk ,

what you mean by listheader unsubscribe on typology rule? It is not clear at all. What I understand is to create warning if the template does not contain list-unsubscribe header?

 

If that so in typology rules you have entire delivery object at hand and you can to check the headers of the message with eg regular expression

Marcel_Szimonisz_0-1701889158008.png

 

 

Marcel

Avatar

Correct answer by
Community Advisor

Hello @jkkkkkkkkkkkkkkkkk 

 

I believe you are trying to use the typology rule to automatically add a list-unsubscribe header to the deliveries.

 

You can use this code to do that:

function addHeader(headers, header, value)  {
  
  var headerLine = header + ": " + value;
  var regExp = new RegExp(header + ":(.*)$", "i")
  var headerLines = headers.split("\n");
  for (var i=0; i < headerLines.length; i++) {
    var match = headerLines[i].match(regExp)
    if ( match != null ) {
      // replace the existing header line
      headerLines[i] = headerLine;
      return headerLines.join("\n");
    }
  }
  
  // add a new header line
  headerLines.push(headerLine);
  return headerLines.join("\n");
}

function getHeader(headers, header) {
  
  var regExp = new RegExp(header + ":(.*)$", "i")
  var headerLines = headers.split("\n");
  for each (line in headerLines) {
    var match = line.match(regExp)
    if ( match != null ) {
      return match[1].replace(/^\s*/, "");
    }
  }
  
  return "";
}

var header = getHeader(delivery.mailParameters.headers, "List-Unsubscribe");
if ( header == "" ) {
  // List-Unsubscribe header needs to be added
  delivery.mailParameters.headers = addHeader(delivery.mailParameters.headers, "List-Unsubscribe", "<mailto:<%@ include option='NmsEmail_DefaultErrorAddr' %>?subject=unsubscribe<%= escape(message.mimeMessageId) %>>");
}
else if ( header.match(/message\.mimeMessageId/) == null ) {
  // message ID need to be added for gmail
  header = header.replace(/subject=unsubscribe/, "subject=unsubscribe<%= escape(message.mimeMessageId) %>")
  delivery.mailParameters.headers = addHeader(delivery.mailParameters.headers, "List-Unsubscribe", header);
}

 

 


     Manoj
     Find me on LinkedIn

Avatar

Level 1

I am receiving an error saying the typology rule "List Unsubscribe" has detected a problem. Do we need to change anything in the code?

Avatar

Community Advisor

@Swaroopgowtham  Do you see the header added in the email? If yes, then you can ignore the warning.


     Manoj
     Find me on LinkedIn

Avatar

Level 1

@Manoj_Kumar_ Email is not sent and it is erroring out due to this typology rule, Do we need to make any changes to the code or will the same code work?

Avatar

Community Advisor

Do you see any option created in your instance with the name?

NmsEmail_DefaultErrorAddr

 

If not then create an option and check with Adobe on what should be the value.


     Manoj
     Find me on LinkedIn

Avatar

Level 1

@Manoj_Kumar_ 

NmsEmail_DefaultErrorAddr

 Option is present in the instance. I am now able to send the email without any errors, however I am unable to locate the unsubscribe link in the gmail header.

 

Avatar

Community Advisor

Hello @Swaroopgowtham 

Are you testing it in sandbox or a production instance?

 

It might not work on sandbox because of the IP reputation. Try to test it on production to see the results.


     Manoj
     Find me on LinkedIn

Avatar

Level 3

@Manoj_Kumar_ This code works perfectly for audiences of Recipient table. But what are the tweaks required for this code to work on table other than recipient(different target mapping).

Avatar

Level 4

Hi @ratika ,

Since the code by default updates the nms:recipient table, so for any custom table to get updated, you need to tweak the script code in the noClick Unsub Webapp, which has the defined Unsubscription function and there you need to provide context to your custom table.

Hope that helps.

 

Avatar

Level 3

@rahulkad29786 I tried that approach as well.

In noClick Unsub Webapp I am using below script:

var id = ctx.tableabc.@id.toString();
var recipient = <tableabc _key="@id" id={id} _operation="update" xtkschema="cus:tableabc" blacklistEmail={1} ></tableabc>;
xtk.session.Write(recipient);

 

And in Typology rule I used below code:

// Function to add or replace a header in the provided headers
function addHeader(headers, header, value) {

// Create the new header line
var headerLine = header + ": " + value;

// Create a regular expression to find the specified header
var regExp = new RegExp(header + ":(.*)$", "i")

// Split the headers into individual lines
var headerLines = headers.split("\n");

// Loop through each line
for (var i=0; i < headerLines.length; i++) {

// Check if the specified header exists
var match = headerLines[i].match(regExp)

// If it exists
if ( match != null ) {

// Replace the existing header line
headerLines[i] = headerLine;

// Return the modified headers
return headerLines.join("\n");
}
}

// If the header does not exist, add the new header line
headerLines.push(headerLine);

// Return the modified headers
return headerLines.join("\n");
}

// Function to get the value of a specified header from the provided headers
function getHeader(headers, header) {

// Create a regular expression to find the specified header
var regExp = new RegExp(header + ":(.*)$", "i")

// Split the headers into individual lines
var headerLines = headers.split("\n");

// Loop each line
for each (line in headerLines) {

// Check if the specified header exists
var match = line.match(regExp);

// If it exists
if ( match != null ) {

// Return the header value, removing leading whitespace
return match[1].replace(/^\s*/, "");
}
}

// If the header does not exist, return an empty string
return "";
}


// Define the unsubscribe URL
var headerUnsubUrl = "serverURL/webApp/unsubNoClick?id=<%= tableabc.cryptedId %>";

// Get the value of the List-Unsubscribe header
var headerUnsub = getHeader(delivery.mailParameters.headers, "List-Unsubscribe");

// If the List-Unsubscribe header does not exist
if ( headerUnsub === "" ) {
// Add the List-Unsubscribe header
delivery.mailParameters.headers = addHeader(delivery.mailParameters.headers, "List-Unsubscribe", "<"+headerUnsubUrl+">");
}
// If the List-Unsubscribe header exists and contains 'mailto'
else if(headerUnsub.search('mailto')){
// Replace the existing List-Unsubscribe header
delivery.mailParameters.headers = addHeader(delivery.mailParameters.headers, "List-Unsubscribe", "<"+headerUnsubUrl+">");
}

// Get the value of the List-Unsubscribe-Post header
var headerUnsubPost = getHeader(delivery.mailParameters.headers, "List-Unsubscribe-Post");

// If the List-Unsubscribe-Post header does not exist
if ( headerUnsubPost === "" ) {
// Add the List-Unsubscribe-Post header
delivery.mailParameters.headers = addHeader(delivery.mailParameters.headers, "List-Unsubscribe-Post", "List-Unsubscribe=One-Click");
}

// Return true to indicate success
return true;

 

The unsubscription page is working properly in the preview.

But when I am doing an actual test through mailer, the Unsubscribe option is not coming up.

 

Could you please check and confirm if I missed out anything in code?

 

Thanks

 

 

Avatar

Level 4

Hi @ratika ,
You can write this of your custom code(below) in a dynamic JSSP and call that instead of webApp as in webapp script this script won't provide the expected result .
var id = ctx.tableabc.@id.toString();
var recipient = <tableabc _key="@id" id={id} _operation="update" xtkschema="cus:tableabc" blacklistEmail={1} ></tableabc>;
xtk.session.Write(recipient);

 

 

Avatar

Administrator

Hi @jkkkkkkkkkkkkkkkkk,

Were you able to resolve this query with the help of the given solutions or do you still need more help here? Do let us know. In case the given solutions were helpful, then kindly choose the one that helped you the most as the 'Correct Reply'.
Thanks!



Sukrity Wadhwa