我们的电子邮件发送正常,现在我们要添加附件。 因此我们复制了此示例: https :
并像这样制作代码:
const sendgrid: any = require('sendgrid')(process.env.sendgrid_key);
sendEmail(data: IEmailData): void {
const email: any = new sendgrid.Email();
email.addTo(data.emailTo);
for (let attachment in data.attachments)
this.addAttachment(email, attachment);
sendgrid.send(email, (error: any, json: string) => {
if (error)
console.log(error);
});
}
addAttachment(email: any, attachmentData: any): void {
const attachment = new sendgrid.Attachment();
attachment.setContent(attachmentData.content);
attachment.setType(attachmentData.type);
attachment.setFilename(attachmentData.filename);
attachment.setDisposition(attachmentData.disposition);
email.addAttachment(attachment);
}
但是,当调用sendEmail
我们会被告知sendgrid.Attachment is not a function
(第二个函数的第一行)。
我打印出sendgrid对象,它看起来像这样:
{ api_key: 'SG.DaV0L',
api_user: null,
options:
{ uriParts:
{ protocol: 'https',
host: 'api.sendgrid.com',
port: '',
endpoint: '/api/mail.send.json' },
uri: 'https://api.sendgrid.com/api/mail.send.json' },
version: '1.9.2',
Email: [Function: Email],
smtpapi: [Function: smtpapi],
send: [Function] }
谢谢,
理查德
阅读代码后想通了。 github中的示例是完全错误的。 应该是这样的:
addAttachment(email: any, attachmentData: any): void {
email.addFile(attachmentData);
}
我们现在在这里有一个附件示例: https :
嗨,我知道这个问题已经解决,但这是我尝试向gmail用户发送嵌入图像时遇到的一个问题。
我发现了一些原因,为什么gmail由于它们强加了一些额外的安全性而无法渲染图像,所以我认为我将使用attachments参数添加content_id并查看其是否有效,而不是使用常规的src标记。
发送后,发送网格将返回错误的请求。 这是我的方法。
我这样编码JPG:
let bitmap = fs.readFileSync(imageDir);
imageBase64URL = new Buffer(bitmap).toString('base64');
然后,将编码后的JPG传递到附件参数内的content字段中:
mailer.send({
to: '[email protected]',
from: '[email protected]',
subject: `You have been removed from ${house.name} by ${removerName}`,
html: result,
attachments: [
{
content: imageBase64URL,
filename: 'cluttr-logo.jpg',
contentId: 'cluttr-logo'
},
]
});
最后,我将自己的CID嵌入到我的ejs文件中:
html
<img alt="Logo" src="cid:cluttr-logo" width="60"
height="60"
希望有人对我做错了什么有所了解。 谢谢。
@thinkingserious :您好,您知道如何使用事务模板发送附件吗?
@thinkingserious有什么方法可以将URL附件添加到电子邮件中,还有什么其他方法可以将附件添加到电子邮件中。 找不到确切的文件说明附件在Sendgrid上的工作方式。
问候,
莫希特
@thinkingserious :您好,您知道如何使用事务模板发送附件吗?
如果您使用的是v3 API,只需添加一个attachments
键。 content
必须是Base64编码
https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
email = {
"from":{
"email":"[email protected]"
},
"personalizations":[
{
"to":[
{
"email" : "[email protected]"
}
],
"dynamic_template_data": {
}
}
],
"attachments": [
{
"content": "",
"filename" ""
}
]
"template_id":"someid"
}
示例链接不起作用。
这是新的链接:
https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/attachments.md
新的新链接: https :
最有用的评论
示例链接不起作用。
这是新的链接:
https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/attachments.md