This might be dumb but, can someone help me with E-Mail Workers?
So, I just got a new domain, and, wanted to make a E-Mail worker to sort out E-Mails sent to the domain.
Thing is, every time I try to import mimetext, it says Uncaught (in response) Error: No such module "mimetext".
imported from "worker.js".
Can someone help me? It's my first time using Workers.
177 Replies
( I'm using the https://developers.cloudflare.com/email-routing/email-workers/send-email-workers/ code, by the way )
Send emails from Workers · Cloudflare Email Routing docs
You can send an email about your Worker’s activity from your Worker to an email address verified on Email Routing. This is useful for when you want to …
Okay i got to
"install it with npm"
but how do I use NPM on Workers
I can't install Workers on a JS console...
I installed worker
It won't go past one point and can't access any sort of command line to install Mime with.
Still can't find out how
wrangler*
wrangler*
hello is anyone here
Try to install your package localy in your ide/terminal, and check dependencies in package.json. I use cloudflare/ai package and wrangler is capable of importing the code in final .js on deploy
How
I tried downloading wrangler
it froze at a stage
and wont let me do anything
( it broke at init )
Can you show what you did?
uhh sure
1 sec
so i installed wrangler
i logged in
Did that install correctly?
yeah
installed fine
Then?
i logged in
then i did the init command with whatever the code page provided me... can't find it anymore tho
init command?
yeah?
You mean to initialise a new wrangler thingy
i think so
i guess?
Have you got a worker
Now
here's the point where i got lost
I can't figure out what the... duck is happening
is it making a new one? how do i access the command line to use npm? Can I access the command line to use NPM? Am I stuck at forwarding and blocking emails forever because I can't install mime?
If you go onto your worker on the dashboard, then click quick edit and open up that page, there should be a section talking about develop with wrangler
There you can copy the command it gives you to copy the current worker code
oh wait
quick edit
1 sec
yeah
Yep click that
okay
Third bullet point you can copy the code
i already installed wrangler on my none js btw
and i logged in too
That’s fine, that’s also covered in that page
okay yeah it works
now
what do i do
init again?
publish?
Now just install your packages. Note that you can’t use a lot of node features since it’s a serverless environment
Idk what this is
just install npm i mimetext
No, since you have your project now
won't that install it... on my computer?
By doing this in your terminal
How else do you think you install packages?
1 sec
so do i need to run the third code or not
You need to run this
It’ll copy everything over
so the "wrangler init --from-dash tiny-bush-563d"
That’s just to create a new project, but you have your existing worker you created on the dashboard
... yeah and now how do i control it?
now do i access a sort of command line to
install maybe
do something
in nodejs
Are you using vscode?
No but I could open it
What else are you using then
guess what
cmd
You should use a code editor if you’re, well, gonna code…
true
alright i booted it up
Do you have nodejs installed on your machine?
yeah obviously
you’re in a completely new folder or inside of the folder that wrangler installed the worker code in?
Where is the worker code
Wherever you installed it with cmd. Or you can just install it in a brand new folder
yeeaahhhhhhhh i just let it install and it did what it wanted to do
Which imo would be better for you
Create a new folder, on your desktop or wherever is best for you
Open it in vscode
npm i wrangler -g (should already be installed globally)
npm init from-dash (ur worker)
npm i mimetext
Or just skip step one-four and open that
Won't npm i mimetext install it on my pc and not anything related to the worker?
When you use
wrangler publish
, or wrangler deploy
(i think deploy is the newer one) wrangler will compile everything for you, including packagesohhhhhh
okayy
so
now i'm gonna find the worker folder... somehow but i will
Or just install a fresh one, again
yeah i'll just do a fresh one
n
n
n
oh sorry 1 sec
Yeah
thats great.
The logs are just
non existent
It’s very clear to me what’s going on… can you screenshot your code
do you mean the logs or the code i snt
ran*
Well what did you do to make that happen
Is this an empty folder
no
It should probably be something with nothing else in it
okay let me try with an empty folder then
Still didn't work
same error
same thing as earlier
nonexisting logs
but
it did do this
is it good enough
it has package.json n stuff in it
Remove the npx, can you also screenshot your barcode
barcode?
Vscode
Sorry autocorrect haha
wdym remove the npx
if i just say "wrangler" to the terminal it does nothing
You do npx wrangler init… but the page says to not do that
Did you install wrangler globally
but... my page said to do it?
I think so, if you want i could reinstall it just to be safe
-g right
What page are you using…??
Part 3 has no npx in it
it says its not "digitally signed"
and thats why wrangler without npx isnt working
Did you login in that folder again
i'll do it again
what does this MEAN
it's been popping up everywhere
What’s your version of wrangler
let me chec
k
3.19.0
https://github.com/cloudflare/workers-sdk/issues/2097 as this did happen but at a lower version
GitHub
🐛 BUG: Entry-point file at "src/index.ts" was not found · Issue #20...
What version of Wrangler are you using? 2.1.13 What operating system are you using? wsl2 Describe the Bug Hello ! The error ✘ [ERROR] The entry-point file at "src/index.ts" was not found....
what was the solution
To update ig
npm list -g shows?
yup
What’s inside of that folder, screenshot the left
sorry what
wdym?
The file part of vscode
Idrk how to help you anymore if I’m honest.
Idk what all of that is, you can probably delete it
Just not the node modules folder ofc
that's... my npm folder?
in the appdata roaming folder
npm
it contains htis
Why are you there…?
i think?
I said open a new empty folder
I have a folder that i made
that i installed
the tiny-bush563d worker in
In it, there is only the tiny bush folder
and in the tiny bush folder
there is htis
this*
So go there
I already cd'd there
This code does not match
Match what?
This
Open this in vscode
Now run wrangler deploy
Is your terminal in the same place the folder is?
What’s your wrangler.toml
name = "tiny-bush-563d"
main = "src/index.ts"
compatibility_date = "2000-01-01"
env = { }
[triggers]
crons = [ ]
What’s inside src
there is only worker.ts
OOHHH
IS THAT WHY IT HAS BEEN FAILING
Yes, as it’s not there
did it seriously make a wrtong toml
now lets see
O
yes
total upload
okay okay
IT PUBLISHED
YESS!
Awesome
thank you
okay now
will mime work
Because you didn’t install it
I think i did install it? Well i guess i'll install it again
uploading
npm ls shows?
Also show your code
The example code
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";
export default {
async fetch(request, env) {
const msg = createMimeMessage();
msg.setSender({ name: "GPT-4", addr: "<SENDER>@example.com" });
msg.setRecipient("<RECIPIENT>@example.com");
msg.setSubject("An email generated in a worker");
msg.addMessage({
contentType: 'text/plain',
data:
Congratulations, you just sent an email from a worker.
});
var message = new EmailMessage(
"<SENDER>@example.com",
"<RECIPIENT>@example.com",
msg.asRaw()
);
try {
await env.SEB.send(message);
} catch (e) {
return new Response(e.message);
}
return new Response("Hello Send Email World!");
},
};
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";
export default {
async fetch(request, env) {
const msg = createMimeMessage();
msg.setSender({ name: "GPT-4", addr: "<SENDER>@example.com" });
msg.setRecipient("<RECIPIENT>@example.com");
msg.setSubject("An email generated in a worker");
msg.addMessage({
contentType: 'text/plain',
data:
Congratulations, you just sent an email from a worker.
});
var message = new EmailMessage(
"<SENDER>@example.com",
"<RECIPIENT>@example.com",
msg.asRaw()
);
try {
await env.SEB.send(message);
} catch (e) {
return new Response(e.message);
}
return new Response("Hello Send Email World!");
},
};
thank you discord for making the symbol for the code fonts something I don't have on my keyboardDo you have type: module in your package.json (I think it is)
{
"name": "tiny-bush-563d",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev"
},
"devDependencies": {
"wrangler": "^3.0.0"
},
"dependencies": {
"mimetext": "^3.0.16"
}
}
package.json
add “type”: “module”
where
You use import, well they use import, but you aren’t using that syntax (your package.json thinks)
Below private if you want, doesn’t matter where
{
"name": "tiny-bush-563d",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev"
},
"devDependencies": {
"wrangler": "^3.0.0"
},
"dependencies": {
"mimetext": "^3.0.16"
}
}
Now rerun
okay deploying
okay now
1 sec
new error alert
Still got out of the no module thing
but now i need to get out of yet another error
Maybe there’s another type of worker for emails. I’m not too sure since I haven’t touched emails yet
Hmmm
Well i'll try something
what the... THIS ISN'T MY CODE??
Why is it adding all this
aand now the whole thing is just spitting out errors left and right
I'll just make a new one
I haven't followed the conv but generally that is to be expected. Same reason why mimetext would work with Wrangler, when you publish it converts your code into a single javascript file, bundling all depends and anything else
ohh so... that's the entirety of minetext?
mimetext*
Mimetext and any dependencies are supported with Workers by them being uploaded alongside your code
Oh alright
probably mimetext + some other stuff included by being something mimetext depends on or default, seeing wrangler's watch stub is a bit weird but I haven't looked at the output of esbuild/wrangler publish enough to know if it should be included or not
ok
Okay
so this is mimetext
awww not again
That sounds like you're trying to use the Email Worker debug panel or something?
What's more context to that?
UHh so
I wanted to automate my website's e-mail by making some addresses go through a worker that replied to them instead of dropping them or sending them to me
And now i wanted to test it
You want your code to receive emails as an Email Worker, and respond to them? Your code has a fetch handler, for http requests
This is my code, it's an example
1 sec
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";
export default {
async email(message, env, ctx) {
const ticket = createTicket(message);
const msg = createMimeMessage();
msg.setHeader("In-Reply-To", message.headers.get("Message-ID"));
msg.setSender({ name: "Thank you for you contact", addr: "<SENDER>@example.com" });
msg.setRecipient(message.from);
msg.setSubject("Email Routing Auto-reply");
msg.addMessage({
contentType: 'text/plain',
data:
We got your message, your ticket number is ${ ticket.id }
});
const replyMessage = new EmailMessage(
"<SENDER>@example.com",
message.from,
msg.asRaw()
);
await message.reply(replyMessage);
}
}
import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";
export default {
async email(message, env, ctx) {
const ticket = createTicket(message);
const msg = createMimeMessage();
msg.setHeader("In-Reply-To", message.headers.get("Message-ID"));
msg.setSender({ name: "Thank you for you contact", addr: "<SENDER>@example.com" });
msg.setRecipient(message.from);
msg.setSubject("Email Routing Auto-reply");
msg.addMessage({
contentType: 'text/plain',
data:
We got your message, your ticket number is ${ ticket.id }
});
const replyMessage = new EmailMessage(
"<SENDER>@example.com",
message.from,
msg.asRaw()
);
await message.reply(replyMessage);
}
}
If you slap
around it, it'll be better formatted
but anyway, so the issue if you add that code, and go to the Email Worker Code Editor, and it tells you that it can't find handler/trigger? Or where does that error you put above come from?
It dropped the email for no reason
Normal. Email handled by Workers will always show as dropped
Oh lol you told me right after i read it on a forum post
Still no response either way...
What matters more is how your Worker executed it. You can tail it while you send the email to see what it did
WAIT
IT WORKED??
Whaat it sent it like a minute later but
it did
Email is a bit slow lol
well, nice
lol
Is there any way to reply to an email
but yea if you were debugging email issues in the future, go to your worker and then Logs -> Start log stream, and then send the email. You'd see the email event and any logs/errors
You could also use
npx wrangler tail
locally
isn't that what you're doing? You're sending a reply to the email which sends via message.replyFor whatever reason it sent the email independent from the original message
hmm what do you mean as independent? Are you talking about how it shows in your email client?
welp, either way, does it support html emails?
like if i put <h1>hi!</h1>
does it work
There's an example in the mimetext npm description on how you can add an html content https://www.npmjs.com/package/mimetext
npm
mimetext
RFC 2822 compliant raw email message generator written in node.js. Latest version: 3.0.16, last published: 5 months ago. Start using mimetext in your project by running
npm i mimetext
. There are 15 other projects in the npm registry using mimetext.you'll want a text/plain and a text/html. text/plain is used as fallback for old email clients and some other uses
what is ' + EOL + EOL +
End of line. That's a nodejs example I guess and it appears to be part of node:os there. I believe with email it should just be
\r\n
hello all 👋 .. beyond including an
email()
handler in the default export of a worker module, is there anything else that needs to be explicitly done for that worker to be considered an "email worker"?
currently having issues where the worker doesn't show in the Email Workers tab of the Email routing dashboard, so I can't attach said worker to an email routing rule.
on the workers side in the dashboard, there's a pill just above the email trigger with the note: Email listener not definedIt's entirely based off the exported handler. If you're having issues please make your own #workers-help post and include details/code if possible
Is there any way to set a custom profile icon for the e-mails?
Surprisingly not that easy
BIMI is the core thing you want to look into I believe, I don't know of the specifics really other then it's annoying and you need to pay for a certificate I believe (?)
oh ok
I believe via Google Workspace or creating a google acct with your custom email you can get it to use your avatar in gmail, and there's also gravatar which some may use
https://support.google.com/a/answer/10911320?hl=en
yea lol via BIMI Google wants you to register for a trademark 🙃