cloudflare / workers-sdk

โ›…๏ธ Home to Wrangler, the CLI for Cloudflare Workersยฎ
https://developers.cloudflare.com/workers/
Apache License 2.0
2.69k stars 702 forks source link

๐Ÿš€ Feature Request: Support/don't crash when using `send_mail` in miniflare #4570

Open jonasb opened 11 months ago

jonasb commented 11 months ago

Describe the solution

When adding import { EmailMessage } from 'cloudflare:email'; to index.ts (following the docs) I get the following error when running wrangler dev:

[wrangler:inf] Ready on http://localhost:8787
โœ˜ [ERROR] service core:user:my-project: Uncaught Error: No such module "cloudflare-internal:email".

    imported from "cloudflare:email"

โœ˜ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

I don't need to send emails locally but it would be nice if I could at least run my project locally. Maybe if Miniflare just writes a log message when I try to send an email?

jonasb commented 11 months ago

It works as well if I run it with --remote: npx wrangler dev --remote

mrbbot commented 11 months ago

Hey! ๐Ÿ‘‹ Thanks for raising this. Agree importing cloudflare:email probably shouldn't throw. Logging seems like a nice simple solution too. Will get this added to the backlog. ๐Ÿ‘

shyim commented 10 months ago

If someone needs a workaround; This works fine for me.

    if (env.MAIL_ACTIVE === 'false') {
        console.log(`Sending mail to ${mail.to} with subject ${mail.subject}`)
        console.log(mail.body);
        return;
    }

    const cfMail = await import('cloudflare:email');
    const email = new cfMail.EmailMessage(env.MAIL_FROM, mail.to, msg.asRaw());
andrew-pyle commented 6 months ago

What does the error message mean? If it is telling me that the email binding is not available in local development, shouldn't that just be a [WARNING]?

โœ˜ [ERROR] service core:user:REDACTED-PROJECT-NAME: Uncaught Error: No such module "cloudflare-internal:email".

    imported from "cloudflare:email"
tony95271 commented 6 months ago

The --remote option does not work:

C:\work\src\still-wave-a176>yarn run start --remote
yarn run v1.22.22
$ wrangler dev --remote
 โ›…๏ธ wrangler 3.53.1
-------------------
โ–ฒ [WARNING] Enabling Node.js compatibility mode for built-ins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details.

[wrangler:inf] Ready on http://127.0.0.1:8787
โœ˜ [ERROR] Error while creating remote dev session: TypeError: fetch failed

      at fetch (C:\work\src\still-wave-a176\node_modules\wrangler\wrangler-dist\cli.js:17033:19)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async createPreviewSession
  (C:\work\src\still-wave-a176\node_modules\wrangler\wrangler-dist\cli.js:202569:28)
      at async start
  (C:\work\src\still-wave-a176\node_modules\wrangler\wrangler-dist\cli.js:203154:9) {
    cause: Error: read ECONNRESET
        at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
      errno: -4077,
      code: 'ECONNRESET',
      syscall: 'read'
    }
  }

node --version v21.7.1

BTW: this doc example reply email worker does not work https://developers.cloudflare.com/email-routing/email-workers/reply-email-workers/

vickyRathee commented 2 months ago

This is still throwing error in Wrangler 3.72.2, any solution?

Uncaught Error: No such module "cloudflare-internal:email".
    imported from "cloudflare:email"
Jk3ndo commented 2 months ago

Hello Guys, me I have instead the following error when trying to use cloudflare:email inside my worker. Should I understand that I cannot use it inside the worker directly ? I am using wrangler 3.72.2


X [ERROR] Could not resolve "cloudflare:email"

    src/services/sendReports.ts:23:32:
      23 โ”‚     const cfMail = await import('cloudflare:email');
         โ•ต                                 ~~~~~~~~~~~~~~~~~~

  You can mark the path "cloudflare:email" as external to exclude it from the bundle, which will
  remove this error. You can also add ".catch()" here to handle this failure at run-time instead of
  bundle-time.```
vickyRathee commented 2 months ago

@Jk3ndo How to mark external?

Jk3ndo commented 2 months ago

Sorry for late reply @vickyRathee .. Indeed I have marked it as external and it worked. Now it depends on your bundler. For vite.js you can do the following and the issue should be solved.

export default {
  build: {
    rollupOptions: {
      external: ['cloudflare:email'],
    },
  },
};
vickyRathee commented 2 months ago

@Jk3ndo i build with wrangler

wrangler pages ./public โ€”kv=CACHE โ€”d1=testdb

gerhardcit commented 2 months ago

Any advice on how one get to test the flow of emails in development with pages (in a sveltekit project?) Documentation seems very basic on this topic. Does not seem like it is design for pages, or am I missing something?

Normally there is a variety of factors that would make up an email to be send. Is this the tool for that or do we are we suppose to use something external. Can cloudflare please comment on that?

Jk3ndo commented 2 months ago

@Jk3ndo i build with wrangler

wrangler pages ./public โ€”kv=CACHE โ€”d1=testdb

Hello @vickyRathee , I am sorry but I can't help since I never did it like. But I will check and let you know if I find a way.

gerhardcit commented 2 months ago

Any advice on how one get to test the flow of emails in development with pages (in a sveltekit project?) Documentation seems very basic on this topic. Does not seem like it is design for pages, or am I missing something?

Normally there is a variety of factors that would make up an email to be send. Is this the tool for that or do we are we suppose to use something external. Can cloudflare please comment on that?

In case someone else passes by with this problem. My solution was to singup with https://resend.com/ and 10 minutes later I had my mail delivered. Quicker than trying to find out how this works.