首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用sendgrid模板从node.js发送邀请?

如何使用sendgrid模板从node.js发送邀请?
EN

Stack Overflow用户
提问于 2021-04-18 19:40:54
回答 1查看 1.7K关注 0票数 2

我试图将ics日历邀请发送给来自node.js服务器的用户,目的是让电子邮件客户端(如Gmail/Outlook等)将它们呈现为实际的邀请,而不仅仅是通常的文件附件。

这与Calend.ly所做的非常相似。

所以,基本上,我试着在Gmail中得到这样的信息:

我需要的流程如下(在客户端):

  1. 在我的前端应用程序中,用户1按schedule event按钮;
  2. 用户2按accept按钮。
  3. 该事件会自动安排,并出现在两个用户的谷歌日历(没有任何OAuth 2.0的东西或类似的东西。只按两个按钮)。
  4. 同时,用户会收到带有事件细节和.ics文件附件的电子邮件。但邀请应该已经在他们的日历上了。

我怎么能这么做?

如果我需要在这方面使用,那么如果我不能为我的用户提供任何OAuth 2.0工具,我应该如何处理这个问题呢?

我目前正在做的是生成.ics文件并使用SendGrid发送它们。但是,有了.ics,我无法实现像上面的图像那样的结果。这些.ics文件不是被邀请的,它们只是附件。

所以我想知道我该怎么处理这个问题?使用Google是实现这一点的正确方法吗?如果是,那么如何在不让用户进行身份验证的情况下实现服务器端?

我知道这是可能的因为Calendly就是这么做的。用户只需将电子邮件输入到输入字段,按submit,事件邀请就会自动出现在他们的Google日历中。

这是如何实施的?

也许我没有得到一些东西,但是生成.ics文件似乎不起作用,同时,由于OAuth2身份验证,Google似乎也不是解决方案。

在他们的医生里他们说

应用程序必须使用OAuth 2.0来授权请求。不支持其他授权协议。

下面是我用来发送带有.ics附件的电子邮件的代码( SendGrid端也有一个模板,因此是dynamicTemplateData支柱):

代码语言:javascript
运行
复制
const SendGrid = require("@sendgrid/mail");

  const attachment = {
    filename: 'invite.ics',
    name: 'invite.ics',
    content: Buffer.from(data).toString('base64'),
    disposition: 'attachment',
    contentId: uuid(),
    type: 'text/calendar; method=REQUEST',
  };

SendGrid.send({
      attachments: [attachment],
      templateId,
      from: {
        email: config.emailSender,
        name: config.emailName,
      },
      to: user.email,
      dynamicTemplateData: {
        ...rest,
        user,
      },
      headers: {
        'List-Unsubscribe': `<mailto:unsubscribe.link`,
      },
    });

下面是我的.ics附件文件的样子:

代码语言:javascript
运行
复制
BEGIN:VCALENDAR
PRODID:-//Organization//Organization App//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20210420T180000Z
DTEND:20210420T190000Z
DTSTAMP:20210418T201735Z
ORGANIZER;CN=Denis Yakovenko:MAILTO:test+1@gmail.com
UID:25bb4d3e-b69d-46b0-baea-489c71c48c88
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=Denis Yakovenko;X-NUM-GUESTS=0:MAILTO:test+1@gmail.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=John Smith;X-NUM-GUESTS=0:MAILTO:test+2@gmail.com
CREATED:20210418T201735Z
DESCRIPTION:my description
LAST-MODIFIED:20210418T201735Z
LOCATION:https://virtual.location.com
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:my summary
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
EN

回答 1

Stack Overflow用户

发布于 2021-04-18 20:46:34

我引用了sendgrid存储库中的问题,并将解决方案从红宝石版转换为javascript。

下面的工作成功地为我和谷歌生成了事件预览。

代码语言:javascript
运行
复制
const ics = require("ics");

const sendgrid = require("@sendgrid/mail");

const event = {
  start: [2018, 5, 30, 6, 30],
  duration: { hours: 6, minutes: 30 },
  title: "Bolder Boulder",
  description: "Annual 10-kilometer run in Boulder, Colorado",
  location: "Folsom Field, University of Colorado (finish line)",
  url: "http://www.bolderboulder.com/",
  geo: { lat: 40.0095, lon: 105.2669 },
  categories: ["10k races", "Memorial Day Weekend", "Boulder CO"],
  status: "CONFIRMED",
  busyStatus: "BUSY",
  organizer: { name: "Admin", email: "Race@BolderBOULDER.com" },
  attendees: [
    {
      name: "Adam Gibbons",
      email: "adam@example.com",
      rsvp: true,
      partstat: "ACCEPTED",
      role: "REQ-PARTICIPANT",
    },
  ],
};

const { value } = ics.createEvent(event);

sendgrid.setApiKey(process.env.SENDGRID_API_KEY);

sendgrid.send({
  to: "email@example.com",
  from: "test@example.com",
  subject: "This is an example email 3",
  content: [
    {
      type: "text/plain",
      value: "Plain Content",
    },
    {
      type: "text/html",
      value: "HTML Content",
    },
    {
      type: "text/calendar; method=REQUEST",
      value: value,
    },
  ],
  attachments: [
    {
      content: Buffer.from(value).toString("base64"),
      type: "application/ics",
      name: "invite.ics",
      filename: "invite.ics",
      disposition: "attachment",
    },
  ],
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67152710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档