Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - Can I send .ics file with sparkpost? (node.js)

I'm trying to create an i-cal event and attach it to a sparkpost transmission like this:

const event = cal.createEvent({
    start: req.body.a.start,
    end: req.body.a.end,
    summary: req.body.a.title,
    description: req.body.a.body,
    url: Config.get('/sparkpost/app'),
});
    
// create event organizer
event.organizer({
    name: 'Test',
    email: '[email protected]',
    mailto: '[email protected]'
})

// add an alarm
event.createAlarm({
    type: 'display',
    trigger: 900, // 30min before event
});

req.body.a.teammates.map(async (a) => {
    await event.createAttendee({email: a})
})

b64Event = base64.encode(event)

req.body.a.teammates.map(async (b) => {
    client.transmissions.send({
        recipients: [{address: b}],
        content: {
            from: Config.get('/email/fromEmail'),
            subject: req.body.a.title,
            text: req.body.a.body,
            attachments: [{name: "Test Event", type:"ics", data: [base64.encode(event)]}]
        },
        options: {sandbox: false}
    }).then(data => {console.log(data);}).catch(err => {console.log(err);});
})

Everything seems to work. Except: I cannot get sparkpost to send this file. Keep getting an error:

  { SparkPostError: Unprocessable Entity
     at createSparkPostError (C:Sonar
ode_modulessparkpostlibsparkpost.js:38:15)
     at Request._callback (C:Sonar
ode_modulessparkpostlibsparkpost.js:128:15)
     at Request.self.callback (C:Sonar
ode_modules
equest
equest.js:185:22)
     at Request.emit (events.js:182:13)
     at Request.<anonymous> (C:Sonar
ode_modules
equest
equest.js:1154:10)
     at Request.emit (events.js:182:13)
     at IncomingMessage.<anonymous> (C:Sonar
ode_modules
equest
equest.js:1076:12)
     at Object.onceWrapper (events.js:273:13)
     at IncomingMessage.emit (events.js:187:15)
     at endReadableNT (_stream_readable.js:1094:12)
     at process._tickCallback (internal/process/next_tick.js:63:19)
   name: 'SparkPostError',
   errors:
    [ { message:
         'field 'content.attachments[0].data' value '["W29iamVjdCBPYmplY3Rd"]' is of type 'json_array
' but needs to be of type 'string'',
        code: '1300' } ],
   statusCode: 422 }

I also tried it without the Base64.encoding, and got the same error. Sparkpost doesn't offer a lot of examples or advice, so I am blocked... Please any suggestions??

question from:https://stackoverflow.com/questions/65951382/can-i-send-ics-file-with-sparkpost-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Short answer: "YES"

Long Answer:

Attaching .ics to Email Client:

// convert the invite to base64 for attachment
const buf = Buffer.from(iCal.toString(), 'utf-8');
const base64Cal = buf.toString('base64');

// build the email
const sendEmail = async () => {
    const res = await client.transmissions.send({
        recipients: [{ address: '[email protected]' }],
        content: {
          from: '[email protected]',
          subject: req.body.a.title,
          text: req.body.a.body,
          attachments: [
            {
              name: 'invite.ics',
              type: 'text/calendar;method=REQUEST;name="invite.ics"',
              data: base64Cal,
            },
          ],
        },
        options: { sandbox: false },
    });
}

// send the email
sendEmail();

Please note: The attachment type is critical for Outlook to recognize the event.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...