@serve.zone/platformservice
contains the platformservice container with mail, sms, letter, ai services.
Install
To install @serve.zone/platformservice
, run the following command:
npm install @serve.zone/platformservice --save
Make sure you have Node.js and npm installed on your system to use this package.
Usage
This document provides extensive usage scenarios for the @serve.zone/platformservice
, a comprehensive ESM module written in TypeScript offering a wide range of services such as mail, SMS, letter, and artificial intelligence (AI) functionalities. This service is an exemplar of a modular design, allowing users to leverage various communication methods and AI services efficiently. Key features provided by this platform include sending and receiving emails, managing SMS services, letter dispatching, and utilizing AI for diverse purposes.
Prerequisites
Before diving into the examples, ensure you have the platform service installed and configured correctly. The package leverages environment variables for configuration, so you must set up the necessary variables, including service endpoints, authentication tokens, and database connections.
Initialization
First, initialize the platform service, ensuring all dependencies are correctly loaded and configured:
import { SzPlatformService } from '@serve.zone/platformservice';
async function initService() {
const platformService = new SzPlatformService();
await platformService.start();
console.log('Platform service initialized successfully.');
}
initService();
Sending Emails
One of the primary services offered is email management. Here's how to send an email using the platform service:
import { EmailService, IEmailOptions } from '@serve.zone/platformservice';
async function sendEmail() {
const emailOptions: IEmailOptions = {
from: 'no-reply@example.com',
to: 'recipient@example.com',
subject: 'Test Email',
body: '<h1>This is a test email</h1>',
};
const emailService = new EmailService('MAILGUN_API_KEY'); // Replace with your real API key
await emailService.sendEmail(emailOptions);
console.log('Email sent successfully.');
}
sendEmail();
Managing SMS
Similar to email, the platform also facilitates SMS sending:
import { SmsService, ISmsConstructorOptions } from '@serve.zone/platformservice';
async function sendSms() {
const smsOptions: ISmsConstructorOptions = {
apiGatewayApiToken: 'SMS_API_TOKEN', // Replace with your real token
};
const smsService = new SmsService(smsOptions);
await smsService.sendSms(1234567890, 'SENDER_NAME', 'This is a test SMS.');
console.log('SMS sent successfully.');
}
sendSms();
Dispatching Letters
For physical mail correspondence, the platform provides a letter service:
import { LetterService, ILetterConstructorOptions } from '@serve.zone/platformservice';
async function sendLetter() {
const letterOptions: ILetterConstructorOptions = {
letterxpressUser: 'USER',
letterxpressToken: 'TOKEN',
};
const letterService = new LetterService(letterOptions);
await letterService.sendLetter('This is a test letter body.', {address: 'Recipient Address', name: 'Recipient Name'});
console.log('Letter dispatched successfully.');
}
sendLetter();
Mail Transfer Agent (MTA)
The platform includes a robust Mail Transfer Agent (MTA) for enterprise-grade email handling with complete control over the email delivery process:
graph TD
API[API Clients] --> ApiManager
SMTP[External SMTP Servers] <--> SMTPServer
subgraph "MTA Service"
MtaService[MTA Service] --> SMTPServer[SMTP Server]
MtaService --> EmailSendJob[Email Send Job]
MtaService --> DnsManager[DNS Manager]
MtaService --> DkimCreator[DKIM Creator]
ApiManager[API Manager] --> MtaService
end
subgraph "External Services"
DnsManager <--> DNS[DNS Servers]
EmailSendJob <--> MXServers[MX Servers]
end
The MTA service provides:
- Complete SMTP server for receiving emails
- DKIM signing and verification
- SPF and DMARC support
- DNS record management
- Retry logic with queue processing
- TLS encryption
Here's how to use the MTA service:
import { MtaService, Email } from '@serve.zone/platformservice';
async function useMtaService() {
// Initialize MTA service
const mtaService = new MtaService(platformService);
await mtaService.start();
// Send an email
const email = new Email({
from: 'sender@yourdomain.com',
to: 'recipient@example.com',
subject: 'Hello World',
text: 'This is a test email',
html: '<p>This is a <b>test</b> email</p>',
attachments: [] // Optional attachments
});
const emailId = await mtaService.send(email);
console.log(`Email queued with ID: ${emailId}`);
// Check email status
const status = mtaService.getEmailStatus(emailId);
console.log(`Email status: ${status.status}`);
// Set up API for external access
const apiManager = new ApiManager(mtaService);
await apiManager.start(3000);
console.log('MTA API running on port 3000');
}
useMtaService();
The MTA provides key advantages for applications requiring:
- High-volume email sending
- Compliance with email authentication standards
- Detailed delivery tracking
- Custom email handling logic
- Multi-domain email management
- Complete control over email infrastructure
Leveraging AI Services
The platform also integrates AI functionalities, allowing for innovative use cases like generating content, analyzing text, or automating responses:
import { AiService } from '@serve.zone/platformservice';
async function useAiService() {
const aiService = new AiService('OPENAI_API_KEY'); // Replace with your real API key
const response = await aiService.generateText('Prompt for the AI service.');
console.log(`AI response: ${response}`);
}
useAiService();
Conclusion
The @serve.zone/platformservice
offers a robust set of features for modern application requirements, including but not limited to communication and AI services. By following the examples above, developers can integrate these services into their applications, harnessing the power of email, SMS, letters, MTA capabilities, and artificial intelligence seamlessly.