Skip to content

Commit

Permalink
feat(validateemail): add and export validateEmail function (#103)
Browse files Browse the repository at this point in the history
Add validateEmail function which used mailtrap and nodemailer to send emails

Resolves #97

Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>

Signed-off-by: Niloy Sikdar <niloysikdar30@gmail.com>
  • Loading branch information
niloysikdar committed Sep 9, 2022
1 parent d209bdb commit a400b31
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions src/utils/validateEmail/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { validateEmail } from './validateEmail';
export type { MailtrapOptions } from './usingMailtrap';
52 changes: 52 additions & 0 deletions src/utils/validateEmail/usingMailtrap.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
6 changes: 6 additions & 0 deletions src/utils/validateEmail/validateEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { MailtrapOptions } from './usingMailtrap';
import { validateEmailUsingMailtrap } from './usingMailtrap';

export async function validateEmail(options: MailtrapOptions) {
return await validateEmailUsingMailtrap(options);
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a400b31

Please sign in to comment.