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' +
' '
}
Try to send email to yourself (account email) via this command in console:
is it working?
If its not.. I assume you have written api and domain correctly so later if you have free acount you should check authorized recipant on overview section (you cant send email everywhere you want on trial account you have to type it first)
and the
definates where html file with content is located so how it looks (in this case reset-password.html):
and values in {{}} wil be replaced by library automatically
in this example example ResetPasswordReplacement its noly basic object which contains 3 properties and it inherits by IReplacement which is empty interface - made it only for define values in template file
sources: