Skip to content

Commit

Permalink
feat: Update email sending function to use MailerSend API
Browse files Browse the repository at this point in the history
  • Loading branch information
niyobern committed May 3, 2024
1 parent 7f9cff6 commit ea1a55b
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/emails/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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 <account@${domain}>`,
'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) {
Expand Down

0 comments on commit ea1a55b

Please sign in to comment.