eleith / emailjs

html emails and attachments to any smtp server with nodejs
MIT License
2.19k stars 230 forks source link

TextEncoder is not a constructor error in nextjs server side api #332

Closed vycos-zen closed 1 year ago

vycos-zen commented 1 year ago

Hi,

I ran into an issue when implementing emailjs SMTPClient in a NextJs server side api.

the error: TypeError: util__WEBPACK_IMPORTED_MODULE_3__.TextEncoder is not a constructor

implementation:

import { SMTPClient } from "emailjs";

export const emailClient = () =>
  new SMTPClient({
    user: process.env.EMAILJS_USER,
    password: process.env.EMAILJS_PASSWORD,
    host: process.env.EMAILJS_HOST,
    ssl: true,
  });

api endpoint call:

  try {
    const { email } = req?.body;

    if (!email) {
      return res.status(500)?.json({
        message: "Missing email",
      });
    }

    const client = emailClient();

    const textConstruct = `bruderbau.hu kapcsolat felvétel üzenet: 

      ${email.text}`;

    const messageConstruct = `Üzenet ${email.from} feladótól, bruderbau.hu kapcsolat felvétel`;
    const from = email.from;

    const message = await client.sendAsync({
      text: textConstruct,
      from: from,
      to: process.env.EMAILJS_DEFAULT_RECIPENT ?? "",
      subject: messageConstruct,
    });

    return res.status(200)?.json({
      message: message.text,
    });
  } catch (error) {
    res.status(500)?.json({
      message: "Error submitting email",
    });
  }

call stack extract: Screenshot 2022-12-28 at 14 04 37

i want to release this feature to my client as soon as possible. please let me know if it`s something on my side, or more info needed. Also if no possibility of fixing it, so I look for an alternative solution.

Best regards, Peter