Using a Cloudflare Worker for Email on Hugo

I’m trying to add a CF Worker to my Contact page. I want to allow visitors to send me messages with this service: https://blog.cloudflare.com/sending-email-from-workers-with-mailchannels/. In the article, they make it look straightforward: “The only thing you need to do is copy and paste the following code snippet in your /functions/_middleware.ts file.” It didn’t work on my first attempt. My question is on Hugo, would I create and put it in that file? Or should I put that code somewhere else? Thanks for the help and advice!

It’s not Hugo-specific. Cloudflare will “look for” /functions/_middleware.ts in your repo’s top level. There are several such references within Cloudflare documentation.

1 Like

Did you read through the whole post and add the required form parameter to your form or were you swerved by the “the only thing you need to do”?

<form data-static-form-name="contact">

The name can be anything, but data-static-form-name needs to be present for this to work. I also wonder if there might be some setup regarding an account at Mailchannels required? Maybe not, but I would expect it.

You could either post your FULL sample contact form here, or try using the Cloudflare Community Discourse to solve the issue. I am pretty sure I would try “over there” first, because of the Cloudflare specifity.

1 Like

Thanks for the help @bwintx and @davidsneighbour. This is the error I’m getting when I deploy the site. The site deploys just fine until I add the Cloudflare code.

07:11:57.692
07:11:57.692
07:11:57.692 ±-----------------±—+
07:11:57.692 Pages
07:11:57.692 Paginator pages
07:11:57.692 Non-page files
07:11:57.692 Static files
07:11:57.693 Processed images
07:11:57.693 Aliases
07:11:57.693 Sitemaps
07:11:57.693 Cleaned
07:11:57.693
07:11:57.693 Total in 13 ms
07:11:57.698 Finished
07:11:58.105 :construction: ‘wrangler pages ’ is a beta command. Please report any issues to Sign in to GitHub · GitHub
07:11:58.153 ✘ [ERROR] Could not resolve @cloudflare/pages-plugin-mailchannels
07:11:58.153
07:11:58.153 …/…/…/buildhome/repo/functions/_middleware.ts:1:31:
07:11:58.153 1 │ …rt mailchannelsPlugin from @cloudflare/pages-plugin-mailchannels;
07:11:58.153 ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
07:11:58.153
07:11:58.153 You can mark the path @cloudflare/pages-plugin-mailchannels as external to exclude it from the bundle, which will remove this error.
07:11:58.154
07:11:58.156 1 error(s) and 0 warning(s) when compiling Worker.
07:11:58.157
07:11:58.235 e[31m✘ e[41;31m[e[41;97mERRORe[41;31m]e[0m e[1mBuild failed with 1 error:e[0m
07:11:58.236
07:11:58.236 …/…/…/buildhome/repo/functions/_middleware.ts:1:31: ERROR: Could not resolve @cloudflare/pages-plugin-mailchannels
07:11:58.236
07:11:58.236
07:11:58.236 e[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choosee[0m
07:11:58.244 Failed building Pages Functions from /functions.
07:11:59.332 Failed: an internal error occurred

Yes, this is clearly something that you’ll need to run by either the Cloudflare Discourse or the appropriate channel on the Cloudflare Discord; I’ve had good luck with both, but you’ll likely get faster help with the latter.

1 Like

Sure thing. Thanks for the help.

1 Like

Putting the plugin name into google this came up:

https://developers.cloudflare.com/pages/platform/functions/plugins/mailchannels/

There is definitely more work to be done before it works. The current error you are getting is because you need to run the npm install command (first code piece on the plugin link).

1 Like