How to send large attachments from Node SDK

Using Typescript:

import Nylas, { CreateAttachmentRequest, SendMessageRequest } from "nylas";
import fs from "fs"

const nylas = new Nylas({
  apiUri: "https://api.us.nylas.com",
  apiKey:
    "{REDACTED}",
  timeout:90000
});


async function readFile(filename:string, contentType:string):Promise<CreateAttachmentRequest> {
  const data = fs.createReadStream(`./${filename}`);
  const fileStats = fs.statSync(`./${filename}`);

  return {
    content: data,
    contentType: `${contentType}`,
    filename: `${filename}`,
    size:fileStats.size
  }
}

const files = [{filename:"file1.txt", contentType:"text/plain"}, {filename:"file2.txt", contentType:"text/plain"}]
async function sendAsyncMessage() {
  let pdfAttachments:CreateAttachmentRequest[] = []

  for (let file of files) {
    const { filename, contentType } = file
    const attachment = await readFile(filename, contentType)
    pdfAttachments.push(attachment)
  }

  const message:SendMessageRequest = {
    to: [
      {
        email: "test@test.com",
      },
    ],
    subject: `Test - attachment test - ${Math.random()}`,
    body: "Test body",
    attachments:pdfAttachments
  };
  console.log(message)
  try {
 
    const { data } = await nylas.messages.send({
      identifier: "test@test.com",
      requestBody: message,
    });
    console.log(data)
  } catch (error) {
    console.log(error)
    console.log("Catching send error");
  }
}


(async () => {
  try {
    await sendAsyncMessage();

  } catch (error) {
    console.log("Catching timeout error 2");
  }
})();

 

Using plain JavaScript:

const { default: Nylas } = require("nylas");
const fs = require("fs");


const nylas = new Nylas({
apiUri: "https://api.us.nylas.com",
apiKey: "{REDACTED}",
timeout: 90000
})

async function readFile(filename, contentType) {
const data = fs.createReadStream(`./${filename}`);
const fileStats = fs.statSync(`./${filename}`);

return {
content: data,
contentType: `${contentType}`,
filename: `${filename}`,
size: fileStats.size,
};
}

const files = [
{ filename: "image1.jpeg", contentType: "image/jpeg" },
{ filename: "image2.jpg", contentType: "image/jpg" },
];

async function sendAsyncMessage() {
let attachmentData = [];

for (let file of files) {
const { filename, contentType } = file;
const attachment = await readFile(filename, contentType);
attachmentData.push(attachment);
}

const message = {
to: [
{
email: "test@gmail.com",
},
],
subject: `Test - attachment test - ${Math.random()}`,
body: "Test body",
attachments: attachmentData,
};

console.log(message);

try {
const { data } = await nylas.messages.send({
identifier: "test@test.com",
requestBody: message,
});
console.log(data);
} catch (error) {
console.log(error);
console.log("Catching send error");
}
}

(async () => {
try {
await sendAsyncMessage();
} catch (error) {
console.log("Catching timeout error 2");
}
})();

Remove type: module from the package.json.

 

For more information, see our documentation:

- Working with email attachments  

- Using the Node.js SDK 

Updated

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.