MailChannels Pages Plugin
The MailChannels Pages Plugin intercepts all form submissions made which have the data-static-form-name attribute set. It then emails these form submissions using the MailChannels API.
Installation
$ npm install @cloudflare/pages-plugin-mailchannels
Usage
functions/_middleware.tsimport mailChannelsPlugin from "@cloudflare/pages-plugin-mailchannels";
export const onRequest: PagesFunction = mailChannelsPlugin({ personalizations: [ { to: [{ name: "ACME Support", email: "support@example.com" }], }, ], from: { name: "ACME Support", email: "support@example.com", }, respondWith: () => { return new Response( `Thank you for submitting your enquiry. A member of the team will be in touch shortly.` ); },
});
public/contact.html<body> <h1>Contact us</h1> <form data-static-form-name="contact"> <label>Email address <input type="email" name="email" /></label> <label>Message <textarea name="message"></textarea></label> <button type="Submit"> </form>
</body>
The Plugin takes a single argument, an object with a number of properties:
personalizationsis required and should be set to either an array, or a function which returns an array. If set to a function, it is passed a single object parameter which contains the incomingrequest(Request),formData(FormData) andname(String). This is useful if you want to populate dynamic values based on the form submission.fromis also mandatory and should be set to either an object, or a function which returns an object. Again, if set to a function, it is passed a single object parameter which containsrequest,formDataandname.subjectis an optionalStringor function which returns a string. It defaults toNew <NAME> form submission.contentis an optional array or function which returns an array. It defaults to atext/htmlandtext/plainbody array, detailing the form submission contents.respondWithis a required function which takes therequest,formDataandnameobject parameter and returns aResponseorPromiseof aResponse. Assuming the form submission is successful, this function will be called to generate response for visitors.
The method and action attributes of the HTML form do not need to be set. The Plugin will automatically override them to allow it to intercept the submission.
For more information about MailChannels and the options they support, refer to the documentation.