Send emails from Workers fails with "Cannot read properties of undefined (reading '

I’m following the official documentation.

To get one step further I’d to comment out compatibility_flags = ["nodejs_compat"] in wrangler.toml and add node_compat = true (with compatibility_date = “2024-05-02”).

However, the request to the Cloudflare Worker fails with: “Cannot read properties of undefined (reading ‘send’)”.

Here is the full wrangler.toml content:

#:schema node_modules/wrangler/config-schema.json
name = "poc-cloudflare-email"
main = "src/index.ts"
compatibility_date = "2024-05-02"
# compatibility_flags = ["nodejs_compat"]

node_compat = true

[vars]
send_email = [
    {type = "send_email", name = "SEB", destination_address = "[email protected]"},
]

And the source code:

/**
 * Welcome to Cloudflare Workers! This is your first worker.
 *
 * - Run `npm run dev` in your terminal to start a development server
 * - Open a browser tab at http://localhost:8787/ to see your worker in action
 * - Run `npm run deploy` to publish your worker
 *
 * Bind resources to your worker in `wrangler.toml`. After adding bindings, a type definition for the
 * `Env` object can be regenerated with `npm run cf-typegen`.
 *
 * Learn more at https://developers.cloudflare.com/workers/
 */

import { EmailMessage } from "cloudflare:email";
import { createMimeMessage } from "mimetext";

export default {
	async fetch(request, env) {
		const msg = createMimeMessage();
		msg.setSender({ name: "GPT-4", addr: "[email protected]" });
		msg.setRecipient("[email protected]");
		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(
			"[email protected]",
			"[email protected]",
			msg.asRaw()
		);
		try {
			console.log(JSON.stringify(env));
			await env.SEB.send(message);
		} catch (e) {
			return new Response(e.message);
		}

		return new Response("Hello Send Email World!");
	},
};

Further debug info: The variable env has the following value:
{“send_email”:[{“destination_address":"[email protected]”,“name”:“SEB”,“type”:“send_email”}]}

Any suggestions on how to solve this, please?

SEB is a JSON (as you’ve seen). This is because you’ve put the send_email under [vars] so it’s treating it as an env var.
TOML is not the best config lang so making this mistake is far too common.

You’ll want your TOML to be:

#:schema node_modules/wrangler/config-schema.json
name = "poc-cloudflare-email"
main = "src/index.ts"
compatibility_date = "2024-05-02"
# compatibility_flags = ["nodejs_compat"]

node_compat = true

send_email = [
    {type = "send_email", name = "SEB", destination_address = "[email protected]"},
]

[vars]
ENV_VAR = "true"

Thank you very much. My bad.

In case Cloudflare is interested in improving the documentation, I suggest to add this to the docs explicitly. Moveover, a note on the requirement for node_compat = true would be helpful, too.

Thanks again.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.