mailjet / mailjet-apiv3-nodejs

[API v3] Official Mailjet API v3 NodeJS wrapper
https://dev.mailjet.com
MIT License
236 stars 69 forks source link

Which type of Setup to use (Next.js v13, Node v18) #246

Closed RobinWitt closed 1 year ago

RobinWitt commented 1 year ago

Hello, I am currently trying to setup a Contact-Form forwarding handler that sends the formData via Mailjet to Email Client.

I am confused by the Setup guideline. There is "Basic Setup", "API-Setup" and "send your first Email". Each of these look different. I tried different methods, the closest I came to was an error in my console stating "API_KEY is required". (I checked, my env variables are working) What should I use to get it working when using an api handler route in next.js? Can I use "import" instead of "require"?

Thanks in advance

grafik

this is what my current code looks like:


import Mailjet from "node-mailjet";

export default async function barContactHandler(data) {
  const { subject, content, email } = await data;
  console.log(subject, content, email);

  try {
    const mailjet = new Mailjet({
      apiKey: process.env.MJ_APIKEY_PUBLIC,
      apiSecret: process.env.MJ_APIKEY_PRIVATE,
    });

    const request = mailjet
      .apiConnect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
      .post("send", { version: "v3.1" })
      .request({
        Messages: [
          {
            From: {
              Email: process.env.MAILJET__SENDER_EMAIL,
              Name: "Website Bar Contact",
            },
            To: [
              {
                Email: process.env.MAILJET_RECIPIENT_EMAIL,
                Name: "Robin",
              },
            ],
            Subject: subject,
            TextPart: `Hello Robin,
            you reveiced a new Message via the Bar Contact Form:
            ________________________________________________
            ${content}
            ________________________________________________
            from ${email}`,
            //   HTMLPart:
            //     '<h3>Dear passenger 1, welcome to <a href="https://www.mailjet.com/">Mailjet</a>!</h3><br />May the delivery force be with you!',
          },
        ],
      });
    request
      .then((result) => {
        console.log(result.body);
      })
      .catch((err) => {
        console.log(err.statusCode);
      });
  } catch (error) {
    console.error(error);
  }
}
ai-wintermute commented 1 year ago

Hi @RobinWitt you should either use new Mailjet(...) syntax or the static method of the Mailjet client class Mailjet.apiConnect(....). You can use both require and import statements. Here're examples of possible usage:

const Mailjet = require("node-mailjet");  
const mailjet = new Mailjet({
      apiKey: process.env.MJ_APIKEY_PUBLIC,
      apiSecret: process.env.MJ_APIKEY_PRIVATE,
    });
const request = mailjet
            .post("send", { version: "v3.1" })
....

or

const Mailjet = require("node-mailjet");  
const request = Mailjet
      .apiConnect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
.....

or

import { Client } from "node-mailjet"; 
const mailjet = new Client({
      apiKey: process.env.MJ_APIKEY_PUBLIC,
      apiSecret: process.env.MJ_APIKEY_PRIVATE,
    });
const request = mailjet
            .post("send", { version: "v3.1" })
....

or

import { Client } from "node-mailjet"; 
const request = Client
            .apiConnect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
....

or even

import * as Mailjet from "node-mailjet";
const mailjet = new Mailjet.Client({
      apiKey: process.env.MJ_APIKEY_PUBLIC,
      apiSecret: process.env.MJ_APIKEY_PRIVATE,
    });
const request = mailjet
            .post("send", { version: "v3.1" })
....

or

import * as Mailjet from "node-mailjet";
const request = Mailjet.Client
             .apiConnect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
....

Please let us know if something is unclear.

RobinWitt commented 1 year ago

thanks a lot!