diff --git a/packages/helpers/classes/email-address.js b/packages/helpers/classes/email-address.js index 148177553..f8d2cbbf0 100644 --- a/packages/helpers/classes/email-address.js +++ b/packages/helpers/classes/email-address.js @@ -55,7 +55,11 @@ class EmailAddress { if (typeof name !== 'string') { throw new Error('String expected for `name`'); } - this.name = name; + // Wrap name in quotes to address API issue + // https://github.com/sendgrid/sendgrid-csharp/issues/268#issuecomment-232177443 + const isQuoted = (name[0] === '\"') && (name[name.length - 1] === '\"'); + const shouldQuote = name.includes(',') && !isQuoted; + this.name = shouldQuote ? `\"${name}\"` : name; } /** diff --git a/packages/helpers/classes/email-address.spec.js b/packages/helpers/classes/email-address.spec.js index 3b00ffad7..2d4c173e9 100644 --- a/packages/helpers/classes/email-address.spec.js +++ b/packages/helpers/classes/email-address.spec.js @@ -54,6 +54,16 @@ describe('EmailAddress', function() { email.setName('Test'); expect(email.name).to.equal('Test'); }); + + it('should wrap name in quotes if a comma is present', function() { + email.setName('Doe, John'); + expect(email.name).to.equal('\"Doe, John\"'); + }); + + it('should not double wrap in quotes', function() { + email.setName('\"Doe, John\"'); + expect(email.name).to.equal('\"Doe, John\"'); + }); it('should throw an error for invalid input', function() { expect(function() { email.setName(5);