Skip to content

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

@vycos-zen

Description

@vycos-zen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions