devpolo / ics

Simple .ics API generator
0 stars 0 forks source link

[next gen] email ms #1

Open devpolo opened 3 years ago

devpolo commented 3 years ago

From api to email microservice :

Params

Sources

Sendgrid email ms with ics attachment

devpolo commented 3 years ago

code for next js api

const sgMail = require("@sendgrid/mail")
const ics = require("ics")

sgMail.setApiKey(process.env.SENDGRID_API_KEY)

const handler = async (req, res) => {
  if (req.method === "POST") {
    try {
      // create ics
      const event = {
        start: [2021, 7, 22, 17, 30],
        duration: { hours: 1, minutes: 30 },
        title: "Classe Virtuelle",
        description: "Une classe de l'espace",
        location: "Online",
        url: "https://google.com/",
        geo: { lat: 40.0095, lon: 105.2669 },
        categories: ["Virtual Classroom", "Webinar"],
        status: "CONFIRMED",
        busyStatus: "BUSY",
        organizer: {
          name: "Be SuperAdmin",
          email: "b@outlook.com",
        },
        attendees: [
          {
            name: "Be Admin",
            email: "b@outlook.com",
            rsvp: true,
            role: "CHAIR",
          },
          {
            name: "P User",
            email: "p@gmail.com",
            role: "REQ-PARTICIPANT",
          },
          {
            name: "A User",
            email: "a@gmail.com",
            role: "REQ-PARTICIPANT",
          },
          {
            name: "P User",
            email: "p@outlook.com",
            role: "REQ-PARTICIPANT",
          },
        ],
      }

      ics.createEvent(event, async (error, value) => {
        try {
          if (error) {
            console.log(error)
            res.status(500).json({ response: "ics error" })
          }

          console.log(value)
          const textBuffered = Buffer.from(value)

          // send email
          const msg = {
            to: "p@gmail.com",
            from: process.env.FROM_EMAIL_ADRESS,
            templateId: process.env.SENDGRID_TEMPLATE_ID,
            dynamicTemplateData: {
              subject: "New doge message",
              name: "Hubert",
              email: "p@outlook.com",
              message: "Nouvel evenement",
              source: "",
            },
            // headers: {
            //   "Content-Type": "text/calendar",
            // },
            attachments: [
              {
                content: textBuffered.toString("base64"),
                filename: "event.ics",
                type: "text/calendar",
                disposition: "attachment",
                content_id: "event",
              },
            ],
          }

          await sgMail.send(msg)
        } catch (err) {
          console.error(err.response.body)
          res.status(500).json({ response: "email not sent" })
        }

        res.status(200).json({ response: "email sent" })
      })
    } catch {
      res.status(500).json({ response: "email not sent" })
    }
  } else {
    res.status(404).json({ response: "not found" })
  }
}

export default handler