kritsana135-hooray-server
Version:
for server
59 lines (52 loc) • 1.65 kB
JavaScript
import { getTokenVerify } from "./verify"
import { TEMPLATE_TYPE } from "../constants/template"
import { EN } from "../constants/language"
const nodemailer = require("nodemailer")
export default async function sendEmail(
smtp,
fromEmail,
toEmail,
title,
content
) {
// Create the SMTP transport.
const transporter = nodemailer.createTransport({
host: smtp.host,
port: smtp.port,
secure: false, // true for 465, false for other ports
auth: {
user: smtp.username,
pass: smtp.password,
},
})
// Specify the fields in the email.
const mailOptions = {
from: fromEmail,
to: toEmail,
subject: title,
html: content,
// Custom headers for configuration set and message tags.
}
// Send the email.
const info = await transporter.sendMail(mailOptions)
console.log("Message sent: %s", info.messageId)
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview only available when sending through an Ethereal account
// console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
return info
}
export const sendToVerifyEmail = async (locale = EN, email = "", app = "") => {
const template = app.templates.find(
(element) => element.name === TEMPLATE_TYPE.VERIFY_EMAIL
)
const { smtp } = app.option
const toEmail = email
const title = "Verify Email"
const { content, verifyToken } = await getTokenVerify(
template,
TEMPLATE_TYPE.VERIFY_EMAIL
)
const info = await sendEmail(smtp, smtp.senderEmail, toEmail, title, content)
return verifyToken
}