Created
February 6, 2023 11:26
-
-
Save Lejo1/92fb60095ddea5b5fb78da6f00d78a59 to your computer and use it in GitHub Desktop.
Listmonk Bounce Processing using Cloudflare Route to workers.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fromAdress = "[email protected]"; | |
const webhook = "https://listmonk.example.com/webhooks/bounce"; | |
const auth = "user:password"; | |
export default { | |
async email(message, env, ctx) { | |
if (message.from != fromAdress) { | |
message.setReject("This is a Noreply Address"); | |
return; | |
} | |
let rawEmail = new Response(message.raw) | |
let body = await rawEmail.text() | |
// Match original receipient in bounce Mail | |
let toMail = new RegExp("(?<=Original-Recipient: rfc822;).*?(?=\\r\\n)").exec(body); | |
console.log(toMail); | |
if (!toMail) { | |
message.setReject("No bounce mail found"); | |
return; | |
} | |
let data = {"email": toMail[0], "source": "CloudflareWorker", "type": "hard"}; | |
// Optionaly Match campaignUuid | |
let campaignUuid = new RegExp("(?<=X-Listmonk-Campaign: ).*?(?=\\r\\n)").exec(body); | |
if (campaignUuid) { | |
data["campaign_uuid"] = campaignUuid[0]; | |
} | |
await fetch(webhook, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": "Basic " + btoa(auth) | |
}, | |
body: JSON.stringify(data) | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment