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

Usage in Typescript #36

Open
clabnet opened this issue Mar 23, 2024 · 0 comments
Open

Usage in Typescript #36

clabnet opened this issue Mar 23, 2024 · 0 comments

Comments

@clabnet
Copy link

clabnet commented Mar 23, 2024

Hi @meghein,
trying to use @autotelic/fastify-mail 0.8.0 with Fastify 4.26 , Typescript 5.4 and Node 20.11.
I have trouble to pass the correct opts object to fastify.mailer.sendMail method.
I cannot instanciate correctly your package. Can you support me please ?
Here a reproducible project.
Thank you in advance.

Into plugins folder I have this file to register the plugin:

/// <reference path="../global.d.ts" />
import { FastifyInstance, FastifyPluginOptions } from "fastify";
const mail = require("@autotelic/fastify-mail")
const defaults = {
  from: "John Doe <[email protected]>",
  subject: "Hello world",
};
const transport = {
  provider: "mailtrap", // using aws to send email
  pool: true,
  host: "sandbox.smtp.mailtrap.io",
  port: 2525,
  secure: false, // use TLS
  auth: {
    user: "xxxxxxxxxxxxx",
    pass: "yyyyyyyyyyyyy",
  },
};
export default async function (
  fastify: FastifyInstance,
  opts: FastifyPluginOptions
) {
  await fastify.register(mail, { pov: { engine: { nunjucks: require('nunjucks') } }, transporter: transport })
  fastify.log.debug("Registered plugin autotelic-fastify-mail");
}

global.d.ts

import {
  PlatformaticApp,
  PlatformaticServiceConfig,
} from "@platformatic/service";
import { Transporter } from "nodemailer";
export interface FastifyMailerNamedInstance {
  [namespace: string]: Transporter;
}
export type FastifyMailer = FastifyMailerNamedInstance & Transporter;
declare module "fastify" {
  interface FastifyInstance {
    platformatic: PlatformaticApp<PlatformaticServiceConfig>;
    mailer: FastifyMailer;
  }
}

routes\root.ts

/// <reference path="../global.d.ts" />
import { FastifyInstance, FastifyPluginOptions } from "fastify";

import fastifyMail from "@autotelic/fastify-mail";
import Mail from "nodemailer/lib/mailer";

export default async function (
  fastify: FastifyInstance,
  opts: FastifyPluginOptions
) {
  fastify.post("/send-compose", async (request, reply) => {
    const message = {
      to: "[email protected]",
      from: "[email protected]",
      cc: "[email protected]",
      bcc: "[email protected]",
      subject: "This is a subject",
    };

//  ====  ERROR ZONE =====================================
    const opts : Mail.Options = {
      templatePath: "./templates/",
      context: { name: 'Test Name', sender: '[email protected]' }
    };

    const queued = fastify.mailer.sendMail(opts);
// ====  ERROR ZONE END =====================================

    if (queued.error) {
      const { error } = queued;
      reply.send(error);
    } else {
      const { messageId } = queued;
      reply.send({ messageId });
    }
  });
}

package.json

...
  "dependencies": {
    "@fastify/view": "^8.2.0",
    "fastify-plugin": "^4.5.1",
    "nodemailer": "^6.9.8",
    "nunjucks": "^3.2.4"
  },
  "devDependencies": {
    "fastify": "^4.25.2",
    "fastify-cli": "^6.0.0",
...    
  },
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

1 participant