How to add custom email sending functionalities for verify email and forgot password ?

default discord avatar
Veera26last year
10

Hello, Do you have any idea about how to add our own custom email sending functionalities for verify email and forgot password auth operations ?



Right now, we are doing the email configuraton in payload.init .



We don't want do it. So, We have to write our own email sending functionalities and configure with verify email and forgot password functionalities.



Do you have the solution , feel free to share . Thanks

  • default discord avatar
    notchrlast year

    Good morning @Veera26



    On Auth-enbabled collections, you can override the generated email with something custom



    https://payloadcms.com/docs/authentication/config#forgot-password


    import { CollectionConfig } from 'payload/types';
    
    export const Customers: CollectionConfig = {
      slug: 'customers',
      auth: {
        forgotPassword: {
          generateEmailHTML: ({ req, token, user }) => {
            // Use the token provided to allow your user to reset their password
            const resetPasswordURL = `https://yourfrontend.com/reset-password?token=${token}`;
    
            return `
              <!doctype html>
              <html>
                <body>
                  <h1>Here is my custom email template!</h1>
                  <p>Hello, ${user.email}!</p>
                  <p>Click below to reset your password.</p>
                  <p>
                    <a href="${resetPasswordURL}">${resetPasswordURL}</a>
                  </p>
                </body>
              </html>
            `;
          }
        }
      }
    };
  • default discord avatar
    Veera26last year

    Okay



    is there a way to disable the mockTransport email configuration ?

  • default discord avatar
    notchrlast year

    Hmm, I think you can disable it by setting logMockCredentials to false -

    https://payloadcms.com/docs/email/overview#mock-transport
  • default discord avatar
    s4chin11 months ago

    user.email giving me error "Property 'email' does not exist on type 'unknown'."

  • default discord avatar
    notchr11 months ago

    @s4chin Hmm that's odd, and just to confirm, that field is on the user collection right? (i think its mandated)



    You may need to run the update types cmd, though I'm not sure on first look



    Are you still having issues @s4chin

  • default discord avatar
    s4chin11 months ago

    here it is



    And yes I m still having this issue after updating types

    image.png
  • default discord avatar
    notchr11 months ago

    @s4chin What happens when you log

    user


    Also, are you able to try logging

    req.user
  • default discord avatar
    s4chin11 months ago

    I can log in using email it just giving me type error thats all and I am able to see that email address in received email as well

  • default discord avatar
    notchr11 months ago

    Right, im trying to identify the type of User in this case and why it doesn't have an email property



    Maybe because User can technically be null



    Which would probably be fixed by wrapping the code in a condition like



    "if (user)"



    i think

  • default discord avatar
    s4chin11 months ago

    user["email"] I did this and issue is solved

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.