send email to user after create request using payload

default discord avatar
bhavikak
last month
6

is it possible when user subscribe by email after post request i want to send email

  • discord user avatar
    tylandavis
    Payload Team
    last month

    @bhavikak You can use a

    beforeChange

    or

    afterChange

    Hook that runs during a

    create

    operation on your collection.

  • discord user avatar
    denolfe
    Payload Team
    last month

    Here is a code snippet example of a collection afterchange hook:



    hooks: {
        afterChange: [
          ({ operation, req }) => {
            if (operation === 'create') {
              req.payload.sendEmail({
                to: 'john@doe.com',
                from: 'test@test.com',
                subject: 'Welcome!',
                text: 'Thank you for subscribing',
              });
            }
          },
        ],
      },
  • default discord avatar
    bhavikak
    last month

    Thanks @denolfe. I'll Try This.



    can we send proper email template?



    and is anything else that i have to do for send email to end user?

  • discord user avatar
    denolfe
    Payload Team
    last month

    @bhavikak sendEmail can accept an

    html

    property that can be a full email template you've created.



    For email sending, you'll need to hook up a proper email service. Docs here:

    https://payloadcms.com/docs/email/overview#email-functionality
  • default discord avatar
    bhavikak
    last month

    okay i'll try, Thanks @denolfe



    hello @denolfe we have tried with your given hook example but it's not working.



    can you give me a full example or perfect reference.



    we want to use google service for email

  • discord user avatar
    denolfe
    Payload Team
    last month
    but it's not working.

    You'll need to be more specific.



    we want to use google service for email

    You'll need to use nodemailer's OAuth2 transport in order to send email using Gmail. Their docs on configuration are here:

    https://nodemailer.com/smtp/oauth2/

    In all honesty, unless you

    really need to send emails from gmail

    , I'd recommend going with another provider for sending email like Resend, SendGrid, or similar. The OAuth configuration for nodemailer looks cumbersome.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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