53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
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.",
|
|
};
|
|
}
|