email not being blocked

Hello I am trying to block a certain email for testing and i am still recieving it.
export default {
async email(message, env, ctx) {
const block = ["emailhere@gmail.com"]
if (block.indexOf(message.headers.get("from")) == -1) {
console.log("done")
await message.forward("emailhere@proton.me");
console.log("done yes")
} else {
console.log("blocked")
message.setReject("Address is blocked");
console.log("blocked yes")
}
}
}
export default {
async email(message, env, ctx) {
const block = ["emailhere@gmail.com"]
if (block.indexOf(message.headers.get("from")) == -1) {
console.log("done")
await message.forward("emailhere@proton.me");
console.log("done yes")
} else {
console.log("blocked")
message.setReject("Address is blocked");
console.log("blocked yes")
}
}
}
No description
19 Replies
Chaika
Chaika•5mo ago
Hey 👋 You're getting the from header -- which is in a specific format RFC 5322. It could be just the email like that, but most email clients also include a name, something like Jack Smith <jack.smith@abcinc.com> https://www.xeams.com/difference-envelope-header.htm
wawa
wawa•5mo ago
oh how would I get the email then
Chaika
Chaika•5mo ago
You could parse it out. There's various parsing libraries out there There's also the from on the message message.from, "envelope from', but that's very easy to spoof and not visible to the end user, can be very different in some cases. End users (like in gmail/etc) only see the header from, the one you're using If you're doing something simple and don't need it to be too robust, you could just do message.headers.get('from').endsWith('<actual@email.address>'). With a list you could do that in .some()
wawa
wawa•5mo ago
ty ill try that Hm, that still goes through to my email
export default {
async email(message, env, ctx) {
if (!message.headers.get('from').endsWith('email@gmail.com')) {
console.log("done")
await message.forward("email@proton.me");
console.log("done yes")
} else {
console.log("blocked")
message.setReject("Address is blocked");
console.log("blocked yes")
}
}
}
export default {
async email(message, env, ctx) {
if (!message.headers.get('from').endsWith('email@gmail.com')) {
console.log("done")
await message.forward("email@proton.me");
console.log("done yes")
} else {
console.log("blocked")
message.setReject("Address is blocked");
console.log("blocked yes")
}
}
}
Chaika
Chaika•5mo ago
The format is surrounded by brackets if there's a display name can always log and tail too console.log(message.headers.get('from'))
wawa
wawa•5mo ago
console.logs arent being printed out
Chaika
Chaika•5mo ago
You're tailing and not seeing them, o r?
wawa
wawa•5mo ago
yes
Chaika
Chaika•5mo ago
Do you see the email event?
wawa
wawa•5mo ago
yesd
Chaika
Chaika•5mo ago
Using dashboard or wrangler tail? If you're using dashboard make sure you're expanding
wawa
wawa•5mo ago
dashboard, I just see "ok" in the console
wawa
wawa•5mo ago
No description
Chaika
Chaika•5mo ago
click on it to expand?
wawa
wawa•5mo ago
{
"outcome": "ok",
"scriptName": "testemailworker",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [
{
"message": [
"\"z\" <censored@gmail.com>"
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done"
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done yes"
],
"level": "log",
"timestamp": 1706130515533
}
],
"eventTimestamp": 1706130511896,
"event": {
"rawSize": 6012,
"rcptTo": "test@jbzis.tech",
"mailFrom": "censored@gmail.com"
},
"id": 0
}
{
"outcome": "ok",
"scriptName": "testemailworker",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [
{
"message": [
"\"z\" <censored@gmail.com>"
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done"
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done yes"
],
"level": "log",
"timestamp": 1706130515533
}
],
"eventTimestamp": 1706130511896,
"event": {
"rawSize": 6012,
"rcptTo": "test@jbzis.tech",
"mailFrom": "censored@gmail.com"
},
"id": 0
}
Chaika
Chaika•5mo ago
nice, logs! well you can see the format there
wawa
wawa•5mo ago
alright thanks ill try that
Chaika
Chaika•5mo ago
there is proper parsing libraries you can use too, like https://github.com/jackbearheart/email-addresses. If you wanted to do it the "right way", haven't tried that package before with Workers but it doesn't look like it has any node deps
wawa
wawa•5mo ago
I managed to figure it out by doing this
export default {
async email(message, env, ctx) {
const senderAddress = message.headers.get('from');

console.log(senderAddress);

if (!senderAddress.includes('email@gmail.com')) {
console.log("done");
await message.forward("email@proton.me");
console.log("done yes");
} else {
console.log("blocked");
message.setReject("Address is blocked");
console.log("blocked yes");
}
}
}
export default {
async email(message, env, ctx) {
const senderAddress = message.headers.get('from');

console.log(senderAddress);

if (!senderAddress.includes('email@gmail.com')) {
console.log("done");
await message.forward("email@proton.me");
console.log("done yes");
} else {
console.log("blocked");
message.setReject("Address is blocked");
console.log("blocked yes");
}
}
}
Thanks ill take a look at those