简体   繁体   中英

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: 'organizer@example.com',
    mailto: 'explicit@mailto.com'
})

// 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\node_modules\sparkpost\lib\sparkpost.js:38:15)
     at Request._callback (C:\Sonar\node_modules\sparkpost\lib\sparkpost.js:128:15)
     at Request.self.callback (C:\Sonar\node_modules\request\request.js:185:22)
     at Request.emit (events.js:182:13)
     at Request.<anonymous> (C:\Sonar\node_modules\request\request.js:1154:10)
     at Request.emit (events.js:182:13)
     at IncomingMessage.<anonymous> (C:\Sonar\node_modules\request\request.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??

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: 'name@outlook.com' }],
        content: {
          from: 'name@sparkmail.com',
          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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM