A robust TypeScript email service package with automatic failover and multiple provider support. Built for reliability and ease of use in modern Node.js applications.
- Automatic Failover: Seamlessly switch between providers to ensure delivery
-
Multiple Provider Support:
- SendGrid
- Mailgun
- Extensible architecture for custom providers
- TypeScript First: Full type safety and comprehensive definitions
-
Reliability Features:
- Priority-based provider selection
- Automatic retry mechanism
- Health monitoring
- Configurable timeouts
-
Developer Experience:
- Clean, intuitive API
- Comprehensive error handling
- Detailed logging options
npm install skymail
import { EmailService, SendGridProvider, MailgunProvider } from 'skymail';
// Initialize providers
const sendgrid = new SendGridProvider({
apiKey: 'YOUR_SENDGRID_API_KEY',
maxRetries: 3,
retryDelay: 1000
});
const mailgun = new MailgunProvider({
apiKey: 'YOUR_MAILGUN_API_KEY',
domain: 'YOUR_DOMAIN',
maxRetries: 3,
retryDelay: 1000
});
// Create service with failover
const emailService = new EmailService([
{ provider: sendgrid, priority: 1 }, // Primary
{ provider: mailgun, priority: 2 } // Backup
]);
// Send email
try {
const success = await emailService.sendEmail({
to: 'recipient@example.com',
from: 'sender@yourdomain.com',
subject: 'Hello!',
text: 'Message content',
html: '<p>Message content</p>'
});
console.log('Email sent:', success);
} catch (error) {
console.error('Failed to send:', error);
}
The package follows a modular design with three main components:
- Email Service: Orchestrates sending process and manages providers
- Providers: Implement email sending logic for specific services
- Core Abstractions: Define interfaces and base classes
interface SendGridConfig {
apiKey: string; // Required
maxRetries?: number; // Optional (default: 3)
retryDelay?: number; // Optional (default: 1000ms)
}
interface MailgunConfig {
apiKey: string; // Required
domain: string; // Required
maxRetries?: number; // Optional (default: 3)
retryDelay?: number; // Optional (default: 1000ms)
}
interface EmailMessage {
to: string | string[];
from: string;
subject: string;
text: string;
html?: string;
cc?: string | string[];
bcc?: string | string[];
replyTo?: string;
attachments?: Array<{
filename: string;
content: Buffer | string;
contentType?: string;
}>;
}
import { AbstractEmailProvider, EmailMessage } from 'skymail';
export class CustomProvider extends AbstractEmailProvider {
constructor(config: YourConfig) {
super(config);
}
protected async sendEmailRequest(message: EmailMessage): Promise<boolean> {
// Implement provider-specific sending logic
return true;
}
public async isAvailable(): Promise<boolean> {
// Implement availability check
return true;
}
}
// Add new provider
emailService.addProvider(newProvider, 3);
// Remove provider
emailService.removeProvider(existingProvider);
// Update priority
emailService.updatePriority(existingProvider, 2);
// Check status
const status = await emailService.getProvidersStatus();
try {
const result = await emailService.sendEmail(message);
if (!result) {
// All providers failed
}
} catch (error) {
if (error instanceof InvalidEmailError) {
// Handle validation errors
} else {
// Handle other errors
}
}
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the ISC License - see the LICENSE file for details.
- Issues: GitHub Issues
- Documentation: Full Documentation