UNPKG

@omx-sdk/email

Version:

Email sending functionality for omx-sdk

211 lines 6.78 kB
export class EmailClient { constructor(config) { this.config = config; } /** * Send a single email */ async send(message) { try { // Validate required fields this.validateMessage(message); // Prepare the email payload const payload = this.preparePayload(message); // Simulate API call await this.makeApiCall('/send', payload); return { success: true, messageId: this.generateMessageId(), statusCode: 200, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error', statusCode: 500, }; } } /** * Send bulk emails */ async sendBulk(messages, options) { const batchSize = options?.batchSize || 10; const delay = options?.delay || 1000; const results = []; for (let i = 0; i < messages.length; i += batchSize) { const batch = messages.slice(i, i + batchSize); // Process batch in parallel const batchPromises = batch.map((message) => this.send(message)); const batchResults = await Promise.all(batchPromises); results.push(...batchResults); // Add delay between batches (except for the last batch) if (i + batchSize < messages.length && delay > 0) { await this.sleep(delay); } } return results; } /** * Send email using a template */ async sendTemplate(template, recipients, variables) { try { const mergedVariables = { ...template.variables, ...variables }; // Simulate template processing const processedContent = this.processTemplate(template, mergedVariables); const message = { to: recipients, from: this.config.defaultFrom, subject: processedContent.subject, body: processedContent.body, html: processedContent.html, }; return await this.send(message); } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Template processing failed', statusCode: 500, }; } } /** * Validate email address format */ validateEmail(email) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } /** * Get delivery status of an email */ async getDeliveryStatus(messageId) { try { // Simulate API call to check status await this.makeApiCall(`/status/${messageId}`); return { messageId, status: 'delivered', timestamp: new Date(), }; } catch (error) { return { messageId, status: 'failed', error: error instanceof Error ? error.message : 'Status check failed', }; } } /** * Get email statistics */ async getStats(dateFrom, dateTo) { try { // Simulate API call await this.makeApiCall('/stats', { dateFrom, dateTo }); return { sent: 100, delivered: 95, failed: 3, bounced: 2, opened: 80, clicked: 15, }; } catch (error) { throw new Error(`Failed to get stats: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Validate email message */ validateMessage(message) { if (!message.to || (Array.isArray(message.to) && message.to.length === 0)) { throw new Error('Recipient email is required'); } if (!message.subject || message.subject.trim() === '') { throw new Error('Email subject is required'); } if (!message.body || message.body.trim() === '') { throw new Error('Email body is required'); } // Validate email addresses const recipients = Array.isArray(message.to) ? message.to : [message.to]; for (const email of recipients) { if (!this.validateEmail(email)) { throw new Error(`Invalid email address: ${email}`); } } if (message.from && !this.validateEmail(message.from)) { throw new Error(`Invalid from email address: ${message.from}`); } } /** * Prepare email payload for API */ preparePayload(message) { return { to: message.to, from: message.from || this.config.defaultFrom, subject: message.subject, body: message.body, html: message.html, cc: message.cc, bcc: message.bcc, attachments: message.attachments, replyTo: message.replyTo, priority: message.priority || 'normal', }; } /** * Make API call (simulated) */ async makeApiCall(endpoint, data) { const url = `${this.config.baseUrl || 'https://api.oxinion.com/email'}${endpoint}`; // Simulate API call delay await this.sleep(Math.random() * 500 + 100); console.log(`API Call to ${url}`, data); // Simulate occasional failures for testing if (Math.random() < 0.05) { throw new Error('API call failed'); } return { success: true }; } /** * Process email template */ processTemplate(template, variables) { // Simple template variable replacement let subject = `Template: ${template.name}`; let body = `Hello, this is a template email with variables: ${JSON.stringify(variables)}`; let html = `<h1>Template: ${template.name}</h1><p>Variables: ${JSON.stringify(variables)}</p>`; return { subject, body, html }; } /** * Generate unique message ID */ generateMessageId() { return `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } /** * Sleep utility */ sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } /** * Get client configuration */ getConfig() { return { ...this.config }; } } // Export default instance creation helper export function createEmailClient(config) { return new EmailClient(config); } //# sourceMappingURL=index.js.map