-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
110 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,136 @@ | ||
import mail from "nodemailer"; | ||
import { Company_Name, GetSMTPConfig } from "../Config"; | ||
import { IConfigs } from "interfaces/Admin/Configs.interface"; | ||
import { AW } from "lib"; | ||
import Logger from "@cpg/logger"; | ||
import mail from 'nodemailer'; | ||
import { Company_Name, GetSMTPConfig } from '../Config'; | ||
import { IConfigs } from 'interfaces/Admin/Configs.interface'; | ||
import { AW } from 'lib'; | ||
import Logger from '@cpg/logger'; | ||
|
||
const log = new Logger("cpg:api:email:send"); | ||
const log = new Logger('cpg:api:email:send'); | ||
|
||
/** | ||
* @description | ||
* Send a email | ||
*/ | ||
export async function SendEmail( | ||
receiver: string, | ||
subject: string, | ||
body: { | ||
isHTML: boolean; | ||
body: any; | ||
attachments?: any; | ||
receiver: string, | ||
subject: string, | ||
body: { | ||
isHTML: boolean; | ||
body: any; | ||
attachments?: any; | ||
}, | ||
callback?: (error: Error | null, sent: boolean | null) => void | ||
): Promise<boolean | void> { | ||
const [SMTPConfig, SMTP_Error] = await AW<IConfigs['smtp']>(await GetSMTPConfig()); | ||
if (!SMTPConfig || SMTP_Error) throw new Error(`No SMTP config.`); | ||
|
||
const config = { | ||
host: SMTPConfig.host, | ||
port: SMTPConfig.port, | ||
secure: SMTPConfig.secure, | ||
secureConnection: false, | ||
ignoreTLS: false, | ||
requireTLS: true, | ||
auth: { | ||
user: SMTPConfig.username, | ||
pass: SMTPConfig.password | ||
}, | ||
callback?: (error: Error | null, sent: boolean | null) => void | ||
): Promise<boolean | void> | ||
{ | ||
const [SMTPConfig, SMTP_Error] = await AW<IConfigs["smtp"]>(await GetSMTPConfig()); | ||
if (!SMTPConfig || SMTP_Error) | ||
throw new Error(`No SMTP config.`); | ||
|
||
const config = { | ||
host: SMTPConfig.host, | ||
port: SMTPConfig.port, | ||
secure: SMTPConfig.secure, | ||
secureConnection: false, | ||
ignoreTLS: false, | ||
requireTLS: true, | ||
auth: { | ||
user: SMTPConfig.username, | ||
pass: SMTPConfig.password | ||
}, | ||
tls: { | ||
rejectUnauthorized: false | ||
}, | ||
tls: { | ||
rejectUnauthorized: false | ||
} | ||
}; | ||
|
||
const email: { | ||
from: string; | ||
to: string; | ||
subject: string; | ||
text?: string; | ||
html?: string; | ||
attachments?: any; | ||
} = { | ||
from: `"${await Company_Name()}" <${SMTPConfig.username}>`, | ||
to: `${receiver}`, | ||
subject: subject, | ||
} | ||
|
||
if (body.isHTML) | ||
email.html = body.body; | ||
const email: { | ||
from: string; | ||
to: string; | ||
subject: string; | ||
text?: string; | ||
html?: string; | ||
attachments?: any; | ||
} = { | ||
from: `"${await Company_Name()}" <${SMTPConfig.username}>`, | ||
to: `${receiver}`, | ||
subject: subject | ||
}; | ||
|
||
if (!body.isHTML) | ||
email.text = body.body | ||
if (body.isHTML) email.html = body.body; | ||
|
||
if (body.attachments) | ||
email.attachments = body.attachments; | ||
if (!body.isHTML) email.text = body.body; | ||
|
||
//@ts-ignore | ||
const transport = mail.createTransport(config); | ||
if (body.attachments) email.attachments = body.attachments; | ||
|
||
log.info(`Sending email to ${receiver}..`); | ||
//@ts-ignore | ||
const transport = mail.createTransport(config); | ||
|
||
log.info(`Sending email to ${receiver}..`); | ||
|
||
transport.sendMail(email).then(() => | ||
{ | ||
callback ? callback?.(null, true) : Promise.resolve(true); | ||
}).catch(e => | ||
{ | ||
callback ? callback?.(e, false) : Promise.resolve(false); | ||
transport | ||
.sendMail(email) | ||
.then(() => { | ||
callback ? callback?.(null, true) : Promise.resolve(true); | ||
}) | ||
.catch((e) => { | ||
callback ? callback?.(e, false) : Promise.resolve(false); | ||
}); | ||
} | ||
|
||
export async function sendEmail(options: { | ||
receiver: string; | ||
subject: string; | ||
body: { | ||
body: any; | ||
attachments?: any; | ||
}; | ||
}) | ||
{ | ||
const [SMTPConfig, SMTP_Error] = await AW<IConfigs["smtp"]>(await GetSMTPConfig()); | ||
if (!SMTPConfig || SMTP_Error) | ||
throw new Error(`No SMTP config.`); | ||
|
||
const config = { | ||
host: SMTPConfig.host, | ||
port: SMTPConfig.port, | ||
secure: SMTPConfig.secure, | ||
secureConnection: false, | ||
ignoreTLS: false, | ||
requireTLS: true, | ||
auth: { | ||
user: SMTPConfig.username, | ||
pass: SMTPConfig.password | ||
}, | ||
tls: { | ||
rejectUnauthorized: false | ||
}, | ||
receiver: string; | ||
subject: string; | ||
body: { | ||
body: any; | ||
attachments?: any; | ||
}; | ||
}) { | ||
const [SMTPConfig, SMTP_Error] = await AW<IConfigs['smtp']>(await GetSMTPConfig()); | ||
if (!SMTPConfig || SMTP_Error) throw new Error(`No SMTP config.`); | ||
|
||
const USING_GMAIL = SMTPConfig.host === 'smtp.gmail.com'; | ||
|
||
let config: Record<string, any> = { | ||
host: SMTPConfig.host, | ||
port: SMTPConfig.port, | ||
secure: SMTPConfig.secure, | ||
secureConnection: false, | ||
ignoreTLS: false, | ||
requireTLS: true, | ||
auth: { | ||
user: SMTPConfig.username, | ||
pass: SMTPConfig.password | ||
}, | ||
tls: { | ||
rejectUnauthorized: false | ||
} | ||
}; | ||
|
||
if (USING_GMAIL) { | ||
config = { | ||
service: 'gmail', | ||
auth: { | ||
user: SMTPConfig.username, | ||
pass: SMTPConfig.password | ||
} | ||
}; | ||
} | ||
|
||
const email: { | ||
from: string; | ||
to: string; | ||
subject: string; | ||
html: string; | ||
attachments?: any; | ||
} = { | ||
from: `"${await Company_Name()}" <${SMTPConfig.username}>`, | ||
to: `${options.receiver}`, | ||
html: options.body.body, | ||
subject: options.subject, | ||
} | ||
const email: { | ||
from: string; | ||
to: string; | ||
subject: string; | ||
html: string; | ||
attachments?: any; | ||
} = { | ||
from: `"${await Company_Name()}" <${SMTPConfig.username}>`, | ||
to: `${options.receiver}`, | ||
html: options.body.body, | ||
subject: options.subject | ||
}; | ||
|
||
if (options.body.attachments) | ||
email.attachments = options.body.attachments; | ||
if (options.body.attachments) email.attachments = options.body.attachments; | ||
|
||
//@ts-ignore | ||
const transport = mail.createTransport(config); | ||
//@ts-ignore | ||
const transport = mail.createTransport(config); | ||
|
||
log.info(`Sending email to ${options.receiver}..`); | ||
log.info(`Sending email to ${options.receiver}..`); | ||
|
||
return transport.sendMail(email); | ||
} | ||
return transport.sendMail(email); | ||
} |