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
Gaming_YT
Gaming_YT11mo ago
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 …
Gaming_YT
Gaming_YT11mo ago
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
Freeze
Freeze11mo ago
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
Gaming_YT
Gaming_YT11mo ago
How I tried downloading wrangler it froze at a stage and wont let me do anything ( it broke at init )
gwapes
gwapes11mo ago
Can you show what you did?
Gaming_YT
Gaming_YT11mo ago
uhh sure 1 sec so i installed wrangler i logged in
gwapes
gwapes11mo ago
Did that install correctly?
Gaming_YT
Gaming_YT11mo ago
yeah installed fine
gwapes
gwapes11mo ago
Then?
Gaming_YT
Gaming_YT11mo ago
i logged in then i did the init command with whatever the code page provided me... can't find it anymore tho
gwapes
gwapes11mo ago
init command?
Gaming_YT
Gaming_YT11mo ago
yeah?
gwapes
gwapes11mo ago
You mean to initialise a new wrangler thingy
Gaming_YT
Gaming_YT11mo ago
i think so i guess?
gwapes
gwapes11mo ago
Have you got a worker
Gaming_YT
Gaming_YT11mo ago
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?
gwapes
gwapes11mo ago
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
Gaming_YT
Gaming_YT11mo ago
No description
Gaming_YT
Gaming_YT11mo ago
oh wait quick edit 1 sec
Gaming_YT
Gaming_YT11mo ago
yeah
No description
gwapes
gwapes11mo ago
Yep click that
Gaming_YT
Gaming_YT11mo ago
okay
gwapes
gwapes11mo ago
Third bullet point you can copy the code
Gaming_YT
Gaming_YT11mo ago
i already installed wrangler on my none js btw and i logged in too
gwapes
gwapes11mo ago
That’s fine, that’s also covered in that page
Gaming_YT
Gaming_YT11mo ago
okay yeah it works now what do i do init again? publish?
gwapes
gwapes11mo ago
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
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
just install npm i mimetext No, since you have your project now
Gaming_YT
Gaming_YT11mo ago
won't that install it... on my computer?
gwapes
gwapes11mo ago
By doing this in your terminal How else do you think you install packages?
Gaming_YT
Gaming_YT11mo ago
1 sec so do i need to run the third code or not
gwapes
gwapes11mo ago
You need to run this It’ll copy everything over
Gaming_YT
Gaming_YT11mo ago
so the "wrangler init --from-dash tiny-bush-563d"
gwapes
gwapes11mo ago
That’s just to create a new project, but you have your existing worker you created on the dashboard
Gaming_YT
Gaming_YT11mo ago
... yeah and now how do i control it? now do i access a sort of command line to install maybe do something in nodejs
gwapes
gwapes11mo ago
Are you using vscode?
Gaming_YT
Gaming_YT11mo ago
No but I could open it
gwapes
gwapes11mo ago
What else are you using then Awk
Gaming_YT
Gaming_YT11mo ago
guess what cmd
gwapes
gwapes11mo ago
You should use a code editor if you’re, well, gonna code
Gaming_YT
Gaming_YT11mo ago
true alright i booted it up
gwapes
gwapes11mo ago
Do you have nodejs installed on your machine?
Gaming_YT
Gaming_YT11mo ago
yeah obviously
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
you’re in a completely new folder or inside of the folder that wrangler installed the worker code in?
Gaming_YT
Gaming_YT11mo ago
Where is the worker code
gwapes
gwapes11mo ago
Wherever you installed it with cmd. Or you can just install it in a brand new folder
Gaming_YT
Gaming_YT11mo ago
yeeaahhhhhhhh i just let it install and it did what it wanted to do
gwapes
gwapes11mo ago
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
Gaming_YT
Gaming_YT11mo ago
Won't npm i mimetext install it on my pc and not anything related to the worker?
gwapes
gwapes11mo ago
When you use wrangler publish, or wrangler deploy (i think deploy is the newer one) wrangler will compile everything for you, including packages
Gaming_YT
Gaming_YT11mo ago
ohhhhhh okayy so now i'm gonna find the worker folder... somehow but i will
gwapes
gwapes11mo ago
Or just install a fresh one, again
Gaming_YT
Gaming_YT11mo ago
yeah i'll just do a fresh one n n n oh sorry 1 sec
Gaming_YT
Gaming_YT11mo ago
Yeah
No description
Gaming_YT
Gaming_YT11mo ago
thats great. The logs are just non existent
gwapes
gwapes11mo ago
It’s very clear to me what’s going on… can you screenshot your code
Gaming_YT
Gaming_YT11mo ago
do you mean the logs or the code i snt ran*
gwapes
gwapes11mo ago
Well what did you do to make that happen
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
Is this an empty folder
Gaming_YT
Gaming_YT11mo ago
no
gwapes
gwapes11mo ago
It should probably be something with nothing else in it
Gaming_YT
Gaming_YT11mo ago
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
Gaming_YT
Gaming_YT11mo ago
No description
Gaming_YT
Gaming_YT11mo ago
is it good enough it has package.json n stuff in it
gwapes
gwapes11mo ago
Remove the npx, can you also screenshot your barcode
Gaming_YT
Gaming_YT11mo ago
barcode?
gwapes
gwapes11mo ago
Vscode Sorry autocorrect haha
Gaming_YT
Gaming_YT11mo ago
wdym remove the npx if i just say "wrangler" to the terminal it does nothing
gwapes
gwapes11mo ago
You do npx wrangler init… but the page says to not do that Did you install wrangler globally
Gaming_YT
Gaming_YT11mo ago
but... my page said to do it? I think so, if you want i could reinstall it just to be safe -g right
gwapes
gwapes11mo ago
What page are you using…??
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
Part 3 has no npx in it
Gaming_YT
Gaming_YT11mo ago
it says its not "digitally signed"
No description
Gaming_YT
Gaming_YT11mo ago
and thats why wrangler without npx isnt working
gwapes
gwapes11mo ago
Did you login in that folder again
Gaming_YT
Gaming_YT11mo ago
i'll do it again
Gaming_YT
Gaming_YT11mo ago
what does this MEAN
No description
Gaming_YT
Gaming_YT11mo ago
it's been popping up everywhere
gwapes
gwapes11mo ago
What’s your version of wrangler
Gaming_YT
Gaming_YT11mo ago
let me chec k 3.19.0
gwapes
gwapes11mo ago
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....
Gaming_YT
Gaming_YT11mo ago
what was the solution
gwapes
gwapes11mo ago
To update ig npm list -g shows?
Gaming_YT
Gaming_YT11mo ago
No description
Gaming_YT
Gaming_YT11mo ago
yup
gwapes
gwapes11mo ago
What’s inside of that folder, screenshot the left
Gaming_YT
Gaming_YT11mo ago
sorry what wdym?
gwapes
gwapes11mo ago
The file part of vscode Idrk how to help you anymore if I’m honest.
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
Idk what all of that is, you can probably delete it Just not the node modules folder ofc
Gaming_YT
Gaming_YT11mo ago
that's... my npm folder? in the appdata roaming folder npm it contains htis
gwapes
gwapes11mo ago
Why are you there…?
Gaming_YT
Gaming_YT11mo ago
i think?
gwapes
gwapes11mo ago
I said open a new empty folder
Gaming_YT
Gaming_YT11mo ago
I have a folder that i made that i installed the tiny-bush563d worker in
Gaming_YT
Gaming_YT11mo ago
In it, there is only the tiny bush folder
No description
Gaming_YT
Gaming_YT11mo ago
and in the tiny bush folder
Gaming_YT
Gaming_YT11mo ago
No description
Gaming_YT
Gaming_YT11mo ago
there is htis this*
gwapes
gwapes11mo ago
So go there
Gaming_YT
Gaming_YT11mo ago
I already cd'd there
gwapes
gwapes11mo ago
This code does not match
Gaming_YT
Gaming_YT11mo ago
Match what?
gwapes
gwapes11mo ago
This Open this in vscode
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
Now run wrangler deploy
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
Is your terminal in the same place the folder is?
Gaming_YT
Gaming_YT11mo ago
No description
gwapes
gwapes11mo ago
What’s your wrangler.toml
Gaming_YT
Gaming_YT11mo ago
name = "tiny-bush-563d" main = "src/index.ts" compatibility_date = "2000-01-01" env = { } [triggers] crons = [ ]
gwapes
gwapes11mo ago
What’s inside src
Gaming_YT
Gaming_YT11mo ago
there is only worker.ts OOHHH IS THAT WHY IT HAS BEEN FAILING
gwapes
gwapes11mo ago
Yes, as it’s not there
Gaming_YT
Gaming_YT11mo ago
did it seriously make a wrtong toml now lets see O yes total upload okay okay IT PUBLISHED YESS!
gwapes
gwapes11mo ago
Awesome
Gaming_YT
Gaming_YT11mo ago
thank you okay now will mime work
Gaming_YT
Gaming_YT11mo ago
OHMYGOD
No description
gwapes
gwapes11mo ago
Because you didn’t install it
Gaming_YT
Gaming_YT11mo ago
I think i did install it? Well i guess i'll install it again uploading
Gaming_YT
Gaming_YT11mo ago
ScreenShot20231118at7
No description
gwapes
gwapes11mo ago
npm ls shows? Also show your code
Gaming_YT
Gaming_YT11mo ago
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 keyboard
gwapes
gwapes11mo ago
Do you have type: module in your package.json (I think it is)
Gaming_YT
Gaming_YT11mo ago
{ "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
gwapes
gwapes11mo ago
add “type”: “module”
Gaming_YT
Gaming_YT11mo ago
where
gwapes
gwapes11mo ago
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
Gaming_YT
Gaming_YT11mo ago
{ "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" } }
gwapes
gwapes11mo ago
Now rerun
Gaming_YT
Gaming_YT11mo ago
okay deploying okay now 1 sec
Gaming_YT
Gaming_YT11mo ago
new error alert
No description
Gaming_YT
Gaming_YT11mo ago
Still got out of the no module thing but now i need to get out of yet another error
gwapes
gwapes11mo ago
Maybe there’s another type of worker for emails. I’m not too sure since I haven’t touched emails yet
Gaming_YT
Gaming_YT11mo ago
Hmmm Well i'll try something
Gaming_YT
Gaming_YT11mo ago
what the... THIS ISN'T MY CODE??
No description
Gaming_YT
Gaming_YT11mo ago
Why is it adding all this
No description
Gaming_YT
Gaming_YT11mo ago
aand now the whole thing is just spitting out errors left and right I'll just make a new one
Chaika
Chaika11mo ago
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
Gaming_YT
Gaming_YT11mo ago
ohh so... that's the entirety of minetext? mimetext*
Chaika
Chaika11mo ago
Mimetext and any dependencies are supported with Workers by them being uploaded alongside your code
Gaming_YT
Gaming_YT11mo ago
Oh alright
Chaika
Chaika11mo ago
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
Gaming_YT
Gaming_YT11mo ago
ok
Gaming_YT
Gaming_YT11mo ago
Okay
No description
Gaming_YT
Gaming_YT11mo ago
so this is mimetext
Gaming_YT
Gaming_YT11mo ago
awww not again
No description
Chaika
Chaika11mo ago
That sounds like you're trying to use the Email Worker debug panel or something? What's more context to that?
Gaming_YT
Gaming_YT11mo ago
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
Chaika
Chaika11mo ago
You want your code to receive emails as an Email Worker, and respond to them? Your code has a fetch handler, for http requests
Gaming_YT
Gaming_YT11mo ago
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); } }
Chaika
Chaika11mo ago
If you slap
```js
<code>
```js
<code>
```
```
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?
Gaming_YT
Gaming_YT11mo ago
It dropped the email for no reason
No description
Chaika
Chaika11mo ago
Normal. Email handled by Workers will always show as dropped
Gaming_YT
Gaming_YT11mo ago
Oh lol you told me right after i read it on a forum post Still no response either way...
Chaika
Chaika11mo ago
What matters more is how your Worker executed it. You can tail it while you send the email to see what it did
Gaming_YT
Gaming_YT11mo ago
WAIT IT WORKED?? Whaat it sent it like a minute later but it did
Chaika
Chaika11mo ago
Email is a bit slow lol well, nice
Gaming_YT
Gaming_YT11mo ago
lol Is there any way to reply to an email
Chaika
Chaika11mo ago
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.reply
Gaming_YT
Gaming_YT11mo ago
For whatever reason it sent the email independent from the original message
Chaika
Chaika11mo ago
hmm what do you mean as independent? Are you talking about how it shows in your email client?
Gaming_YT
Gaming_YT11mo ago
welp, either way, does it support html emails? like if i put <h1>hi!</h1> does it work
Chaika
Chaika11mo ago
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.
Chaika
Chaika11mo ago
you'll want a text/plain and a text/html. text/plain is used as fallback for old email clients and some other uses
msg.setSender('[email protected]')
msg.setRecipients('[email protected]')
msg.setSubject('Testing MimeText 🐬 (Plain Text + HTML With Mixed Attachments)')
msg.addMessage({
contentType: 'text/plain',
data: 'Hello there,' + EOL + EOL +
'This is a test email sent by MimeText test suite.'
})
// specify inline attachment's content id inside img src tag. <img src="cid:[ID]">
msg.addMessage({
contentType: 'text/html',
data: 'Hello there,<br><br>' +
'This is a test email sent by <b>MimeText</b> test suite.<br><br>' +
'The term MimeText above supposed to be bold. Are you able to see it?<br><br>' +
'Below, there should be a small image that contains little black dots:<br><br>' +
'<img src="cid:dots123456"><br><br>' +
'Best regards.'
})
msg.setSender('[email protected]')
msg.setRecipients('[email protected]')
msg.setSubject('Testing MimeText 🐬 (Plain Text + HTML With Mixed Attachments)')
msg.addMessage({
contentType: 'text/plain',
data: 'Hello there,' + EOL + EOL +
'This is a test email sent by MimeText test suite.'
})
// specify inline attachment's content id inside img src tag. <img src="cid:[ID]">
msg.addMessage({
contentType: 'text/html',
data: 'Hello there,<br><br>' +
'This is a test email sent by <b>MimeText</b> test suite.<br><br>' +
'The term MimeText above supposed to be bold. Are you able to see it?<br><br>' +
'Below, there should be a small image that contains little black dots:<br><br>' +
'<img src="cid:dots123456"><br><br>' +
'Best regards.'
})
Gaming_YT
Gaming_YT11mo ago
what is ' + EOL + EOL +
Chaika
Chaika11mo ago
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
betim
betim11mo ago
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 defined
Chaika
Chaika11mo ago
It'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
Gaming_YT
Gaming_YT11mo ago
Is there any way to set a custom profile icon for the e-mails?
Chaika
Chaika11mo ago
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 (?)
Gaming_YT
Gaming_YT11mo ago
oh ok
Chaika
Chaika11mo ago
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 🙃
Want results from more Discord servers?
Add your server