Email workers send email

Hello im trying to send an email within the email woker. I tried this:
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";

export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},

async email(message, env, ctx) {
console.log("Email received", message);
const msg = createMimeMessage();

msg.setSender({ name: message.from, addr: message.from});

msg.setSubject("Message");
msg.addMessage({
contentType: 'text/plain',
data: `${ message.raw }`
});

msg.setHeaders(message.headers)

console.log("Msg object ready");

var message = new EmailMessage(
message.from,
"mymail@abc.xyz",
msg.asRaw()
);


console.log("Messages ready");

try {
await env.SEB.send(message);
console.log("Message sent");
} catch (e) {
console.error(e.message);
}
}
};
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";

export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},

async email(message, env, ctx) {
console.log("Email received", message);
const msg = createMimeMessage();

msg.setSender({ name: message.from, addr: message.from});

msg.setSubject("Message");
msg.addMessage({
contentType: 'text/plain',
data: `${ message.raw }`
});

msg.setHeaders(message.headers)

console.log("Msg object ready");

var message = new EmailMessage(
message.from,
"mymail@abc.xyz",
msg.asRaw()
);


console.log("Messages ready");

try {
await env.SEB.send(message);
console.log("Message sent");
} catch (e) {
console.error(e.message);
}
}
};
but i get the error: Cannot read properties of undefined (reading 'send'). Im not the best at JS and workers but my guess is that "SEB" is not defined or not the object i need, but i dont have any clue how to fix this. I took code from https://developers.cloudflare.com/email-routing/email-workers/send-email-workers/#example-worker and https://developers.cloudflare.com/email-routing/email-workers/reply-email-workers/
19 Replies
Alisson Acioli
Alisson Acioli4mo ago
Have you configured your wrangler.toml file?
SageSphinx63920
SageSphinx639204mo ago
What should be in my wrangler.toml
Alisson Acioli
Alisson Acioli4mo ago
send_email = [ {name = "SEB"} ]
SageSphinx63920
SageSphinx639204mo ago
oh Now i get the error (error) :email from gmail.com not allowed because domain was not found Tried to send an email from my gmail To the domain after some fixing with my bad variable names this is what i get: (error) could not send email: could not send email: Unknown error: permanent error (521): 5.5.2 mail.sageee.xyz Error: bare <LF> received
Alisson Acioli
Alisson Acioli4mo ago
I am unaware of this error. Check in your Cloudflare dashboard in the Email product that you have correctly configured the correct senders. If possible, post a photo here and also post your code and your wrangler.toml here
SageSphinx63920
SageSphinx639203mo ago
My current code is the following:
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";

export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},

async email(message, env, ctx) {
console.log("Email function called");
const msg = createMimeMessage();

msg.setSender({ name: message.from, addr: message.to});

msg.setSubject("Message");
msg.addMessage({
contentType: 'text/plain',
data: `${ message.raw }`
});

console.log(`Message from ${message.from} to ${message.to}`)
console.log(`Message headers: ${message.headers}`)
console.log(`Message raw: ${message.raw}`)
console.log(`Message raw size ${message.rawSize}`)

var newMessage = new EmailMessage(
message.to,
"sage@sageee.xyz",
msg.asRaw()
);

console.log("Messages ready");

try {
await env.MAILS.send(newMessage);
console.log("Message sent");
} catch (e) {
console.error(e.message);
}
}
};
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";

export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},

async email(message, env, ctx) {
console.log("Email function called");
const msg = createMimeMessage();

msg.setSender({ name: message.from, addr: message.to});

msg.setSubject("Message");
msg.addMessage({
contentType: 'text/plain',
data: `${ message.raw }`
});

console.log(`Message from ${message.from} to ${message.to}`)
console.log(`Message headers: ${message.headers}`)
console.log(`Message raw: ${message.raw}`)
console.log(`Message raw size ${message.rawSize}`)

var newMessage = new EmailMessage(
message.to,
"sage@sageee.xyz",
msg.asRaw()
);

console.log("Messages ready");

try {
await env.MAILS.send(newMessage);
console.log("Message sent");
} catch (e) {
console.error(e.message);
}
}
};
and my wrangler is
name = "multi-forward"
main = "src/index.js"
compatibility_date = "2024-02-23"

node_compat = true

send_email = [
{name = "MAILS"},
]
name = "multi-forward"
main = "src/index.js"
compatibility_date = "2024-02-23"

node_compat = true

send_email = [
{name = "MAILS"},
]
I still don't know why its not working @Alisson Acioli sorry for my late response ^^ Any ideas ._. @Alisson Acioli you are my last hope :(
Alisson Acioli
Alisson Acioli3mo ago
Try change: msg.addMessage({ contentType: 'text/plain', data: ${ message.raw } }); to: msg.addMessage({ contentType: 'text/plain', data: ${message.raw}.replace(/\n/g, '\r\n') }); From the error you indicated, it seems to be something referring to a final line that is being received and could not. Also try to check the text you are sending. If the solution above doesn't work, try adding a direct text to test as data in "data": testing...
SageSphinx63920
SageSphinx639203mo ago
Thats, i guess interessting
No description
SageSphinx63920
SageSphinx639203mo ago
I think the error is some kind of bug with my mail server, because changing it to som other email results in this
msg.setSubject("Message");
msg.addMessage({
contentType: 'text/plain',
data: `Testing: ${message.raw}.replace(/\n/g, '\r\n') Finished`
});
msg.setSubject("Message");
msg.addMessage({
contentType: 'text/plain',
data: `Testing: ${message.raw}.replace(/\n/g, '\r\n') Finished`
});
Now using this But still i sadly do not see the content of the mail
Alisson Acioli
Alisson Acioli3mo ago
Try again: msg.addMessage({ contentType: 'text/plain', data: message.raw.replace(/\n/g, '\r\n') }); If it goes wrong, try: msg.addMessage({ contentType: 'text/plain', data: 'Testing' }); In theory, one of the two would have to work.
SageSphinx63920
SageSphinx639203mo ago
✘ [ERROR] TypeError: message.raw.replace is not a function Replace is only valid for strings and raw is a stream
Alisson Acioli
Alisson Acioli3mo ago
try 2 code
SageSphinx63920
SageSphinx639203mo ago
Don't actually know how to convert a stream into string I google xd Actually i do not use js often actually i can count my js experiences on one hand
Alisson Acioli
Alisson Acioli3mo ago
Buffer.from(message.raw).toString();
SageSphinx63920
SageSphinx639203mo ago
Thx <3
SageSphinx63920
SageSphinx639203mo ago
After some changes i got the message.raw as string but still not the messagte content ._. https://pastebin.com/9t2gUHwG
Pastebin
Received: from mail-ej1-x62f.google.com (2a00:1450:4864:20::62f)by ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
SageSphinx63920
SageSphinx639203mo ago
Content of my mail was keyboard smash or "Asdvdsvdasvsd" I do not find this in the output
SageSphinx63920
SageSphinx639203mo ago
And the docs say it is the email message content
No description
SageSphinx63920
SageSphinx639203mo ago
Its finally working I still do not know why my mail server does not accept these mails but as far at it works on other mails im fine with this