add emailService to handle email sending
This commit is contained in:
56
app/services/emailService.js
Normal file
56
app/services/emailService.js
Normal file
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
let AWS = require("aws-sdk");
|
||||
const htmlToText = require("html-to-text");
|
||||
|
||||
const { AWS_EMAIL_CONFIG } = require("../config/appConfig");
|
||||
|
||||
AWS.config.update({
|
||||
region: AWS_EMAIL_CONFIG.REGION,
|
||||
credentials: {
|
||||
accessKeyId: AWS_EMAIL_CONFIG.CREDENTIALS.ACCESS_KEY_ID,
|
||||
secretAccessKey: AWS_EMAIL_CONFIG.CREDENTIALS.SECRET_ACCESS_KEY
|
||||
}
|
||||
});
|
||||
|
||||
const awsMailer = new AWS.SES({ apiVersion: "2010-12-01" });
|
||||
|
||||
const sendEmail = async (to, subject, message, from) => {
|
||||
const params = {
|
||||
Destination: {
|
||||
ToAddresses: [to]
|
||||
},
|
||||
Message: {
|
||||
Subject: {
|
||||
Charset: "UTF-8",
|
||||
Data: subject
|
||||
},
|
||||
Body: {
|
||||
Html: {
|
||||
Charset: "UTF-8",
|
||||
Data: message
|
||||
},
|
||||
Text: {
|
||||
Charset: "UTF-8",
|
||||
Data: htmlToText.fromString(message)
|
||||
}
|
||||
}
|
||||
},
|
||||
ReturnPath: from ? from : AWS_EMAIL_CONFIG.SOURCE_EMAIL,
|
||||
Source: from ? from : AWS_EMAIL_CONFIG.SOURCE_EMAIL
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
awsMailer.sendEmail(params, (error, data) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
sendEmail
|
||||
};
|
||||
Reference in New Issue
Block a user