feat(smartmail): Add new email validation helper methods (getMxRecords, isDisposableEmail, isRoleAccount) and an applyVariables method to Smartmail for dynamic templating.

This commit is contained in:
Philipp Kunz 2025-05-07 14:57:50 +00:00
parent ffe324a9dc
commit f08544d53a
4 changed files with 94 additions and 1 deletions

View File

@ -1,5 +1,13 @@
# Changelog
## 2025-05-07 - 2.1.0 - feat(smartmail)
Add new email validation helper methods (getMxRecords, isDisposableEmail, isRoleAccount) and an applyVariables method to Smartmail for dynamic templating.
- Introduced getMxRecords in EmailAddressValidator to extract MX records from DNS responses.
- Added isDisposableEmail to determine if an email is from a disposable domain.
- Added isRoleAccount to identify role-based email addresses.
- Implemented applyVariables in Smartmail to update subject, body, and htmlBody templates with provided data.
## 2025-05-07 - 2.0.1 - fix(readme)
Update documentation to include usage of creation object reference and update API details.

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartmail',
version: '2.0.1',
version: '2.1.0',
description: 'A unified format for representing and dealing with emails, with support for attachments and email validation.'
}

View File

@ -110,6 +110,65 @@ export class EmailAddressValidator {
return result;
}
/**
* Gets the MX records for a domain
* @param domain The domain to get MX records for
* @returns Array of MX records as strings
*/
public async getMxRecords(domain: string): Promise<string[]> {
const mxRecords = await this.checkMxRecords(domain);
if (!mxRecords || !Array.isArray(mxRecords)) {
return [];
}
// Extract exchange values from MX records
return mxRecords.map((record: any) => {
if (record && record.exchange) {
return record.exchange;
}
return '';
}).filter(Boolean);
}
/**
* Checks if an email is from a disposable domain
* @param email The email address to check
* @returns True if the email is from a disposable domain
*/
public async isDisposableEmail(email: string): Promise<boolean> {
await this.fetchDomains();
if (!this.isValidEmailFormat(email)) {
return false;
}
const domainPart = email.split('@')[1];
return this.domainMap[domainPart] === 'disposable';
}
/**
* Checks if an email is a role account (e.g. info@, support@, etc.)
* @param email The email address to check
* @returns True if the email is a role account
*/
public isRoleAccount(email: string): boolean {
if (!this.isValidEmailFormat(email)) {
return false;
}
const localPart = email.split('@')[0].toLowerCase();
const roleAccounts = [
'admin', 'administrator', 'webmaster', 'hostmaster', 'postmaster',
'info', 'support', 'sales', 'marketing', 'contact', 'help',
'abuse', 'noc', 'security', 'billing', 'donations', 'donate',
'staff', 'office', 'hr', 'jobs', 'careers', 'team',
'enquiry', 'enquiries', 'feedback', 'no-reply', 'noreply'
];
return roleAccounts.includes(localPart);
}
/**
* Validates an email address
* @param emailArg The email address to validate

View File

@ -74,6 +74,32 @@ export class Smartmail<T> {
return smartmustache.applyData(dataArg);
}
/**
* Applies variables to all template strings in the email
* @param variables Variables to apply to templates
*/
public applyVariables(variables: Record<string, any>): void {
if (!variables || typeof variables !== 'object') {
return;
}
// Process the subject, body, and HTML body with the provided variables
if (this.options.subject) {
const subjectMustache = new plugins.smartmustache.SmartMustache(this.options.subject);
this.options.subject = subjectMustache.applyData(variables);
}
if (this.options.body) {
const bodyMustache = new plugins.smartmustache.SmartMustache(this.options.body);
this.options.body = bodyMustache.applyData(variables);
}
if (this.options.htmlBody) {
const htmlBodyMustache = new plugins.smartmustache.SmartMustache(this.options.htmlBody);
this.options.htmlBody = htmlBodyMustache.applyData(variables);
}
}
/**
* Gets the processed plain text body with template variables applied
* @param dataArg Data to apply to the template