Use nodemailer to send email using AWS SMTP Host using environmental variables
Fri Jun 10 2022 07:41:32 GMT+0000 (Coordinated Universal Time)
Saved by
@swina
#javascript
#nodemailer
#aws
import * as nodemailer from "nodemailer";
//config nodemailer transporter using environment variables
const transporter = nodemailer.createTransport({
host: `${process.env.AWS_SMTP_HOST}` ,
auth: {
user: `${process.env.AWS_ACCESS_KEY_ID}`,
pass: `${process.env.AWS_SECRET_ACCESS_KEY}`
}
});
export const emailSender = async (data) => {
try {
const response = await transporter.sendMail({
from: process.env.EMAIL_FROM,
to: process.env.EMAIL_TO || data.email,
subject: process.env.EMAIL_SUBJECT || data.subject,
html: `<DOCTYPE html>
<html>
<body>
${data.message}
</body>
</html>
`
});
return response?.messageId
? { success: true }
: { success: false, error: "Failed to send email" };
} catch ( error ){
console.log("ERROR", error.message);
return { success: false, error: error.message };
}
}
content_copyCOPY
Use this code from backend
Comments