Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Lead Reporter with Email for Mautic | ||
|
||
### Installation | ||
First, install required packages with npm: | ||
|
||
$ cd /path/to/mautic-lead-reporter | ||
$ npm install | ||
|
||
After installation, edit configuration file: | ||
|
||
$ nano /path/to/config.json | ||
|
||
Finally run: | ||
|
||
$ node index.js -l 50 | ||
|
||
### Usage | ||
|
||
Usage: index [options] | ||
Options: | ||
|
||
-V, --version output the version number | ||
-l, --limit <limit> count of leads | ||
-f, --from <from> date from: filtering by last active time. (yyyy-mm-dd h:i:s) | ||
-t, --to <to> date to: Filtering by last active time. (yyyy-mm-dd h:i:s) | ||
-h, --help output usage information |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"db_host": "localhost", | ||
"db_port": 3306, | ||
"db_user": "root", | ||
"db_password": "", | ||
"db_name": "mautic_db_name", | ||
"mautic_url": "https://example.com/mautic/", | ||
"subject": "Report", | ||
"smtp": { | ||
"host": "mail.example.com", | ||
"port": "587", | ||
"username": "root@example.com", | ||
"password": "password", | ||
"secure": false | ||
}, | ||
"mail": { | ||
"from": "me@example.com", | ||
"fromName": "Example", | ||
"to": "info@example.com" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const config = require('./config'); | ||
const mysql = require('mysql'); | ||
const commander = require('commander'); | ||
|
||
commander.version('1.0.1').option('-l, --limit <limit>', 'count of leads') | ||
.option('-f, --from <from>', 'date from: Filtering by last active time. (yyyy-mm-dd h:i:s)') | ||
.option('-t, --to <to>', 'date To: Filtering by last active time. (yyyy-mm-dd h:i:s)') | ||
.parse(process.argv); | ||
|
||
var connection = mysql.createConnection({ | ||
host: config.db_host, | ||
user: config.db_user, | ||
password: config.db_password, | ||
database: config.db_name, | ||
port: config.db_port | ||
}); | ||
|
||
connection.connect( err => { | ||
if(err) console.log('error connecting: %s', err.stack); | ||
var leads = require('./mods/leads'); | ||
var options = {}; | ||
|
||
if(commander.limit) options.limit = commander.limit; | ||
if(commander.from) options.dateFrom = commander.from; | ||
if(commander.to) options.dateTo = commander.to; | ||
|
||
leads.getLastLeads(connection, options, (err, leads) => { | ||
var template = require('./mods/template'); | ||
template.generateMail(leads, (err, rendered) => { | ||
if(err) throw err; | ||
template.sendMail(rendered); | ||
}); | ||
}); | ||
} | ||
); | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var dateFormat = require('dateformat'); | ||
|
||
module.exports.getLastLeads = (connection, options, callback) => { | ||
|
||
var fn_options = { | ||
limit: 10, | ||
dateFrom: dateFormat(new Date(), 'yyyy-mm-dd 00:00:00'), | ||
dateTo: dateFormat(new Date(), 'yyyy-mm-dd 23:59:59') | ||
} | ||
|
||
if(typeof options === 'object') { | ||
if(options.limit) fn_options.limit = options.limit; | ||
if(options.dateFrom) fn_options.dateFrom = options.dateFrom; | ||
if(options.dateTo) fn_options.dateTo = options.dateTo; | ||
} | ||
|
||
var leadString = "SELECT * FROM leads where email IS NOT NULL AND last_active > '"+ fn_options.dateFrom +"' AND last_active < '"+ fn_options.dateTo +"' ORDER BY points DESC LIMIT " + fn_options.limit; | ||
|
||
connection.query(leadString, (err, results, fields) => { | ||
if(err) throw err; | ||
var leads = []; | ||
var lead_count = results.length; | ||
|
||
results.forEach( lead => { | ||
lead_info = { | ||
firstname: lead.firstname, | ||
lastname: lead.lastname, | ||
email: lead.email, | ||
phone: lead.phone, | ||
position: lead.position, | ||
company: lead.company, | ||
city: lead.city, | ||
points: lead.points, | ||
website: lead.website, | ||
profile_url: require('../config').mautic_url + "s/contacts/view/" + lead.id | ||
} | ||
|
||
leads.push(lead_info); | ||
lead_count--; | ||
if(lead_count <= 0) callback(null, leads); | ||
|
||
} | ||
); | ||
|
||
}); | ||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const handlebars = require('handlebars'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const config = require('../config'); | ||
const nodemailer = require('nodemailer'); | ||
|
||
module.exports.generateMail = (leads, callback) => { | ||
if(typeof leads !== 'object') throw Error("It is not object."); | ||
fs.readFile(path.join(__dirname, "../template/default.html"), (err, data) => { | ||
var source = data.toString(); | ||
var template = handlebars.compile(source); | ||
|
||
var context = { leads: leads, subject: config.subject }; | ||
var rendered = template(context); | ||
callback(null, rendered); | ||
}) | ||
} | ||
|
||
module.exports.sendMail = rendered => { | ||
var transporter = nodemailer.createTransport({ | ||
host: config.smtp.host, | ||
port: config.smtp.port, | ||
secure: config.smtp.secure, | ||
auth: { | ||
user: config.smtp.username, | ||
pass: config.smtp.password | ||
}, | ||
tls: { | ||
rejectUnauthorized: false | ||
} | ||
}); | ||
|
||
var mailOptions = { | ||
from: '"'+ config.mail.fromName +'" <'+ config.mail.from +'>', | ||
to: config.mail.to, | ||
subject: config.subject, | ||
html: rendered | ||
}; | ||
|
||
transporter.sendMail(mailOptions, (err, info) => { | ||
if(err) throw err; | ||
console.log(info.response); | ||
}); | ||
} |
Oops, something went wrong.