File size: 950 Bytes
69fc649
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea09d29
 
69fc649
 
 
 
ea09d29
 
 
69fc649
 
 
 
 
 
 
 
 
 
 
 
d747060
 
69fc649
ea09d29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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);
  }
}