orliesaurus / nodemailer-mailgun-transport

nodemailer is an amazing node module to send emails within any of your nodejs apps. This is the transport plugin that goes with nodemailer to send email using Mailgun 🔫
MIT License
880 stars 97 forks source link

TypeScript: 'template' does not exist in type 'Options' #79

Open alusev opened 5 years ago

alusev commented 5 years ago

I cannot specify a template. I have the following code:

import nodemailer from "nodemailer";
import mg from "nodemailer-mailgun-transport";
import dotenv from "dotenv";
import Mail from "nodemailer/lib/mailer";
import pug from "pug";

dotenv.config();

const {
    MG_API_KEY = "",
    MG_DOMAIN = "",
    MG_FROM_EMAIL = "",
    MG_FROM_NAME = ""
} = process.env;

const auth = {
    auth: {
        api_key: MG_API_KEY,
        domain: MG_DOMAIN
    },
};

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

export interface IMailAddress {
    name?: string;
    address: string;
}

export const sendMail = (to: Mail.Address, subject: string, templateName: string, context: any = {}) => {
    return nodemailerMailgun.sendMail({
        from: { name: MG_FROM_NAME, address: MG_FROM_EMAIL },
        to, subject,
        template: {
            name: templateName,
            engine: 'pug',
            context: context
        }
    });
}

This is the error: Object literal may only specify known properties, and 'template' does not exist in type 'Options'.

JaquelinaG commented 4 years ago

Any news on this error? I have the same problem!

amine-louni commented 2 years ago

Any updates ?

josethz00 commented 2 years ago

any updates?

josethz00 commented 2 years ago

As we don't have an official solution yet, I just downgraded my types/nodemailer version to 6.1.0 and then the problem was solved. I also noticed that this bug is present since the 6.2.2 version.

rohanrajpal commented 2 years ago

Facing the same issue :/

mechadragon01 commented 2 years ago

Any updates?

enyineer commented 1 year ago

You could just extend the Options type like this:

import { Options } from "nodemailer/lib/mailer";

type ExtendedOptions = Options & { template: string, context: Record<string, unknown> };

const options: ExtendedOptions = {
  from: '"enyineer@github" <hi@enking.dev>',
  to: 'someone@github.com',
  template: 'myTemplateName',
  context: {
    foo: 'bar',
  },
};

await transport.sendMail(options);

Fixes the TypeScript complaint until the types are corrected.