fix: Improve email verification logic by prioritizing MX record checks and clarifying domain existence validation
This commit is contained in:
@@ -42,8 +42,8 @@ export class EmailVerificationService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Verify an email address
|
* Verify an email address
|
||||||
* - Checks if domain exists (DNS A/AAAA records)
|
* - Checks for MX records (required for receiving email)
|
||||||
* - Checks for MX records
|
* - Checks if domain exists (DNS A/AAAA records) - informational only
|
||||||
* - Detects disposable email addresses
|
* - Detects disposable email addresses
|
||||||
* - Detects forwarding/alias email addresses
|
* - Detects forwarding/alias email addresses
|
||||||
* - Suggests corrections for common typos
|
* - Suggests corrections for common typos
|
||||||
@@ -88,7 +88,23 @@ export class EmailVerificationService {
|
|||||||
result.isTypo = true;
|
result.isTypo = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if domain exists (has any DNS records)
|
// Check MX records first - this is what matters for email delivery
|
||||||
|
// A domain can receive email with only MX records, no A/AAAA records needed
|
||||||
|
try {
|
||||||
|
const mxRecords = await dns.resolveMx(domain);
|
||||||
|
result.hasMxRecords = mxRecords && mxRecords.length > 0;
|
||||||
|
if (!result.hasMxRecords) {
|
||||||
|
result.valid = false;
|
||||||
|
result.reasons.push('No MX records found for domain');
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
result.hasMxRecords = false;
|
||||||
|
result.valid = false;
|
||||||
|
result.reasons.push('No MX records found for domain');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if domain exists (has A/AAAA records) - informational only
|
||||||
|
// This doesn't affect validity since email delivery only requires MX records
|
||||||
try {
|
try {
|
||||||
await dns.resolve(domain, 'A');
|
await dns.resolve(domain, 'A');
|
||||||
result.domainExists = true;
|
result.domainExists = true;
|
||||||
@@ -98,25 +114,8 @@ export class EmailVerificationService {
|
|||||||
await dns.resolve(domain, 'AAAA');
|
await dns.resolve(domain, 'AAAA');
|
||||||
result.domainExists = true;
|
result.domainExists = true;
|
||||||
} catch {
|
} catch {
|
||||||
|
// Domain doesn't have A/AAAA records, but this is OK if it has MX records
|
||||||
result.domainExists = false;
|
result.domainExists = false;
|
||||||
result.valid = false;
|
|
||||||
result.reasons.push('Domain does not exist');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check MX records (only if domain exists)
|
|
||||||
if (result.domainExists) {
|
|
||||||
try {
|
|
||||||
const mxRecords = await dns.resolveMx(domain);
|
|
||||||
result.hasMxRecords = mxRecords && mxRecords.length > 0;
|
|
||||||
if (!result.hasMxRecords) {
|
|
||||||
result.valid = false;
|
|
||||||
result.reasons.push('No MX records found for domain');
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
result.hasMxRecords = false;
|
|
||||||
result.valid = false;
|
|
||||||
result.reasons.push('No MX records found for domain');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user