nextjs/emailjs 无法找到图像附件的路径

huangapple go评论61阅读模式
英文:

nextjs/emailjs can't find path for image attachment

问题

我正在使用NextJS和emailJS(npm包,不要与emailjs.com服务混淆)来发送带有图像附件的电子邮件。我似乎无法弄清楚为什么,但每当我尝试在发送电子邮件时附加文件时,我会收到错误消息:
错误:image.jpeg不存在

我在/public/image.jpeg中有一个文件image.jpeg,我尝试了以下组合:

/image.jpeg
/public/image.jpeg
../../public/image.jpg(从emailjs文件的相对路径)
localhost:3000/image.jpeg
还尝试了从互联网上提取的图像,只需:www.url.com/images/image.jpeg

这些都似乎不起作用

我是这样构建我的消息的:

const message = { ... 
attachment: [ {data: ..., alternative: true},
 { path: '/image.jpeg', type: 'image/jpeg', name: 'image.jpeg' }, ], 
}; 

我在这里缺少什么?谢谢。

英文:

I'm using NextJS and emailJS (npm package, not to be confused with emailjs.com service) to send email with image attachment. I can't seem to figure out why, but whenever I try to attach a file while sending email, I get the error:
Error: image.jpeg does not exist

I have a file image.jpeg in /public/image.jpeg, I've tried these combinations:

/image.jpeg
/public/image.jpeg
../../public/image.jpg (relative path from emailjs file)
localhost:3000/image.jpeg
Also tried to pull one off of internet, just: www.url.com/images/image.jpeg

None of these seem to work

I'm constructing my message this way:

const message = { ... 
attachment: [ {data: ..., alternative: true},
 { path: '/image.jpeg', type: 'image/jpeg', name: 'image.jpeg' }, ], 
}; 

What am I missing here? Thanks.

答案1

得分: 0

path: '/public/image.jpeg';
这应该允许您将文件附加到电子邮件消息中。

英文:

Since the file is located in the /public directory, you should use the following value for the path property:

path: '/public/image.jpeg'

This should allow you to attach the file to your email message.

答案2

得分: 0

以下是翻译好的部分:

"我想说的一个重要事情是,我正在在nextJS的/api文件夹中使用这个,我想这个处理路径的方式可能会有点不同。

需要导入"path"并使用这个:

const publicDirectory = path.join(process.cwd(), 'public');

来获取公共文件夹的实际路径。"

英文:

One major thing I've missed to say was that I'm using this in nextJS /api folder which I suppose handles paths a little different.

Answer:
Need to import "path" and use this:

const publicDirectory = path.join(process.cwd(), 'public');

to get the actual path of public folder.

huangapple
  • 本文由 发表于 2023年1月9日 17:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定