Skip to content

Commit

Permalink
feat: testing gmail
Browse files Browse the repository at this point in the history
  • Loading branch information
Tolfx committed Sep 8, 2023
1 parent 3a925f7 commit 0d546df
Showing 1 changed file with 110 additions and 106 deletions.
216 changes: 110 additions & 106 deletions apps/api/src/Email/Send.ts
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);
}

0 comments on commit 0d546df

Please sign in to comment.