From ea1a55bab1855140e2f9dace4a59ec83bbe47b35 Mon Sep 17 00:00:00 2001 From: NIYOMUGABO BERNARD <85235653+niyobern@users.noreply.github.com> Date: Fri, 3 May 2024 16:14:23 +0000 Subject: [PATCH] feat: Update email sending function to use MailerSend API --- src/emails/index.ts | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/emails/index.ts b/src/emails/index.ts index 29f8fea1..fbeb9416 100644 --- a/src/emails/index.ts +++ b/src/emails/index.ts @@ -1,10 +1,7 @@ import axios from 'axios'; import handlebars from 'handlebars'; import fs from 'fs'; -import qs from 'qs' -const apiKey = process.env.MAILGUN_API_KEY || 'your_api_key'; -const domain = process.env.MAILGUN_DOMAIN; type EmailType = 'confirm' | 'reset'; type Data = { @@ -33,21 +30,27 @@ async function sendEmail(emailType: EmailType, recipient: string, data: Data) { const html = template(data); // Send the Email - const url = `https://api.mailgun.net/v3/${domain}/messages` - const body = { - 'from': `Dynamites Account Team `, - 'to': [recipient], - 'subject': 'Verification Email', - 'html': html, - } - const formattedBody = qs.stringify(body); - - const response = await axios.post(url, formattedBody, { - auth: { - username: 'api', - password: apiKey + const domain2 = process.env.MAILERSEND_DOMAIN + const key2 = process.env.MAILERSEND_TOKEN + const body2 = { + 'from': { + 'email': `info@${domain2}`, }, + 'to': [ + { + 'email': recipient + } + ], + 'subject': 'Verification Email', + 'html': html + } + const mailersend = 'https://api.mailersend.com/v1/email' + const response = await axios.post(mailersend, body2, { + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${key2}` + } }); return response } catch (error) {