narinder1231's picture
improve error logging and error messages
d747060
raw
history blame contribute delete
950 Bytes
import nodemailer from 'nodemailer';
import { SMTP_CONFIG } from '../config/app.config';
import { logger } from './logger';
interface MailOptions {
from?: string;
to?: string;
subject: string;
content: string;
}
export const sendMail = async ({ from, to, subject, content }: MailOptions) => {
const transporter = nodemailer.createTransport({
host: SMTP_CONFIG.HOST,
port: parseInt(SMTP_CONFIG.PORT, 587),
secure: SMTP_CONFIG.SECURE === 'true' ? true : false,
auth: {
user: SMTP_CONFIG.USERNAME,
pass: SMTP_CONFIG.PASSWORD,
},
tls: {
rejectUnauthorized: false,
},
});
try {
const info = await transporter.sendMail({
from: from || SMTP_CONFIG.SENDER,
to: to,
subject,
html: content,
});
logger.info('Message sent: %s', info.messageId);
} catch (error) {
logger.error('Error occurred while sending email');
logger.error(error);
}
}