Search code examples
next.jscontent-management-systemnodemailerpayload-cms

Send Verification Email error, where is the problem?


I'm new to PayloadCMS and I'm trying to build a new webapp. I arrived at the step where I try to implement email verification, although I'm encountering a strange error I cannot understand... That's why I wanted to ask for your help! I'm using resend and the custom NodeMailer transport (https://payloadcms.com/docs/email/overview#use-a-custom-nodemailer-transport). Here is the following setup for email verification I have made :

I observe the following behaviour when the user is created (should I precise user is indeed created):

TypeError: req.get is not a function
    at sendVerificationEmail (C:\git\myapp\node_modules\payload\src\auth\sendVerificationEmail.ts:35:36)
    at create (C:\git\myapp\node_modules\payload\src\collections\operations\create.ts:269:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
 ⨯ unhandledRejection: TypeError: req.get is not a function
    at sendVerificationEmail (C:\git\myapp\node_modules\payload\src\auth\sendVerificationEmail.ts:35:36)
    at create (C:\git\myapp\node_modules\payload\src\collections\operations\create.ts:269:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
 ⨯ unhandledRejection: TypeError: req.get is not a function
    at sendVerificationEmail (C:\git\myapp\node_modules\payload\src\auth\sendVerificationEmail.ts:35:36)
    at create (C:\git\myapp\node_modules\payload\src\collections\operations\create.ts:269:28)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Expected behaviour: Email should be sent

Setup :

Transporter :

const transporter = nodemailer.createTransport({
  host: 'smtp.resend.com',
  secure: true,
  port: 465,
  auth: {
    user: 'resend',
    pass: process.env.RESEND_API_KEY,
  },
})

Payload init:

cached.promise = payload.init({
      email: {
        transport: transporter,
        fromAddress: '[email protected]',
        fromName: 'MyApp',
      },
      secret: process.env.PAYLOAD_SECRET,
      local: initOptions?.express ? false : true,
      ...(initOptions || {}),
    })

User model:

const Users: CollectionConfig = {
    slug: "users",
    auth: {
        verify:{
            generateEmailHTML: ({req, token, user}) => {
                // Use the token provided to allow your user to verify their account
                const url = `https://yourfrontend.com/verify?token=${token}`
                return `Hey ${user.email}, verify your email by clicking here: ${url}`
            }
        }
    },
    access: {
        read: () => true,
        create: () => true,
    },
    fields:[
        {
            name: "role",
            defaultValue: "user",
            required: true,

            type: "select",
            options: [
                {label: "Admin", value: "admin"},
                {label: "User", value: "user"}
            ]
        },
        {
            name: "username",
            required: true,
            type: "text"
        }
    ]
}

Solution

  • in src/payload.config.ts, you have mis-typed env variable

    export default buildConfig({
        // not PUBLICK
        serverURL: process.env.NEXT_PUBLICK_SERVER_URL || '',
        ....
    })
    

    in every else you are correctly using process.env.NEXT_PUBLIC_SERVER_URL. this bug might be causing the issue