node-form-mailer 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.
Potentially problematic release.
This version of node-form-mailer might be problematic. Click here for more details.
- package/.env.sample +22 -0
- package/app.js +21 -0
- package/modules/mailer.js +106 -0
- package/package.json +36 -0
- package/test/.env.sample +22 -0
- package/test/package-lock.json +1730 -0
- package/test/package.json +17 -0
- package/test/pages/contact-form.html +129 -0
- package/test/pages/thank-you.html +123 -0
- package/test/test.js +18 -0
package/.env.sample
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# This is a sample .env file for use in local development.
|
|
2
|
+
# Duplicate this file as .env in the root of the project
|
|
3
|
+
# and update the environment variables to match your
|
|
4
|
+
# desired config
|
|
5
|
+
#
|
|
6
|
+
# See the README for full descriptions of each of the
|
|
7
|
+
# available configurations.
|
|
8
|
+
|
|
9
|
+
#Node
|
|
10
|
+
NODE_ENV=
|
|
11
|
+
|
|
12
|
+
# SMTP
|
|
13
|
+
SMTP_HOST=
|
|
14
|
+
SMTP_PORT=
|
|
15
|
+
SMTP_USER=
|
|
16
|
+
SMTP_PASSWORD=
|
|
17
|
+
|
|
18
|
+
#Mail Options
|
|
19
|
+
MAIL_FROM_NAME=
|
|
20
|
+
MAIL_FROM_EMAIL=
|
|
21
|
+
MAIL_TO_NAME=
|
|
22
|
+
MAIL_TO_EMAIL=
|
package/app.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const formidable = require('formidable')
|
|
2
|
+
const mailer = require('./modules/mailer.js')
|
|
3
|
+
|
|
4
|
+
const formMailer = (req, res, next) => {
|
|
5
|
+
|
|
6
|
+
const form = formidable({ multiples: true });
|
|
7
|
+
|
|
8
|
+
form.parse(req, async(err, fields, files) => {
|
|
9
|
+
if (err) {
|
|
10
|
+
next(err);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
req.fields = fields;
|
|
14
|
+
req.files = files;
|
|
15
|
+
await mailer(req.fields,req.files)
|
|
16
|
+
next();
|
|
17
|
+
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports = formMailer
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const nodemailer = require('nodemailer')
|
|
3
|
+
const dotenv = require('dotenv')
|
|
4
|
+
dotenv.config()
|
|
5
|
+
|
|
6
|
+
const getSMTPCredientails = () => {
|
|
7
|
+
|
|
8
|
+
const SMTP = {
|
|
9
|
+
host: process.env.SMTP_HOST,
|
|
10
|
+
port: process.env.SMTP_PORT,
|
|
11
|
+
user: process.env.SMTP_USER,
|
|
12
|
+
password: process.env.SMTP_PASSWORD
|
|
13
|
+
}
|
|
14
|
+
return SMTP
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const createTestTransport = (testAccount) => {
|
|
18
|
+
|
|
19
|
+
// create reusable transporter object using the default SMTP transport
|
|
20
|
+
const transporter = nodemailer.createTransport({
|
|
21
|
+
host: "smtp.ethereal.email",
|
|
22
|
+
port: 587,
|
|
23
|
+
secure: false, // true for 465, false for other ports
|
|
24
|
+
auth: {
|
|
25
|
+
user: testAccount.user, // generated ethereal user
|
|
26
|
+
pass: testAccount.pass, // generated ethereal password
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
return transporter
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const createTransport = () => {
|
|
33
|
+
|
|
34
|
+
const SMTP = getSMTPCredientails()
|
|
35
|
+
const transporter = nodemailer.createTransport({
|
|
36
|
+
host: SMTP.host,
|
|
37
|
+
port: SMTP.port || 587,
|
|
38
|
+
secure: SMTP.port === 465, // true for 465, false for other ports
|
|
39
|
+
auth: {
|
|
40
|
+
user: SMTP.user,
|
|
41
|
+
pass: SMTP.password,
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
return transporter
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const getTransporter = (testAccount) => {
|
|
48
|
+
if(process.env.NODE_ENV === 'production') return createTransport()
|
|
49
|
+
return createTestTransport(testAccount)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const mailer = async (fields,files) => {
|
|
53
|
+
|
|
54
|
+
// Generate test SMTP service account from ethereal.email
|
|
55
|
+
// Only needed if you don't have a real mail account for testing
|
|
56
|
+
let testAccount
|
|
57
|
+
if(process.env.NODE_ENV !== 'production') {
|
|
58
|
+
testAccount = await nodemailer.createTestAccount()
|
|
59
|
+
console.log('testAccount: ', testAccount)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let transporter = getTransporter(testAccount)
|
|
63
|
+
|
|
64
|
+
const mailFromName = process.env.MAIL_FROM_NAME
|
|
65
|
+
const mailFrom = process.env.MAIL_FROM_EMAIL
|
|
66
|
+
const mailToName = process.env.MAIL_TO_NAME
|
|
67
|
+
const mailTo = process.env.MAIL_TO_EMAIL
|
|
68
|
+
|
|
69
|
+
let attachments = []
|
|
70
|
+
Object.entries(files).forEach(([file, value]) => {
|
|
71
|
+
attachments.push({
|
|
72
|
+
path:file
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
let submissionFields=''
|
|
77
|
+
Object.entries(fields).forEach(([filed, value]) => {
|
|
78
|
+
let capitalizefirstLetter= filed.charAt(0).toUpperCase()+ filed.slice(1)
|
|
79
|
+
submissionFields+=`<p><b>${capitalizefirstLetter}:</b> ${value}</p>`
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const mailSubject = fields.subject ? fields.subject : "New submission"
|
|
83
|
+
|
|
84
|
+
// send mail with defined transport object
|
|
85
|
+
let email = await transporter.sendMail({
|
|
86
|
+
from: `${mailFromName} <${mailFrom}>`, // sender address
|
|
87
|
+
to: mailTo, // list of receivers
|
|
88
|
+
subject: mailSubject, // Subject line
|
|
89
|
+
html:`<p>Hola ${mailToName}, </p>
|
|
90
|
+
<p>You have a new submission on from Contact form.</p>
|
|
91
|
+
<p style="color: #999999;"><strong>SUBMISSION<strong></p>
|
|
92
|
+
${submissionFields}
|
|
93
|
+
`, // html body
|
|
94
|
+
attachments:attachments
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
console.log("Message sent: %s", email.messageId);
|
|
98
|
+
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
|
|
99
|
+
|
|
100
|
+
// Preview only available when sending through an Ethereal account
|
|
101
|
+
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(email))
|
|
102
|
+
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports=mailer
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-form-mailer",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Simple Form Mailer. ✉",
|
|
5
|
+
"main": "app.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/LazyFolks/form-mailer.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"email",
|
|
15
|
+
"email-sender",
|
|
16
|
+
"form-to-email",
|
|
17
|
+
"form-mail",
|
|
18
|
+
"form-mailer",
|
|
19
|
+
"formtoemail"
|
|
20
|
+
],
|
|
21
|
+
"author": "Karthik",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/LazyFolks/form-mailer/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/LazyFolks/form-mailer#readme",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"dotenv": "^16.0.3",
|
|
29
|
+
"formidable": "^2.0.1",
|
|
30
|
+
"nodemailer": "^6.8.0"
|
|
31
|
+
},
|
|
32
|
+
"directories": {
|
|
33
|
+
"test": "test"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {}
|
|
36
|
+
}
|
package/test/.env.sample
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# This is a sample .env file for use in local development.
|
|
2
|
+
# Duplicate this file as .env in the root of the project
|
|
3
|
+
# and update the environment variables to match your
|
|
4
|
+
# desired config
|
|
5
|
+
#
|
|
6
|
+
# See the README for full descriptions of each of the
|
|
7
|
+
# available configurations.
|
|
8
|
+
|
|
9
|
+
#Node
|
|
10
|
+
NODE_ENV=
|
|
11
|
+
|
|
12
|
+
# SMTP
|
|
13
|
+
SMTP_HOST=
|
|
14
|
+
SMTP_PORT=
|
|
15
|
+
SMTP_USER=
|
|
16
|
+
SMTP_PASSWORD=
|
|
17
|
+
|
|
18
|
+
#Mail Options
|
|
19
|
+
MAIL_FROM_NAME=
|
|
20
|
+
MAIL_FROM_EMAIL=
|
|
21
|
+
MAIL_TO_NAME=
|
|
22
|
+
MAIL_TO_EMAIL=
|