Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send mailgun using nodemailer-mailgun-transport received connect ECONNREFUSED ::1:587 #1210

Open
tungnt2k opened this issue Jun 26, 2024 · 1 comment

Comments

@tungnt2k
Copy link

I set up this for my backend service:

import * as MailGun from 'nodemailer-mailgun-transport';
import { createTransport } from 'nodemailer';


MailerModule.forRootAsync({
  inject: [ConfigService],
  useFactory: (configService: ConfigService) => {
    console.log({
      api_key: configService.get<string>('MAILGUN_API_KEY'),
      domain: configService.get<string>('MAILGUN_DOMAIN'),
    });

    return {
      transport: createTransport(
        MailGun({
          auth: {
            api_key: configService.get<string>('MAILGUN_API_KEY'),
            domain: configService.get<string>('MAILGUN_DOMAIN'),
          },
        }),
      ),
      defaults: {
        from: '"Incash Option" <[email protected]>',
      },
      template: {
        dir: join(process.cwd(), 'dist', 'libs', 'mail', 'templates'),
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
    };
  },
}),

And I will received the response:

    Error: connect ECONNREFUSED ::1:587

If I just used a simple like this. Everything is ok

    const transporter = createTransport(
      MailGun({
        auth: {
          api_key: configService.get<string>('MAILGUN_API_KEY'),
          domain: configService.get<string>('MAILGUN_DOMAIN'),
        },
      }),
    );

   transporter.sendMail({
      from: '"Incash Option" <[email protected]>',
      to: '[email protected]',
      subject: 'Incash Option',
      text: 'Incash Option',
    })

Please let me know what the right way to use the MailGun service is.
Thanks

@teddyhendryanto
Copy link

teddyhendryanto commented Dec 21, 2024

Hi @tungnt2k
i just got same error with yours and find out the solution.
you just need to add .transporter at the end of createTransport() function.

here's how you fix it.
hope this answer helps you and others that might face same issue like ours.

const transport = MailGun({
  auth: {
    apiKey: this.configService.get('MAILGUN_API_KEY'),
    domain: this.configService.get('MAILGUN_DOMAIN'),
  },
});

return {
  transport: createTransport(transport).transporter,
  defaults: {
    from: '"test" <[email protected]>',
  },
  template: {
    dir: join(process.cwd(), 'dist', 'libs', 'mail', 'templates'),
    adapter: new HandlebarsAdapter(),
    options: {
      strict: true,
    },
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants