nylas / nylas-nodejs

A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.
MIT License
169 stars 118 forks source link

No recipients specified error when using the sdk #426

Closed freitasjuciel closed 9 months ago

freitasjuciel commented 1 year ago

Describe the bug When I try to send an email using the Draft class it returns and No recipients specified error. But when sending the request with the same fields it works properly

To Reproduce Some steps involved to reproduce the bug and any code samples you can share.

Nylas.config({
    clientId: process.env.NYLAS_CLIENT_ID,
    clientSecret: process.env.NYLAS_CLIENT_SECRET,
  });

  const nylas = Nylas.with(decrypt(user.nylasToken, user.nylasTokenIv));

  const draft = new NylasDraft(nylas, {
    subject: emailMessage.subject,
    body: htmlBody.toString(),
    from: [{ name: `${user.firstName} ${user.lastName}`.trim(), email: user.email }],
    to: [{ email: emailThread.email }],
  });

  const result = await draft.send((error, data) => {
    console.log(error);
    console.log(data);
  });

Expected behavior Send the email since the from and to emails are in the body

SDK Version: 6.6.9, 6.7.0, 6.8.0

Additional context When using the request method from nylas it works properly

await nylas.request({
    path: "/send",
    method: "POST",
    json: true,
    body: {
      subject: emailMessage.subject,
      body: htmlBody.toString(),
      from: [{ name: `${user.firstName} ${user.lastName}`.trim(), email: user.email }],
      to: [{ email: emailThread.email }],
      files: files,
    },
  });

image

mrashed-dev commented 1 year ago

Hey @freitasjuciel thanks for opening the issue. I have not been able to reproduce your issue, is it possible to provide what emailThread.email is? If it's not a string that could be what the issue is. Here's the code I tried and it worked just fine:

const Nylas = require("nylas");
const { default: Draft } = require('nylas/lib/models/draft');

Nylas.config({
    clientId: "CLIENT_ID",
    clientSecret: "CLIENT_SECRET",
});

const nylas = Nylas.with("ACCESS_TOKEN");

const draft = new Draft(nylas, {
    subject: "Hello from Nylas",
    body: "Hello world",
    to: [{ email: "you@test.com" }],
    from: [{ name: "me", email: "me@test.com" }],
})

draft.send().then((result) => {
    console.log(result);
});

Any more details would be appreciated!

Dm-Chebotarskyi commented 1 year ago

I can see the same issue by executing this code:

    const { to, subject, body, reply_to_message_id, email_address } =
      event.body;

    console.log('>>> to', to);

    const draft = new Draft(nylas, {
      from: [{ email: email_address }],
      to: [{ email: to }],
      subject: subject,
      body: body,
      replyToMessageId: reply_to_message_id,
    });

And here is the console output:

>>> to you@gmail.com

Bad Request: No recipients specified
    at null.e (/Users/dimach/workspace/app-api/node_modules/nylas/lib/models/nylas-api-error.js:31:28)
    at null.<anonymous> (/Users/dimach/workspace/app-api/node_modules/nylas/lib/nylas-connection.js:215:41)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  errorMapping: {
    '400': 'Bad Request',
    '401': 'Unauthorized',
    '402': 'Request Failed or Payment Required',
    '403': 'Forbidden',
    '404': 'Not Found',
    '405': 'Method Not Allowed',
    '409': 'Conflict',
    '410': 'Gone',
    '418': "I'm a Teapot",
    '422': 'Sending Error',
    '429': 'Too Many Requests',
    '500': 'Server Error',
    '502': 'Server Error',
    '503': 'Server Error',
    '504': 'Server Error'
  },
  statusCode: 400,
  type: 'invalid_request_error'
}
{"level":"info","message":{"response":{"body":"No recipients specified","headers":{"Access-Control-Allow-Credentials":"true","Access-Control-Allow-Origin":"https://dev.exacare.com","Content-Type":"text/plain","Vary":"Origin"},"statusCode":400}}}
djakaitis commented 1 year ago

We are still experiencing this exact issue when using the SDK to create a Draft and send. Is there any resolution here or workaround to still use a Draft object?

Dm-Chebotarskyi commented 1 year ago

We are still experiencing this exact issue when using the SDK to create a Draft and send. Is there any resolution here or workaround to still use a Draft object?

I used a nylas.request as described in the original question to workaround it

relaxedtomato commented 1 year ago

Hi @djakaitis - is this your latest code snippet?:

Nylas.config({
    clientId: process.env.NYLAS_CLIENT_ID,
    clientSecret: process.env.NYLAS_CLIENT_SECRET,
  });

  const nylas = Nylas.with(decrypt(user.nylasToken, user.nylasTokenIv));

  const draft = new NylasDraft(nylas, {
    subject: emailMessage.subject,
    body: htmlBody.toString(),
    from: [{ name: `${user.firstName} ${user.lastName}`.trim(), email: user.email }],
    to: [{ email: emailThread.email }],
  });

  const result = await draft.send((error, data) => {
    console.log(error);
    console.log(data);
  });

I just tested the following on my machine and it works:

  // Import your dependencies
import dotenv from "dotenv/config.js";
import Nylas from "nylas";
import Draft from "nylas/lib/models/draft.js";

// Configure your Nylas client
Nylas.config({
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
});
const nylas = Nylas.with(process.env.ACCESS_TOKEN);

// Create a draft email
const draft = new Draft.default(nylas, {
  subject: "With Love, from Nylas",
  body: "Well well well...",
  to: [{ name: "Recipient name", email: process.env.RECIPIENT_ADDRESS }],
});

// Send the email
try {
  const message = await draft.send();
  console.log(`Message "${message.subject}" was sent with ID ${message.id}`);
} catch (err) {
  console.error("Error:\n", err);
}

I receive the following message in the console:

  Message "With Love, from Nylas" was sent with ID MESSAGE_ID

Here are a few steps to try to see if it helps:

mrashed-dev commented 1 year ago

Hey @djakaitis were you able to resolve the issue with @relaxedtomato's comment or are you still seeing the same issue?

mrashed-dev commented 9 months ago

Closing this issue, @djakaitis let us know if you still need help.

vik-wed commented 7 months ago

Hi folks,

We're experiencing the same issue as above in production, but it does work perfectly fine in dev. We implemented an API call for now, but would love to use the SDK since it works for all other elements we've implemented.

Here is the function that throws the error in prod:

import nylas from "@/lib/nylas/nylasClient"
import Draft from "nylas/lib/models/draft"
import EmailParticipant from "nylas/lib/models/email-participant"

export default async function sendMessage(draftData: {
  to: EmailParticipant[]
  subject?: string
  body?: string
}): Promise<{ threadId: string; messageId: string }> {
  const draft = new Draft(nylas, draftData)
  const request = await draft.send()
  const { threadId, id: messageId } = request
  return { threadId, messageId }
}