mail-microservice v1.1.3
Mail Microservice
Introduction
This document provides instructions for setting up and using the mail microservice, which allows you to manage SMTP configurations, email templates, and send emails using various SMTP servers and templates.
Usage example
Before running this example, make sure to set up the mail-microservice server.
import EmailServiceClient from 'mail-microservice';
async function main() {
const client = new EmailServiceClient('amqp://user:rabbitmq_password@localhost', {
request: 'email_requests',
response: 'email_responses',
});
await client.connect();
// Add SMTP-configuration
await client.addSMTPConfig('default', {
from: 'noreply@example.com',
host: 'HOST',
port: 587,
user: 'LOGIN',
password: 'PASSWORD',
secure: false,
tls: false,
});
// Add template
await client.addTemplate('welcome', {
subject: 'Welcome, {{name}}!',
text: 'Welcome, {{name}}!\nThis is a test message.',
html: '<b>Welcome, {{name}}!</b><br />This is a test message.',
});
// Send e-mail without callback
await client.sendEmail({
email: '2@ivanoff.org.ua',
templateName: 'welcome',
data: { name: 'John Doe' },
});
Setup
Server Setup
To set up the mail microservice server:
Clone the repository:
git clone https://github.com/ivanoff/mail-microservice.git
Navigate to the project directory:
cd mail-microservice
Build and run the Docker containers:
sudo docker compose build && sudo docker compose up
This will start Redis, RabbitMQ, and the mail-microservice server.
The server will attempt to connect to RabbitMQ. You may see messages like:
email-service-1 | Failed to connect to RabbitMQ. Retrying in 5 seconds... Failed to connect
email-service-1 | Connected
Client Setup
This microservice provides a flexible way to manage email configurations, templates, and sending. It integrates with RabbitMQ for message queuing and uses Mustache for templating. Remember to handle errors appropriately and close the connection when finished.
To use the mail-microservice in your project:
Install the npm module:
npm install mail-microservice
Import the module in your code:
import EmailServiceClient from 'mail-microservice';
Usage
Connecting to the Service
const client = new EmailServiceClient('amqp://user:rabbitmq_password@localhost', {
request: 'email_requests',
response: 'email_responses',
});
await client.connect();
Managing SMTP Configurations
Add an SMTP configuration:
await client.addSMTPConfig('default', {
from: 'noreply@example.com',
host: 'email-smtp.us-east-1.amazonaws.com',
port: 587,
user: 'AKIA...P2',
password: 'BFk...AjIyp',
secure: false,
tls: false,
});
You can also update or delete SMTP configurations using similar methods.
Managing Email Templates
Add an email template:
await client.addTemplate('welcome', {
subject: 'Welcome, {{name}}!',
text: 'Welcome, {{name}}!\nThis is a test message.',
html: '<b>Welcome, {{name}}!</b><br />This is a test message.',
});
The service uses Mustache as the templating engine. You can also update or delete templates using similar methods.
Sending Emails
There are three ways to send emails:
- Send without a callback:
await client.sendEmail({
email: 'recipient@example.com',
templateName: 'welcome',
data: { name: 'John Doe' },
});
- Send with a callback:
await client.sendEmail({
email: 'recipient@example.com',
templateName: 'welcome',
data: { name: 'John Doe' },
callback: (error, result) => {
if (error) {
console.error('Error sending email:', error);
} else {
console.log('Email sent successfully:', result);
}
}
});
- Send and wait for the result:
try {
const result = await client.sendEmailAndWaitForResult({
email: 'recipient@example.com',
templateName: 'welcome',
data: { name: 'John Doe' },
});
console.log('Email sent successfully:', result);
} catch(error) {
console.error('Error sending email:', error);
}
Closing the Connection
When you're done using the client, close the connection:
await client.close();
License
This project is distributed under the MIT
license. See the LICENSE file for more information.
Please note that ClamAV
is licensed under the Apache 2.0 license. More details can be found here
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Support
If you encounter any problems or have questions, please open an issue in the project repository.
Created by
Dimitry Ivanov 2@ivanoff.org.ua # curl -A cv ivanoff.org.ua