Updates
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import handleNodemailer, {
|
||||
HandleNodemailerParam,
|
||||
} from "../../backend/handleNodemailer";
|
||||
import emailMxLookup from "../verification/email-mx-lookup";
|
||||
import emailRegexCheck from "../verification/email-regex-test";
|
||||
|
||||
type Param = {
|
||||
email?: string;
|
||||
welcomeEmailOptions?: HandleNodemailerParam;
|
||||
};
|
||||
export default async function validateEmail({
|
||||
email,
|
||||
welcomeEmailOptions,
|
||||
}: Param): Promise<{ isValid: boolean; message?: string }> {
|
||||
if (!email) {
|
||||
return {
|
||||
isValid: false,
|
||||
message: "Email is required.",
|
||||
};
|
||||
}
|
||||
|
||||
if (!emailRegexCheck(email)) {
|
||||
return {
|
||||
isValid: false,
|
||||
message: "Invalid email format.",
|
||||
};
|
||||
}
|
||||
|
||||
const checkEmailMxRecords = await emailMxLookup(email);
|
||||
|
||||
if (!checkEmailMxRecords) {
|
||||
return {
|
||||
isValid: false,
|
||||
message: "Email domain does not have valid MX records.",
|
||||
};
|
||||
}
|
||||
|
||||
if (welcomeEmailOptions) {
|
||||
const welcomeEmail = await handleNodemailer(welcomeEmailOptions);
|
||||
if (!welcomeEmail?.accepted?.[0]) {
|
||||
return {
|
||||
isValid: false,
|
||||
message: "Email verification failed.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isValid: true,
|
||||
message: "Email is valid.",
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import dns from "dns";
|
||||
import debugLog from "../../../utils/logging/debug-log";
|
||||
|
||||
export default function emailMxLookup(
|
||||
email?: string,
|
||||
debug?: boolean
|
||||
): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!email) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const domain = email.split("@")[1];
|
||||
|
||||
dns.resolveMx(domain, (err, addresses) => {
|
||||
if (err || !addresses.length) {
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: err?.message || "No MX records found",
|
||||
addTime: true,
|
||||
label: "Email MX Lookup",
|
||||
type: "error",
|
||||
});
|
||||
}
|
||||
resolve(false);
|
||||
} else {
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: addresses,
|
||||
addTime: true,
|
||||
label: "MX Records",
|
||||
});
|
||||
}
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export default function emailRegexCheck(email: string): boolean {
|
||||
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
return regex.test(email);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import net from "net";
|
||||
import dns from "dns";
|
||||
|
||||
export default function verifyEmailSMTP(email: string): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const domain = email.split("@")[1];
|
||||
|
||||
dns.resolveMx(domain, (err, addresses) => {
|
||||
if (err || !addresses.length) {
|
||||
console.log("Invalid email domain.");
|
||||
return;
|
||||
}
|
||||
|
||||
const mxServer = addresses[0].exchange;
|
||||
console.log(`Connecting to ${mxServer} to verify email...`);
|
||||
|
||||
const client = net.createConnection(25, mxServer);
|
||||
|
||||
client.on("connect", () => {
|
||||
console.log("Connected to SMTP server.");
|
||||
client.write("HELO example.com\r\n");
|
||||
client.write(`MAIL FROM: <test@example.com>\r\n`);
|
||||
client.write(`RCPT TO: <${email}>\r\n`);
|
||||
});
|
||||
|
||||
client.on("data", (data) => {
|
||||
const response = data.toString();
|
||||
|
||||
if (response.includes("250")) {
|
||||
console.log("✅ Email exists!");
|
||||
resolve(true);
|
||||
} else {
|
||||
console.log("❌ Email does not exist.");
|
||||
resolve(false);
|
||||
}
|
||||
|
||||
client.end();
|
||||
});
|
||||
|
||||
client.on("error", (err) => {
|
||||
console.log("SMTP verification failed:", err.message);
|
||||
resolve(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user