Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Issues using Mailgun with Payload for Email

default discord avatar
just_board9 months ago
4

Hello there, I have spent a great deal of time looking over the docs


https://payloadcms.com/docs/authentication/operations
https://payloadcms.com/docs/email/overview#use-an-email-service
https://nodemailer.com/smtp/
https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-via-api

I have tried two different ways to configure the email and have not yet been successful, if anyone sees what I am doing wrong and could be of some assistance that would be very much appreciated. Feeling very human and open to being humbled. 😄



import express from "express";
import payload from "payload";
require("dotenv").config();
const app = express();

// import Mailgun from "mailgun.js";
// import formData from "form-data";
// require("form-data");
// require("mailgun.js");
// const mailgunKey = process.env.MAILGUN_API_KEY;
// const mailgunDomain = process.env.MAILGUN_DOMAIN;
// const mailgunForm = new Mailgun(formData);
// const mg = mailgunForm.client({
//   username: "api",
//   key: process.env.MAILGUN_API_KEY,
// });

require('nodemailer');
require('nodemailer-mailgun-transport');
import nodemailer from 'nodemailer';
import mg from 'nodemailer-mailgun-transport'; 
const mailgunKey = process.env.MAILGUN_API_KEY;
const mailgunDomain = process.env.MAILGUN_DOMAIN;
const auth = {
  auth: {
    api_key: mailgunKey,
    domain: mailgunDomain
  }
}

const transport =  nodemailer.createTransport(mg(auth));

// Initialize Payload
payload.init({
  secret: process.env.PAYLOAD_SECRET,
  mongoURL: process.env.MONGODB_URI,
  express: app,
 
  email: {
    // transportOptions: mg,
     transport: transport,
    //logMockCredentials: true,
    fromName: process.env.FROM_NAME,
    fromAddress: process.env.FROM_ADDRESS,
  },
  onInit: () => {
    payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
  },
});

// Add your own express routes here
// Redirect root to Admin panel
app.get("/", (_, res) => {
  res.redirect("/admin");
});

app.listen(3001);


Oh I am just an idiot, left it over the weekend, came back monday and it was working



Yo keep threads like this, I use them all the time as adhoc examples, suuuuuuper valuble to me as a developer

  • default discord avatar
    notchr2 years ago
    @741147904017956896

    do you receive any specific error messages?



    It's tough because a lot of times people come back to a related thread and want to continue the discussion

  • discord user avatar
    jarrod_not_jared
    2 years ago

    I’m not sure what we should do with these threads. Should we delete them? Make a special tag for them?



    If we mark it as “answered” it will end up in community help on our website. But should it?? I’m not sure



    I agree, we could make a 'non-issue' tag or similar



    @741147904017956896

    yeah likely we will use a tag. I have no problem with your post, it’s just that you asked a question and then figured it out yourself but didn’t provide an answer, which is fine. I’m just not sure we should mark it as “answered” bc then it will end up in community help

  • default discord avatar
    tonymif.11 months ago

    I have a similar issue



    I am hooking up Mailgun to PayloadCMS (v2) custom server however I am trying to understand how the reset password and emails work as reset password indicates that the email is sent but in reality it isnt. What am I missing here?




    import MailGun from 'nodemailer-mailgun-transport';
    import { createTransport } from 'nodemailer';
    
    const auth = {
      auth: {
        api_key: mailgunAPIKey,
        domain: mailgunDomain,
      },
      host: 'api.eu.mailgun.net'
    };
    
    const transporter = createTransport(MailGun(auth));
    
    const start = async (): Promise<void> => {
      const payload = await getPayloadClient({
        initOptions: {
          express: app,
          onInit: async (newPayload) => {
            newPayload.logger.info(`Payload Admin URL: ${newPayload.getAdminURL()}`);
          },
          email: {
            fromName: 'Payload CMS',
            fromAddress: 'info@example.com',
            ...transporter,
          },
        },
        seed: process.env.PAYLOAD_PUBLIC_SEED === 'true',
      });


    mailgun's config is correct as I had done prior tests within the server.ts file



    seems to me I need to configure the server.ts file to use

    payload.init()

    i'll keep everyone updated wrt this



    Alright, so the issue was that I was passing

    ...transporter

    instead of

    transport: transporter

    I had a look at the types accepted by the object

  • default discord avatar
    jeffreyarts11 months ago

    Hi

    @231710785460633600

    ,



    Recently I just did this for the first time as well, in my scenario it threw me an error in the console that explained me what went wrong. I just configured it via the configuration it in payload.init



    await payload.init({
        // ...
        email: {
            fromName: "Admin",
            fromAddress: process.env.SERVER_ADMIN_MAIL,
            transportOptions: {
                host: process.env.SMTP_HOST,
                auth: {
                    user: process.env.SMTP_USERNAME,
                    pass: process.env.SMTP_PASSWORD,
                },
                port: Number(process.env.SMTP_HOST),
              secure: Number(process.env.SMTP_PORT) === 465, // true for port 465, false (the default) for 587 and others
                requireTLS: true,
            },
        },
        // ...

    .env
    SMTP_HOST=smtp.mailgun.org
    SMTP_PORT=587
    SMTP_USERNAME=<mailgun_emailddress-username>
    SMTP_PASSWORD=<password>
    SERVER_ADMIN_MAIL=<emailaddress>


    I documented my process on Github, its quite the extensive read, but it might give you some insights in resolving this issue yourself.


    https://github.com/payloadcms/payload/discussions/7487

    tldr; is the config in your Mailgun account correct?



    That's silly 😔, how dit you figure it out eventually?

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.