triggeremailfast 1.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of triggeremailfast might be problematic. Click here for more details.

package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # MailSender Email Sender #
2
+ This is a custome module that provides a simple way to send emails using Nodemailer. It allows you to configure the SMTP server and credentials, and then send emails with ease.
3
+
4
+ Installation
5
+ To use this module, make sure you have Node.js installed on your system. Then, follow these steps:
6
+
7
+ Clone or download this repository to your project folder.
8
+ Install the required dependencies by running the following command:
9
+
10
+ npm install mailsender-1.0.0.tgz
11
+
12
+ const { sendMail, mailConfg } = require('./path/to/mailsender');
13
+
14
+
15
+ Configure the email transporter with your email credentials:
16
+
17
+ // Call this function before sending any emails
18
+ mailConfg({
19
+ user: 'your_email@gmail.com',
20
+ pass: 'your_email_password',
21
+ });
22
+
23
+ Send an email using the sendMail function:
24
+
25
+ const from = 'your_email@gmail.com';
26
+ const to = ['recipient1@example.com', 'recipient2@example.com'];
27
+ const subject = 'Email Subject';
28
+ const template = '<p>This is the email content.</p>';
29
+
30
+ sendMail(from, to, subject, template)
31
+ .then(message => {
32
+ console.log(message);
33
+ })
34
+ .catch(error => {
35
+ console.error(error.message);
36
+ });
37
+
38
+
39
+ **Note**: Ensure that you allow "Less Secure Apps" on your Gmail account if you are using Gmail as your SMTP server. For production use, consider using an application-specific password instead.
40
+
41
+ API Reference
42
+ mailConfg(config)
43
+ This function sets up the email transporter with the provided configuration.
44
+
45
+ config: An object containing the email credentials.
46
+ user: Your email address (e.g., 'your_email@gmail.com').
47
+ pass: Your email password or application-specific password.
48
+ sendMail(from, to, subject, template)
49
+ This function sends an email to the specified recipients.
50
+
51
+ from: The email address of the sender.
52
+ to: An array of email addresses of the recipients.
53
+ subject: The subject of the email.
54
+ template: The HTML content of the email.
55
+ Returns a promise that resolves to a success message containing the sent email's message ID or rejects with an error if the email sending process fails.
56
+
57
+ **License**
58
+ This project is licensed under the MIT License - see the LICENSE file for details.
59
+
60
+ Acknowledgments
61
+ This module utilizes the Nodemailer library to facilitate email sending. Nodemailer is a wonderful tool for email communication in Node.js applications.
62
+
63
+ For more information about Nodemailer, visit their website.
64
+
65
+ Replace path/to/emailSender in the import statement with the actual path to the emailSender.js file in your project.
66
+
67
+ Please ensure to replace your_email@gmail.com and your_email_password in the examples with your actual Gmail account and password or application-specific password.
68
+
69
+ Additionally, you may customize the email content template to suit your specific use case. This template supports HTML, so you can format the email as needed.
70
+
71
+ Remember to include any additional setup instructions or configuration details that are specific to your application or use case
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const {sendMail,mailConfg} = require('./sendMail');
2
+
3
+ module.exports = { sendMail,mailConfg };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "triggeremailfast",
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": "Kailash Mewada",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "nodemailer": "^6.9.4"
14
+ }
15
+ }
package/sendMail.js ADDED
@@ -0,0 +1,106 @@
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
+ transporter = nodemailer.createTransport({
44
+ tls: data.tls?data.tls:false,
45
+ host:data.host, //"smtp.gmail.com"
46
+ port: data.port, //465
47
+ secure: true,
48
+ auth: {
49
+ user: data.user,
50
+ pass: data.pass
51
+ },
52
+ });
53
+ }
54
+ /**
55
+ * Sends an email using Nodemailer.
56
+ *
57
+ * @async
58
+ * @param {string} from - The sender's email address.
59
+ * @param {string[]} to - An array of recipient email addresses.
60
+ * @param {string} subject - The subject of the email.
61
+ * @param {string} template - The HTML content of the email.
62
+ * @throws {Error} Will throw an error if any of the parameters are invalid or if there's an issue sending the email.
63
+ * @returns {Promise<string>} A promise that resolves to a success message with the email message ID if the email is sent successfully.
64
+ */
65
+ async function sendMail(from, to, subject, template) {
66
+ try {
67
+ if (typeof from !== 'string' || !isValidEmail(from)) {
68
+ throw new Error('Invalid sender email address.');
69
+ }
70
+
71
+ if (!Array.isArray(to) || to.length === 0 || !to.every(isValidEmail)) {
72
+ throw new Error('Invalid recipient email addresses.');
73
+ }
74
+
75
+ if (typeof subject !== 'string' || subject.trim() === '') {
76
+ throw new Error('Invalid email subject.');
77
+ }
78
+
79
+ if (typeof template !== 'string' || template.trim() === '') {
80
+ throw new Error('Invalid email template.');
81
+ }
82
+
83
+ const mailOptions = {
84
+ from,
85
+ to:to.join(','),
86
+ subject,
87
+ html: template,
88
+ };
89
+ const info = await transporter.sendMail(mailOptions);
90
+ return `Email sent successfully. Message ID: ${info.messageId}`;
91
+ } catch (error) {
92
+ throw new Error(`Error sending email: ${error.message}`);
93
+ }
94
+
95
+ }
96
+ /**
97
+ * Validates an email address.
98
+ *
99
+ * @param {string} email - The email address to validate.
100
+ * @returns {boolean} True if the email is valid, false otherwise.
101
+ */
102
+ function isValidEmail(email) {
103
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
104
+ return emailRegex.test(email);
105
+ }
106
+ module.exports = { sendMail, mailConfg };