Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.
JavaScript agent-skillsai-agentsai-seoclaudeclaude-codeclaude-code-pluginclaude-skillscodexcontent-marketingconversion-rate-optimizationcopywritingcursoremail-marketinggenerative-engine-optimizationgrowth-marketingmarketingmarketing-automationsaas-marketingseoskill-md
fix(sendgrid): allow dynamic templates to supply the email subject
Rudy Celeklid5dacfe59204
diff --git a/tests/sendgrid-dynamic-template.test.cjs b/tests/sendgrid-dynamic-template.test.cjsnew file mode 100644index 0000000..8f94b48--- /dev/null+++ b/tests/sendgrid-dynamic-template.test.cjs@@ -0,0 +1,44 @@+const { test } = require('node:test')+const assert = require('node:assert/strict')+const { spawnSync } = require('node:child_process')+const path = require('node:path')+const cli = path.resolve(__dirname, '../tools/clis/sendgrid.js')+function run(args, fetch = false) {+ const source = `global.fetch = async (url, options) => {+ if (!${fetch}) throw new Error('Unexpected network request');+ return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`+ const result = spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', env: { ...process.env, SENDGRID_API_KEY: 'fake-key' }, timeout: 5000 })+ assert.equal(result.status, 0, result.stderr)+ return JSON.parse(result.stdout)+}++const addresses = ['send', '--from', '[email protected]', '--to', '[email protected]']+test('a dynamic template can supply the subject without a CLI subject', () => {+ const result = run([...addresses, '--template-id', 'd-template123', '--template-data', '{"name":"Zoë","subject":"Receipt"}'], true)+ assert.equal(result.method, 'POST')+ assert.equal(result.url, 'https://api.sendgrid.com/v3/mail/send')+ assert.equal(result.body.template_id, 'd-template123')+ assert.equal(Object.hasOwn(result.body, 'subject'), false)+ assert.equal(Object.hasOwn(result.body, 'content'), false)+ assert.deepEqual(result.body.personalizations[0].dynamic_template_data, { name: 'Zoë', subject: 'Receipt' })+})+test('plain and legacy-template sends still require a subject', () => {+ assert.match(run([...addresses, '--text', 'Hello']).error, /subject/)+ assert.match(run([...addresses, '--template-id', 'legacy-template123']).error, /subject/)+})+test('plain email preserves explicit subject and content', () => {+ const result = run([...addresses, '--subject', 'Hello', '--text', 'World'], true)+ assert.equal(result.body.subject, 'Hello')+ assert.deepEqual(result.body.content, [{ type: 'text/plain', value: 'World' }])+})+test('dynamic-template send still requires sender and recipient', () => {+ assert.match(run(['send', '--template-id', 'd-template123', '--to', '[email protected]']).error, /from/)+ assert.match(run(['send', '--template-id', 'd-template123', '--from', '[email protected]']).error, /to/)+})+test('dynamic-template dry run is redacted and never fetches', () => {+ const result = run([...addresses, '--template-id', 'd-template123', '--dry-run'])+ assert.equal(result.body.template_id, 'd-template123')+ assert.equal(result.headers.Authorization, '***')+ assert.equal(JSON.stringify(result).includes('fake-key'), false)+})diff --git a/tools/clis/sendgrid.js b/tools/clis/sendgrid.jsindex d5167fd..6753e1e 100755--- a/tools/clis/sendgrid.js+++ b/tools/clis/sendgrid.js@@ -56,14 +56,16 @@ async function main() {switch (cmd) {case 'send': {- if (!args.from || !args.to || !args.subject) { result = { error: '--from, --to, and --subject required' }; break }+ if (!args.from || !args.to) { result = { error: '--from and --to required' }; break }+ const dynamicTemplate = typeof args['template-id'] === 'string' && args['template-id'].startsWith('d-')+ if (!args.subject && !dynamicTemplate) { result = { error: '--subject required unless using a dynamic template (--template-id d-...)' }; break }const body = {personalizations: [{to: args.to.split(',').map(e => ({ email: e.trim() })),}],from: { email: args.from },- subject: args.subject,}+ if (args.subject) body.subject = args.subjectif (args['template-id']) {body.template_id = args['template-id']if (args['template-data']) {@@ -191,7 +193,7 @@ async function main() {result = {error: 'Unknown command',usage: {- send: 'send --from <email> --to <email> --subject <subject> --html <html> [--text <text>] [--template-id <id>] [--template-data <json>]',+ send: 'send --from <email> --to <email> [--subject <subject>] [--html <html>] [--text <text>] [--template-id <id> (d-... supplies its own subject)] [--template-data <json>]',contacts: 'contacts [list|add|search] [--email <email>] [--first-name <name>] [--last-name <name>] [--list-ids <ids>] [--query <sgql>]',campaigns: 'campaigns [list|get] [id] [--limit <n>]',stats: 'stats get [--start-date <YYYY-MM-DD>] [--end-date <YYYY-MM-DD>]',