sendgrid-nodejs icon indicating copy to clipboard operation
sendgrid-nodejs copied to clipboard

The content_id parameter is required if disposition = \'inline\'.'

Open emersonreisdev opened this issue 7 years ago • 11 comments

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

emersonreisdev avatar Nov 05 '18 23:11 emersonreisdev

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

thinkingserious avatar Nov 13 '18 23:11 thinkingserious

Hi @thinkingserious , Is there any updates on this one?

Best regards, Solomon

sculaste avatar Nov 27 '18 08:11 sculaste

Hello @dungahk, @sculaste,

Have you tried using content_id instead of contentId as the key?

With Best Regards,

Elmer

thinkingserious avatar Dec 21 '18 23:12 thinkingserious

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);

kozak-codes avatar Dec 06 '19 17:12 kozak-codes

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.

childish-sambino avatar Feb 13 '20 21:02 childish-sambino

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?

kozak-codes avatar Feb 17 '20 18:02 kozak-codes

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.

childish-sambino avatar Feb 18 '20 17:02 childish-sambino

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 🤩.

kozak-codes avatar Feb 18 '20 18:02 kozak-codes

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!

thinkingserious avatar Mar 11 '21 01:03 thinkingserious

Any updates on this issue? Typescript keeps bothering when using "content_id"

Cafezinho avatar Aug 27 '21 21:08 Cafezinho

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..

tudor33sud avatar Dec 08 '24 08:12 tudor33sud