From 9c462a4dbcf09664821e74efa4a7f91f94cc8b2d Mon Sep 17 00:00:00 2001 From: Niloy Sikdar Date: Mon, 5 Sep 2022 23:54:20 +0530 Subject: [PATCH] feat(validateemail): add and export validateEmail function Add validateEmail function which used mailtrap and nodemailer to send emails Resolves #97 Signed-off-by: Niloy Sikdar --- package.json | 2 + src/utils/index.ts | 2 + src/utils/validateEmail/index.ts | 2 + src/utils/validateEmail/usingMailtrap.ts | 52 ++++++++++++++++++++++++ src/utils/validateEmail/validateEmail.ts | 6 +++ yarn.lock | 12 ++++++ 6 files changed, 76 insertions(+) create mode 100644 src/utils/validateEmail/index.ts create mode 100644 src/utils/validateEmail/usingMailtrap.ts create mode 100644 src/utils/validateEmail/validateEmail.ts diff --git a/package.json b/package.json index 2b01414..59564ea 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "@types/html-to-text": "^8.1.0", "@types/jest": "^28.1.3", "@types/node": "^18.0.0", + "@types/nodemailer": "^6.4.5", "@types/react": "^18.0.14", "@types/react-dom": "^18.0.5", "@typescript-eslint/eslint-plugin": "^5.28.0", @@ -92,6 +93,7 @@ "husky": "^8.0.0", "jest": "^28.1.1", "lint-staged": "^13.0.2", + "nodemailer": "^6.7.8", "prettier": "^2.7.1", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/src/utils/index.ts b/src/utils/index.ts index a768d45..57b5c47 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,3 +3,5 @@ export { makeStyles } from './makeStyles'; export { generateEmail } from './generateEmail'; export { generateTextEmailFromHTML } from './generateTextEmailFromHTML'; export { generateTextEmail } from './generateTextEmail'; +export { validateEmail } from './validateEmail'; +export type { MailtrapOptions } from './validateEmail'; diff --git a/src/utils/validateEmail/index.ts b/src/utils/validateEmail/index.ts new file mode 100644 index 0000000..934d4b3 --- /dev/null +++ b/src/utils/validateEmail/index.ts @@ -0,0 +1,2 @@ +export { validateEmail } from './validateEmail'; +export type { MailtrapOptions } from './usingMailtrap'; diff --git a/src/utils/validateEmail/usingMailtrap.ts b/src/utils/validateEmail/usingMailtrap.ts new file mode 100644 index 0000000..38d96ba --- /dev/null +++ b/src/utils/validateEmail/usingMailtrap.ts @@ -0,0 +1,52 @@ +import nodemailer from 'nodemailer'; + +interface Auth { + user: string; + pass: string; +} + +export interface MailtrapOptions { + from: string; + to: string; + subject: string; + HTMLEmail: string; + textEmail?: string; + auth: Auth; +} + +export async function validateEmailUsingMailtrap({ + from, + to, + subject, + HTMLEmail, + textEmail, + auth, +}: MailtrapOptions) { + const transporter = nodemailer.createTransport({ + host: 'smtp.mailtrap.io', + port: 2525, + auth: { + user: auth.user, + pass: auth.pass, + }, + }); + + let info, error; + + try { + const res = await transporter.sendMail({ + from: from, + to: to, + subject: subject, + text: textEmail, + html: HTMLEmail, + }); + + info = res; + } catch (err) { + console.log(err); + error = err; + } + + return { info, error }; +} diff --git a/src/utils/validateEmail/validateEmail.ts b/src/utils/validateEmail/validateEmail.ts new file mode 100644 index 0000000..314b23d --- /dev/null +++ b/src/utils/validateEmail/validateEmail.ts @@ -0,0 +1,6 @@ +import type { MailtrapOptions } from './usingMailtrap'; +import { validateEmailUsingMailtrap } from './usingMailtrap'; + +export async function validateEmail(options: MailtrapOptions) { + return await validateEmailUsingMailtrap(options); +} diff --git a/yarn.lock b/yarn.lock index 7811543..c24fab4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4508,6 +4508,13 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.41.tgz#88eb485b1bfdb4c224d878b7832239536aa2f813" integrity sha512-mqoYK2TnVjdkGk8qXAVGc/x9nSaTpSrFaGFm43BUH3IdoBV0nta6hYaGmdOvIMlbHJbUEVen3gvwpwovAZKNdQ== +"@types/nodemailer@^6.4.5": + version "6.4.5" + resolved "https://registry.yarnpkg.com/@types/nodemailer/-/nodemailer-6.4.5.tgz#09011ac73259245475d1688e4ba101860567dc39" + integrity sha512-zuP3nBRQHI6M2PkXnGGy1Ww4VB+MyYHGgnfV2T+JR9KLkeWqPJuyVUgLpKXuFnA/b7pZaIDFh2sV4759B7jK1g== + dependencies: + "@types/node" "*" + "@types/normalize-package-data@^2.4.0": version "2.4.1" resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301" @@ -12148,6 +12155,11 @@ node-releases@^2.0.5: resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.5.tgz#280ed5bc3eba0d96ce44897d8aee478bfb3d9666" integrity sha512-U9h1NLROZTq9uE1SNffn6WuPDg8icmi3ns4rEl/oTfIle4iLjTliCzgTsbaIFMq/Xn078/lfY/BL0GWZ+psK4Q== +nodemailer@^6.7.8: + version "6.7.8" + resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.7.8.tgz#9f1af9911314960c0b889079e1754e8d9e3f740a" + integrity sha512-2zaTFGqZixVmTxpJRCFC+Vk5eGRd/fYtvIR+dl5u9QXLTQWGIf48x/JXvo58g9sa0bU6To04XUv554Paykum3g== + nopt@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/nopt/-/nopt-5.0.0.tgz#530942bb58a512fccafe53fe210f13a25355dc88"