This commit is contained in:
2025-05-27 15:06:44 +00:00
parent 073c8378c7
commit cfea44742a
5 changed files with 232 additions and 1942 deletions

View File

@ -25,7 +25,16 @@ export interface IEmailOptions {
variables?: Record<string, any>; // Template variables for placeholder replacement
}
/**
* Email class represents a complete email message.
*
* This class takes IEmailOptions in the constructor and normalizes the data:
* - 'to', 'cc', 'bcc' are always converted to arrays
* - Optional properties get default values
* - Additional properties like messageId and envelopeFrom are generated
*/
export class Email {
// INormalizedEmail properties
from: string;
to: string[];
cc: string[];
@ -38,6 +47,8 @@ export class Email {
mightBeSpam: boolean;
priority: 'high' | 'normal' | 'low';
variables: Record<string, any>;
// Additional Email-specific properties
private envelopeFrom: string;
private messageId: string;
@ -637,6 +648,57 @@ export class Email {
return this.messageId;
}
/**
* Convert the Email instance back to IEmailOptions format.
* Useful for serialization or passing to APIs that expect IEmailOptions.
* Note: This loses some Email-specific properties like messageId and envelopeFrom.
*
* @returns IEmailOptions representation of this email
*/
public toEmailOptions(): IEmailOptions {
const options: IEmailOptions = {
from: this.from,
to: this.to.length === 1 ? this.to[0] : this.to,
subject: this.subject,
text: this.text
};
// Add optional properties only if they have values
if (this.cc && this.cc.length > 0) {
options.cc = this.cc.length === 1 ? this.cc[0] : this.cc;
}
if (this.bcc && this.bcc.length > 0) {
options.bcc = this.bcc.length === 1 ? this.bcc[0] : this.bcc;
}
if (this.html) {
options.html = this.html;
}
if (this.attachments && this.attachments.length > 0) {
options.attachments = this.attachments;
}
if (this.headers && Object.keys(this.headers).length > 0) {
options.headers = this.headers;
}
if (this.mightBeSpam) {
options.mightBeSpam = this.mightBeSpam;
}
if (this.priority !== 'normal') {
options.priority = this.priority;
}
if (this.variables && Object.keys(this.variables).length > 0) {
options.variables = this.variables;
}
return options;
}
/**
* Set a custom message ID
* @param id The message ID to set