Send an email when new entry is created

default discord avatar
.gustavocardoso12 months ago
2

Hello everyone!



Is it possible to send an email every time a new entry is created?



Thanks!!!

  • default discord avatar
    derosul12 months ago

    Yes! you can do this by adding a hook to your collection:



    https://payloadcms.com/docs/hooks/collections


    Here's a little snippet:



    import { AfterChangeHook } from "payload/dist/collections/config/types";
    
    export const sendMailAfterCreate: AfterChangeHook = ({ req, operation, doc }) => {
      //we only want to handle "create operations".
      if(operation !== 'create') {
        return;
      }
    
      const {payload} = req;
    
      payload.sendEmail({
        from: '"Fred Foo 👻" <foo@example.com>', // sender address
        to: "bar@example.com, baz@example.com", // list of receivers
        subject: "Hello ✔", // Subject line
        text: "Hello world?", // plain text body
        html: "<b>Hello world?</b>", // html body
      });
    }
  • default discord avatar
    .gustavocardoso12 months ago

    Thank you buddy. I completely forgot about the hooks!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.