@bggroup/mailer
v1.0.5
Published
Node JS Basic application
Downloads
14
Keywords
Readme
BG-Mailer
📧
BG-mailer is a powerful tool for sending dynamic HTML emails. With BG-mailer, you can generate a token associated with an application, store it in the database, and use that information to customize the HTML template before sending the email.
Key Features
The heart of BG-mailer is the MailManager
class, which manages the entire email sending process. There are two main
functions you need to know to use BG-mailer:
hasPermission
: This function checks whether the provided token belongs to an application registered in the database.send
: This function sends the email. It takes the parameters{to, subject, module, data, attachments}
. Where:to
: Recipient's email address.subject
: Email subject.module
: Module to fetch from the database.data
: Values to be set in the HTML document.attachments
: An array of objects with the structure{ filename: "file name", path: "file path" }
.
Installation
Installing BG-mailer is straightforward. You just need to run the command npm i
to install all the dependencies.
import { MailManager } from '@bggroup/mailer/manager';
Methods :
send({ to, subject, module, data, from, attachments }: IDataSend)
: Sends an email using the provided data.setCredentials({host, port, user, pass}) Set the credentials to send the email
passing the template parameter :
const mailerManager = new MailManager();
mailerManager.setCredentials({
host: 'smtp.example.com',
port: '587',
user: '[email protected]',
pass: 'your_email_password',
});
const emailData = {
to: '[email protected]',
subject: 'Email with Template',
attachments: [
{
filename: 'document.pdf',
path: '/path/to/document.pdf',
},
],
template: '<html><body><h1>Hello, {{name}}!</h1><p>{{message}}</p></body></html>',
variables: {
name: 'John Doe',
message: 'Hello! Please find the attached files.',
},
};
const emailSent = await mailerManager.send(emailData);
if (!emailSent.error) {
console.log('Email sent successfully!');
} else {
console.error('Error sending email:', emailSent.error);
}
you can also pass a template and variables only :
import { MailManager } from '@bggroup/mailer/manager';
const mailerManager = new MailManager();
mailerManager.setCredentials({
host: 'smtp.example.com',
port: '587',
user: '[email protected]',
pass: 'your_email_password',
});
const emailData = {
to: '[email protected]',
subject: 'Email with Template',
template: '<html><body><h1>{{name}}</h1><p>{{message}}</p></body></html>',
variables: {
name: 'Bob',
message: 'This is a test email with template and variables.',
},
};
const emailSent = await mailerManager.send(emailData);
if (!emailSent.error) {
console.log('Email sent successfully!');
} else {
console.error('Error sending email:', emailSent.error);
}
In this example, MailManager is instantiated, the connection credentials are established, and the e-mail data is defined. The template parameter is included with the content of the HTML template and variables with the values to be replaced.
Contributing 🎁
Please feel free to contribute to the development of Mailer
. You can clone the repository, create a new branch for
your features or fixes, and submit a pull request.