How do I create a listheader unsubsribe based on a typology rules?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
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);
}
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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);
}
Views
Replies
Total Likes
I am receiving an error saying the typology rule "List Unsubscribe" has detected a problem. Do we need to change anything in the code?
Views
Replies
Total Likes
@Swaroopgowtham Do you see the header added in the email? If yes, then you can ignore the warning.
Views
Replies
Total Likes
@_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?
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
@_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).
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
@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
Views
Replies
Total Likes
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);
Views
Replies
Total Likes
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!
Views
Replies
Total Likes
Views
Likes
Replies