0

I'm trying to attach images to an email to display them later. I'm using NextJS and I'm doing that using the serverless functions.

  const letter: any = {
    from: 'ME',
    replyTo: 'ME',
    to: `<${email}>`,
    subject: "subject",
    text,
    html,
    attachments: [
      {
        filename: "mastercard.png",
        path: `${__dirname}/mastercard.png`,
        cid: "cider", 
      },
    ],
  };

I keep getting this error:

There was an error with sending the email
[Error: ENOENT: no such file or directory, open '/mnt/stf/coding/donation-platform/.next/server/pages/api/mastercard.png'] {
  errno: -2,
  code: 'ESTREAM',
  syscall: 'open',
  path: '/mnt/stf/coding/app/.next/server/pages/api/mastercard.png',
  command: 'API'
}

I tried moving the image to different directories and changing the path a few times, but nothing works. What is the correct path to images from public directory? (or where to place the images to get them?)

Likepineapple
  • 486
  • 1
  • 4
  • 13
  • 1
    Use `process.cwd()` instead of `${__dirname}`. Check the doc here -> https://nextjs.org/docs/api-reference/data-fetching/get-static-props#reading-files-use-processcwd – MB_ May 28 '22 at 10:48
  • 1
    ``${process.cwd()}/public/mastercard.png`` – MB_ May 28 '22 at 11:01
  • This works locally, what about deploying on vercel? Do you have any idea? I'm getting `Error: ENOENT: no such file or directory, open '/var/task/public/mastercard.png'` – Likepineapple Jun 10 '22 at 13:00
  • 1
    Hi, you need to use `path.join()` like in this example : [Link](https://vercel.com/support/articles/how-can-i-use-files-in-serverless-functions) – MB_ Jun 10 '22 at 13:24
  • 1
    another useful link : [Link #2](https://github.com/vercel/next.js/discussions/32236) – MB_ Jun 10 '22 at 13:26

0 Answers0