promailer 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -0
- package/index.js +3 -0
- package/package.json +22 -0
- package/sendMail.js +110 -0
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# promailer
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
A simple and fast Node.js package to send emails using Nodemailer.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install promailer
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### mailConfg
|
|
17
|
+
|
|
18
|
+
This function is used to configure the email transporter with the necessary settings.
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
const { mailConfg } = require('promailer');
|
|
22
|
+
|
|
23
|
+
const mailConfigData = {
|
|
24
|
+
host: 'your-smtp-host',
|
|
25
|
+
port: 'your-smtp-port',
|
|
26
|
+
user: 'your-email@example.com',
|
|
27
|
+
pass: 'your-email-password',
|
|
28
|
+
tls: //true , false,
|
|
29
|
+
secure://true, false
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
mailConfg(mailConfigData);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### sendMail
|
|
36
|
+
|
|
37
|
+
This function is used to send an email using the configured transporter.
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
const { sendMail } = require('promailer');
|
|
41
|
+
|
|
42
|
+
// Sample usage
|
|
43
|
+
(async () => {
|
|
44
|
+
try {
|
|
45
|
+
const from = 'sender@example.com';
|
|
46
|
+
const to = ['recipient1@example.com', 'recipient2@example.com'];
|
|
47
|
+
const subject = 'Test Email';
|
|
48
|
+
const template = '<h1>Hello, this is a test email!</h1>';
|
|
49
|
+
|
|
50
|
+
const result = await sendMail(from, to, subject, template);
|
|
51
|
+
console.log(result);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error(error.message);
|
|
54
|
+
}
|
|
55
|
+
})();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Notes
|
|
59
|
+
|
|
60
|
+
- The `mailConfg` function needs to be called once with valid configuration before using the `sendMail` function.
|
|
61
|
+
- The `sendMail` function requires valid email addresses for both sender and recipients.
|
|
62
|
+
- The package uses Nodemailer under the hood, and it supports TLS.
|
|
63
|
+
|
|
64
|
+
## API
|
|
65
|
+
|
|
66
|
+
### mailConfg(data)
|
|
67
|
+
|
|
68
|
+
This function takes a configuration object as input and sets up the email transporter.
|
|
69
|
+
|
|
70
|
+
- `data` (Object) - An object containing mail configuration.
|
|
71
|
+
- `host` (string) - The SMTP host (e.g., "smtp.gmail.com").
|
|
72
|
+
- `port` (number) - The port number (e.g., 465).
|
|
73
|
+
- `user` (string) - The username for authentication.
|
|
74
|
+
- `pass` (string) - The password for authentication.
|
|
75
|
+
- `tls` (boolean) - Whether to use TLS (optional, defaults to false).
|
|
76
|
+
|
|
77
|
+
### sendMail(from, to, subject, template)
|
|
78
|
+
|
|
79
|
+
This function sends an email using the configured transporter.
|
|
80
|
+
|
|
81
|
+
- `from` (string) - The sender's email address.
|
|
82
|
+
- `to` (string[]) - An array of recipient email addresses.
|
|
83
|
+
- `subject` (string) - The subject of the email.
|
|
84
|
+
- `template` (string) - The HTML content of the email.
|
|
85
|
+
|
|
86
|
+
Returns a Promise that resolves to a success message with the email message ID if the email is sent successfully.
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
This project is licensed under the [ISC License](LICENSE).
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
Please note that you can customize the package name, version, author, and description in the `package.json` file based on your preferences. If you have any further questions or need additional customization, feel free to ask!
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "promailer",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Kailash Mewada",
|
|
12
|
+
"email": "kailash.mewada@encoresky.com"
|
|
13
|
+
},
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/kailashencoresky/promailer-Project"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"nodemailer": "^6.9.4"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/sendMail.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const nodemailer = require('nodemailer');
|
|
2
|
+
let transporter;
|
|
3
|
+
/// @ts-ignore mailConfig method takes a config object and creates a new transporter instance.
|
|
4
|
+
/**
|
|
5
|
+
* @ts-ignore mailConfig method takes a config object and creates a new transporter instance.
|
|
6
|
+
* This is a description of your function.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} data - An object containing mail configuration.
|
|
9
|
+
* @param {string} data.host - The SMTP host (e.g., "smtp.gmail.com").
|
|
10
|
+
* @param {number} data.port - The port number (e.g., 465).
|
|
11
|
+
* @param {string} data.user - The username for authentication.
|
|
12
|
+
* @param {string} data.pass - The password for authentication.
|
|
13
|
+
* @param {boolean} [data.tls=false] - Whether to use TLS (optional, defaults to false).
|
|
14
|
+
* @throws {Error} Will throw an error if the input data is invalid.
|
|
15
|
+
* @returns {void}
|
|
16
|
+
*/
|
|
17
|
+
function mailConfg(data) {
|
|
18
|
+
// Input validation
|
|
19
|
+
if (!data || typeof data !== 'object') {
|
|
20
|
+
throw new Error('Invalid data: Please provide an object containing mail configuration.');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (!data.host || typeof data.host !== 'string') {
|
|
24
|
+
throw new Error('Invalid host: Please provide a valid SMTP host.');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!data.port || typeof data.port !== 'number') {
|
|
28
|
+
throw new Error('Invalid port: Please provide a valid port number.');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!data.user || typeof data.user !== 'string') {
|
|
32
|
+
throw new Error('Invalid user: Please provide a valid username.');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!data.pass || typeof data.pass !== 'string') {
|
|
36
|
+
throw new Error('Invalid pass: Please provide a valid password.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// If tls is provided, make sure it's a boolean
|
|
40
|
+
if (data.tls !== undefined && typeof data.tls !== 'boolean') {
|
|
41
|
+
throw new Error('Invalid tls: Please provide a valid boolean value for tls.');
|
|
42
|
+
}
|
|
43
|
+
// If tls is provided, make sure it's a boolean
|
|
44
|
+
if (data.secure !== undefined && typeof data.secure !== 'boolean') {
|
|
45
|
+
throw new Error('Invalid secure: Please provide a valid boolean value for secure.');
|
|
46
|
+
}
|
|
47
|
+
transporter = nodemailer.createTransport({
|
|
48
|
+
tls: data.tls?data.tls:false,
|
|
49
|
+
host:data.host, //"smtp.gmail.com"
|
|
50
|
+
port: data.port, //465
|
|
51
|
+
secure: data.secure,
|
|
52
|
+
auth: {
|
|
53
|
+
user: data.user,
|
|
54
|
+
pass: data.pass
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sends an email using Nodemailer.
|
|
60
|
+
*
|
|
61
|
+
* @async
|
|
62
|
+
* @param {string} from - The sender's email address.
|
|
63
|
+
* @param {string[]} to - An array of recipient email addresses.
|
|
64
|
+
* @param {string} subject - The subject of the email.
|
|
65
|
+
* @param {string} template - The HTML content of the email.
|
|
66
|
+
* @throws {Error} Will throw an error if any of the parameters are invalid or if there's an issue sending the email.
|
|
67
|
+
* @returns {Promise<string>} A promise that resolves to a success message with the email message ID if the email is sent successfully.
|
|
68
|
+
*/
|
|
69
|
+
async function sendMail(from, to, subject, template) {
|
|
70
|
+
try {
|
|
71
|
+
if (typeof from !== 'string' || !isValidEmail(from)) {
|
|
72
|
+
throw new Error('Invalid sender email address.');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!Array.isArray(to) || to.length === 0 || !to.every(isValidEmail)) {
|
|
76
|
+
throw new Error('Invalid recipient email addresses.');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (typeof subject !== 'string' || subject.trim() === '') {
|
|
80
|
+
throw new Error('Invalid email subject.');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (typeof template !== 'string' || template.trim() === '') {
|
|
84
|
+
throw new Error('Invalid email template.');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const mailOptions = {
|
|
88
|
+
from,
|
|
89
|
+
to:to.join(','),
|
|
90
|
+
subject,
|
|
91
|
+
html: template,
|
|
92
|
+
};
|
|
93
|
+
const info = await transporter.sendMail(mailOptions);
|
|
94
|
+
return `Email sent successfully. Message ID: ${info.messageId}`;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
throw new Error(`Error sending email: ${error.message}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Validates an email address.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} email - The email address to validate.
|
|
104
|
+
* @returns {boolean} True if the email is valid, false otherwise.
|
|
105
|
+
*/
|
|
106
|
+
function isValidEmail(email) {
|
|
107
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
108
|
+
return emailRegex.test(email);
|
|
109
|
+
}
|
|
110
|
+
module.exports = { sendMail, mailConfg };
|