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 = ["[email protected]"]
if (block.indexOf(message.headers.get("from")) == -1) {
console.log("done")
await message.forward("[email protected]");
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 = ["[email protected]"]
if (block.indexOf(message.headers.get("from")) == -1) {
console.log("done")
await message.forward("[email protected]");
console.log("done yes")
} else {
console.log("blocked")
message.setReject("Address is blocked");
console.log("blocked yes")
}
}
}
No description
19 Replies
Chaika
Chaika•11mo 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 <[email protected]> https://www.xeams.com/difference-envelope-header.htm
wawa
wawaOP•11mo ago
oh how would I get the email then
Chaika
Chaika•11mo 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('<[email protected]>'). With a list you could do that in .some()
wawa
wawaOP•11mo 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 protected]')) {
console.log("done")
await message.forward("[email protected]");
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 protected]')) {
console.log("done")
await message.forward("[email protected]");
console.log("done yes")
} else {
console.log("blocked")
message.setReject("Address is blocked");
console.log("blocked yes")
}
}
}
Chaika
Chaika•11mo 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
wawaOP•11mo ago
console.logs arent being printed out
Chaika
Chaika•11mo ago
You're tailing and not seeing them, o r?
wawa
wawaOP•11mo ago
yes
Chaika
Chaika•11mo ago
Do you see the email event?
wawa
wawaOP•11mo ago
yesd
Chaika
Chaika•11mo ago
Using dashboard or wrangler tail? If you're using dashboard make sure you're expanding
wawa
wawaOP•11mo ago
dashboard, I just see "ok" in the console
wawa
wawaOP•11mo ago
No description
Chaika
Chaika•11mo ago
click on it to expand?
wawa
wawaOP•11mo ago
{
"outcome": "ok",
"scriptName": "testemailworker",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [
{
"message": [
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done"
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done yes"
],
"level": "log",
"timestamp": 1706130515533
}
],
"eventTimestamp": 1706130511896,
"event": {
"rawSize": 6012,
"rcptTo": "[email protected]",
"mailFrom": "[email protected]"
},
"id": 0
}
{
"outcome": "ok",
"scriptName": "testemailworker",
"diagnosticsChannelEvents": [],
"exceptions": [],
"logs": [
{
"message": [
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done"
],
"level": "log",
"timestamp": 1706130511913
},
{
"message": [
"done yes"
],
"level": "log",
"timestamp": 1706130515533
}
],
"eventTimestamp": 1706130511896,
"event": {
"rawSize": 6012,
"rcptTo": "[email protected]",
"mailFrom": "[email protected]"
},
"id": 0
}
Chaika
Chaika•11mo ago
nice, logs! well you can see the format there
wawa
wawaOP•11mo ago
alright thanks ill try that
Chaika
Chaika•11mo 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
wawaOP•11mo 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 protected]')) {
console.log("done");
await message.forward("[email protected]");
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 protected]')) {
console.log("done");
await message.forward("[email protected]");
console.log("done yes");
} else {
console.log("blocked");
message.setReject("Address is blocked");
console.log("blocked yes");
}
}
}
Thanks ill take a look at those
Want results from more Discord servers?
Add your server