尝试使用生命周期钩子附加来自 Strapi 内容类型的响应中的图像,如本指令 https://docs.strapi.io/dev-docs/plugins/email,找不到有关如何附加的信息使用此方法的文件,下面的代码发送电子邮件,但图像无法发送并显示为它的替代文本
export default {
async afterCreate(event) {
const { result } = event
try {
await strapi.plugins["email"].services.email.send({
to: `[email protected]`,
from: "[email protected]", // e.g. single sender verification in SendGrid
subject: `${result.messageSubject}`,
text: `${result.message}
credits:
Name: ${result.name}
Mobile: ${result.phone}
E-mail: ${result.email}`,
html: `<img alt="image in email" src="http://localhost:1337${result.attachment.formats.medium.url}" />`,
})
} catch (err) {
console.log(err)
}
},
}
向对象添加附件属性。对于
content
,使用 fs.readFileSync
设置内容。将文件 type
指定为 image/png
,并将 disposition
设置为 attachment
试试这个
export default {
async afterCreate(event) {
/*---your code--- */
try {
const attachmentContent = fs.readFileSync(filePath);
await strapi.plugins["email"].services.email.send({
/*---your code--- */
attachments: [
{
content: attachmentContent,
filename: "attachment.png",
type: "image/png",
disposition: "attachment",
},
]
})
} catch (err) {
console.log(err)
}
}