Last active
February 4, 2022 10:03
-
-
Save AntonMyr/e47efbf0b951b070d5e1de6a92ecc443 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | |
import type { NextApiRequest, NextApiResponse } from "next"; | |
import type { Email } from 'node-mailjet'; | |
import mailjet from 'node-mailjet'; | |
const mailjetUid = process.env.MAILJET_UID; | |
const mailjetPwd = process.env.MAILJET_PWD; | |
const mailjetConn: Email.Client = mailjet.connect(mailjetUid, mailjetPwd); | |
const createRequest = (subject: String, htmlBody: String): Promise<Email.Response> => { | |
const mailjetPost: Email.PostResource = mailjetConn.post("send", { 'version': 'v3.1' }); | |
const mailjetResponse: Promise<Email.Response> = mailjetPost.request({ | |
"Messages": [ | |
{ | |
"From": { | |
"Email": "<your email>", | |
"Name": "Tutorial SMS" | |
}, | |
"To": [ | |
{ | |
"Email": "<your recipients mail>", | |
"Name": "<your recipients name>" | |
} | |
], | |
"Subject": subject, | |
"HTMLPart": htmlBody, | |
"CustomID": "AppGettingStartedTest" | |
} | |
] | |
}); | |
return mailjetResponse; | |
} | |
export default function handler( | |
req: NextApiRequest, | |
res: NextApiResponse | |
) { | |
if (req.method === 'POST') { | |
const { direction, id, from, created, message } = req.body; | |
let mailBody = `<h3>You got a new SMS from ${from}</h3><br />`; | |
mailBody += `<h4>See message below:</h4><i>${message}</i>`; | |
const request = createRequest("A new SMS!", mailBody); | |
request | |
.then((result) => { | |
console.log(result.body) | |
res.status(200).json({ status: 'ok' }); | |
}) | |
.catch((err) => { | |
console.log(err.statusCode) | |
res.status(200).json({ status: 'ok' }); | |
}) | |
} else if (req.method === 'GET') { | |
res.status(200).json({ hello: "world" }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment