I try to send a email message by using mailgun. I use node.js (nest.js) and this is my mail service. What should I change? When I try to send first email (description in mailgun official website) I got the same error message.
import { Injectable } from '@nestjs/common';
import * as Mailgun from 'mailgun-js';
import { IMailGunData } from './interfaces/mail.interface';
import { ConfigService } from '../config/config.service';
@Injectable()
export class MailService {
private mg: Mailgun.Mailgun;
constructor(private readonly configService: ConfigService) {
this.mg = Mailgun({
apiKey: this.configService.get('MAILGUN_API_KEY'),
domain: this.configService.get('MAILGUN_API_DOMAIN'),
});
}
send(data: IMailGunData): Promise<Mailgun.messages.SendResponse> {
console.log(data);
console.log(this.mg);
return new Promise((res, rej) => {
this.mg.messages().send(data, function (error, body) {
if (error) {
console.log(error);
rej(error);
}
res(body);
});
});
}
}
When I try to send message I get 401 error with forbidden description.
My mg (console.log(this.mg))
Mailgun {
username: 'api',
apiKey: '920d6161ca860e7b84d9de75e14exxx-xxx-xxx',
publicApiKey: undefined,
domain: 'lokalne-dobrodziejstwa.pl',
auth: 'api:920d6161ca860e7b84d9de75e14exxx-xxx-xxx',
mute: false,
timeout: undefined,
host: 'api.mailgun.net',
endpoint: '/v3',
protocol: 'https:',
port: 443,
retry: 1,
testMode: undefined,
testModeLogger: undefined,
options: {
host: 'api.mailgun.net',
endpoint: '/v3',
protocol: 'https:',
port: 443,
auth: 'api:920d6161ca860e7b84d9de75e14exxx-xxx-xxx',
proxy: undefined,
timeout: undefined,
retry: 1,
testMode: undefined,
testModeLogger: undefined
},
mailgunTokens: {}
}
My email body
{
from: '[email protected]',
to: '[email protected]',
subject: 'Verify User',
html: '\n' +
' <h3>Hello [email protected]!</h3>\n' +
' '
}
To view your primary account API key, in the Mailgun dashboard click on Settings on the left-hand nav in the Mailgun dashboard and then API Keys and click on the eye icon next to Private API key.
5,000 messages/month are included. There is a limit of 300 messages per day on the included sandbox domain. Data retention for Logs and the Events API is 1 day.
Mailgun is an SMTP service provider for transactional email and email marketing campaigns, offering robust features, support, and analytics—for free!
TextMagic, also a Mailgun customer, allows you to send notifications, alerts, reminders, confirmations and SMS marketing campaign messages to your customers, staff members and suppliers. On average, TextMagic customers are sending over 2.5M text messages around the world each month.
I had this problem when my domain was in an EU zone. When you're using an EU zone, you have to specify it in the config - this isn't clearly explained by Mailgun.
So it would be something like this:
var mailgun = require("mailgun-js")({
apiKey: API_KEY,
domain: DOMAIN,
host: "api.eu.mailgun.net",
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With