The content_id parameter is required if disposition = \'inline\'.'
Issue Summary
Error when trying to send an email with an attachment.
The content_id parameter is required if disposition = \'inline\'.',
Steps to Reproduce
Just try to send an email with attachments and disposition 'inline'.
My code:
send({
from: { name: 'From Name', email: '[email protected]' },
to: { name: 'To Name', email: '[email protected]' },
subject: `Some subject`,
text: 'Email text',
attachments: [
{
content: buffers.pop().toString('base64'),
filename: fileMetadata.name.split('/').pop(),
type: fileMetadata.contentType,
disposition: 'inline',
contentId: 'my_content_id'
}
]
});
I'm using Typescript and the attachments property is defined as an Array of AttachmentData, which includes a property called contentId, but the API requires the property content_id which is in the interface AttachmentJSON instead.
send function types: https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/src/mail.d.ts#L20
attachments property types: https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/mail.d.ts#L134
AttachmentData interface types: https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/attachment.d.ts#L1
Technical details:
- @sendgrid/mail Version: 6.3.1
- Node.js Version: 8.11.2
- TypeScript Version: 3.1.6
Hello @dungahk,
It looks like there is a bug in the AttachmentData interface. I will add this issue to our backlog for a fix as well as tag it for external contribution.
With Best Regards,
Elmer
Hi @thinkingserious , Is there any updates on this one?
Best regards, Solomon
Hello @dungahk, @sculaste,
Have you tried using content_id instead of contentId as the key?
With Best Regards,
Elmer
Yeah, looks like just a simple typescript error. A simple cast of your object to any is a quick and simple fix until the fix is roled out. eg:
mail.attachments.push({
content: logo,
content_id: 'logo',
filename: 'logo.png',
disposition: 'inline',
type: 'image/png',
} as any);
So the TS definition is correct, but the underlying Attachment class is not being used for de/serialization. When adding attachments, the object needs to be converted to a new Attachment() if it's not already.
Ah okay. So it looks like there are two classes: AttachmentData and AttachmentJSON
Instead of casting to any like I suggested above you can cast to AttachmentJSON. Alternatively, as @childish-sambino suggested, you use new Attachment({ your attachment data here }). If you do not want to work with classes and just want to define the raw JSON data your mail object can be of type MailJSON.
Using MailData in send is problematic and can be confusing because if you define it without using new Mail(), new Attachment(), or not use the JSON interfaces you'll be missing the critical toJSON() functions. Is there a way to avoid this kind of confusion in the future? Possibly use the type (Mail | MailJSON) instead of just MailData in the send function? Perhaps we could convert properties to camel_case in the send function?
I meant that the underlying JS that adds the attachment to the Mail class should work similar to what's done when adding personalizations: https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/mail.js#L255
But I don't have a strong opinion on which way to go here.
Ah yes that would work considering the mail service already generates a Mail object if it isn't that class already. That object then calls setAttachments, setPersonalizations, etc. This would need to be done on most other classes as well. I'll see if I have time to submit a PR 🤩.
Since there has been no activity on this issue since March 1, 2020, we are closing this issue. Please feel free to reopen or create a new issue if you still require assistance. Thank you!
Any updates on this issue? Typescript keeps bothering when using "content_id"
this issue is already years old. I'm also trying to attach a logo as an inline cid, and i get the same issue in 2024..