Mailchannels simple contact form

I’m trying to build a simple contact form via the mailchannels middleware. I got it to send mail and even figured out how to get a static reply-to and subject header.

How will I get formdata-fields in my mails?

My try so far, which works for environment variables. I would like to have sth like formData.get(‘name’) instead of the variable.

import mailChannelsPlugin from "@cloudflare/pages-plugin-mailchannels";

export const onRequest: PagesFunction = (context) => mailChannelsPlugin({
  personalizations: [
    {
      to: [{ name: "xxx", email: "[email protected]" }],
      headers: { "x-test": "foobar" },
    },
  ],
  from: {
    name: "ACME Support",
    email: "[email protected]",
  },
  subject: context.env.VARIABLE,
  respondWith: () => {
    return new Response(null, {
      status: 302,
      headers: {
        location: "/mail-sent/"
      },
    });
  },
})(context);

I tried changing context.env.VARIABLE to context.request.formData.get(‘name’), but this just throws an error.

2 Likes

I have a similar question. I do get all the form fields in the email, but how to use them in the personalizations? How can I set the reply_to to the submitted email address?