【问题标题】:Can I send .ics file with sparkpost? (node.js)我可以使用 sparkpost 发送 .ics 文件吗? (node.js)
【发布时间】:2021-05-03 04:34:16
【问题描述】:

我正在尝试创建一个 i-cal 事件并将其附加到 sparkpost 传输,如下所示:

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

似乎一切正常。除了:我无法让 sparkpost 发送此文件。不断报错:

  { 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 }

我也尝试了不使用 Base64.encoding 的方法,但得到了同样的错误。 Sparkpost 没有提供很多示例或建议,所以我被屏蔽了......请有什么建议吗??

【问题讨论】:

  • 看起来@Jean-François Fabre 不喜欢我的回答,所以它被删除了。对不起,如果你有它的副本,那就是你需要的。这是我不久前给出的类似答案,应该可以帮助您上路stackoverflow.com/questions/54118343/…
  • 哇老兄,感谢您重新发布!是的,我从您的回答中意识到我遗漏了几件事。当答案消失时,我正在回复你。我能够获取要发送的文件,但现在电子邮件客户端无法将其识别为 ics 文件。感谢您添加此链接。我会继续努力
  • 如果电子邮件来源(附件)看起来正确,您可能需要这个"type": "application/ics; name=\"invite.ics\"",
  • 是的,伙计,您的示例解决了我们的问题。谢谢你。我将发布我们使用的最终代码,这样会有一个节点示例供后代使用。
  • 我在这里发布了答案:stackoverflow.com/questions/66102584/…

标签: javascript node.js base64 icalendar sparkpost


【解决方案1】:

简短回答:“是”

长答案:

将 .ics 附加到电子邮件客户端:

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

请注意:附件类型对于 Outlook 识别事件至关重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多