#Issues using Mailgun with Payload for Email

17 messages · Page 1 of 1 (latest)

tame fiber
#

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 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);
Payload CMS

Enabling Authentication automatically makes key operations available such as Login, Logout, Verify, Unlock, Reset Password and more.

Payload CMS

Payload uses NodeMailer to allow you to send emails smoothly from your app. Set up email functions such as password resets, order confirmations and more.

wanton wave
#

@tame fiber do you receive any specific error messages?

tame fiber
#

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

noble tide
#

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

wanton wave
#

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

noble tide
#

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

tame fiber
#

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

noble tide
#

@tame fiber 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

cedar wharf
#

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 { 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',
  });
polar creek
#

Hi @cedar wharf,

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

GitHub

Context After configuring the email service, I wanted to continue developing my authentication flow. Below you can find details upon this journey. I already have a front-end app that I used as a to...

#

tldr; is the config in your Mailgun account correct?

cedar wharf
#

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

cedar wharf
#

Alright, so the issue was that I was passing ...transporter instead of transport: transporter

polar creek
#

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