email service

default discord avatar
muper
7 months ago
17

Hi, I need help to config email service in my payload cms



I already read the doc but still I think some part are a little bit confusing

  • default discord avatar
    Panda
    7 months ago
    const transport = nodemailer.createTransport({
        host: process.env.PAYLOAD_NODEMAILER_HOST,
        port: parseInt(process.env.PAYLOAD_NODEMAILER_PORT),
        secure: false,
        auth: {
          user: process.env.PAYLOAD_NODEMAILER_USER,
          pass: process.env.PAYLOAD_NODEMAILER_PASSWORD,
        },
      });
    // Initialize Payload
      await payload.init({
        secret: process.env.PAYLOAD_SECRET,
        mongoURL: process.env.MONGODB_URI,
        express: app,
        onInit: async () => {
          payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
        },
    email: {
          fromAddress: 'example@example.com',
          fromName: 'example',
          transport,
        },
      });
    
    
    ./env
    # using mailtrap here
    PAYLOAD_NODEMAILER_HOST=sandbox.smtp.mailtrap.io
    PAYLOAD_NODEMAILER_PORT=465
    PAYLOAD_NODEMAILER_USER=userId
    PAYLOAD_NODEMAILER_PASSWORD=userPassword


    here's mine that's using mailtrap for dev

  • discord user avatar
    denolfe
    Payload Team
    7 months ago
    I already read the doc but still I think some part are a little bit confusing

    You'll have to be more specific in what is confusing. What have you tried? Are you getting an error? Are you using an existing nodemailer transport?

  • default discord avatar
    muper
    7 months ago

    actually I'm not a backend developer so I'm not very professional in it, but let me try the sample panda shared to me



    hi, panda please can you explain me that how you'll use this config through your code, because I want to use it whenever the user submit a form

  • default discord avatar
    Panda
    7 months ago

    You use req.payload.sendEmail(...) and fill in the args, sorry I'm not at home now, if you struggle I can help you later

  • discord user avatar
    denolfe
    Payload Team
    7 months ago

    So in order to use an email within Payload, you'll need to sign up for an email service somewhere. Some workplaces have an SMTP server that can be used. Others used like AWS SES, Sendgrid, and more. Have you decided which service you'll be using?

  • default discord avatar
    muper
    7 months ago

    I decided to use mailtrap which is good I think, isn't it?



    then I should chose SMTP in their configuration ?





    I configured the file, but my question is where is the message and body



    and how can I call this with req.payload.sendEmail



    because I need to use this whenever the user post data to backend



    all done guys, thank you

    image.png
  • discord user avatar
    denolfe
    Payload Team
    7 months ago

    Yeah, once configured, it will be automatically used by

    sendEmail


    Glad you got it sorted

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.