ZoeApi/controllers/emailController.js

346 lines
17 KiB
JavaScript

require('../connections/mysql-server');
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
//service: 'gmail',
host: "email-ssl.com.br",
port: 465,
secure: true,
auth: {
//user: 'trocasenhamyrobot@gmail.com',
//pass: 'trocasenhamyrobot2021',
//user: 'iyrconline@myrobot.com.br',
//pass: 'iyrcMyrobot@2255',
//user: 'myrobotschool@gmail.com',
//pass: 'myrobot2020**==',
user: 'staff@myrobot.com.br',
pass: 'stAff66**'
}
});
var routes = function () {
router.post('/send', (req, res) => {
EnviarEmail(req.body.to, req.body.subject, req.body.text, function(Sucesso) {
res.send(Sucesso);
});
});
function EnviarEmail(Email, Assunto, Texto, callback) {
var mailOptions = {
from: 'MyRobot School <staff@myrobot.com.br>',
to: Email,
subject: Assunto,
html: Texto,
/*attachments: [{ // Basta incluir esta chave e listar os anexos
filename: 'boleto.pdf', // O nome que aparecerá nos anexos
path: 'servidor/boletos/boleto_gerado1234.pdf' // O arquivo será lido neste local ao ser enviado
}]*/
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
callback(false);
} else {
console.log('Email sent to: ' + mailOptions.to + ' - ' + info.response);
callback(true);
}
});
}
router.get('/getRequest/:encript', (req, res) => {
encript = req.params.encript;
execSQLQuery(`SELECT * FROM requestpass WHERE encript = '${encript}'`, res, req.originalUrl);
})
router.get('/getReqNotExpired/:idusers/:email/:db', (req, res) => {
id = req.params.idusers;
email = req.params.email;
db = req.params.db;
execSQLQuery(`
SELECT *
FROM requestpass
WHERE registration = '${id}' AND email = '${email}' AND db = '${db}'`,
res,
req.originalUrl
);
})
router.post('/insert', (req, res) => {
const idrequestpass = req.body.idrequestpass;
const registration = req.body.registration;
const email = req.body.email;
const encript = req.body.encript;
const db = req.body.db;
const expired = req.body.expired;
execSQLQuery(`
INSERT INTO
requestpass(
registration, email, encript, db, expired
) VALUES (
${registration}, '${email}', '${encript}', '${db}', ${expired}
)`,
res,
req.originalUrl
);
})
router.patch('/update', (req, res) => {
const idrequestpass = req.body.idrequestpass;
const registration = req.body.registration;
const email = req.body.email;
const encript = req.body.encript;
const db = req.body.db;
const expired = req.body.expired;
execSQLQuery(`
UPDATE
requestpass SET
registration = ${registration},
email = '${email}',
encript = '${encript}',
db = '${db}',
expired = ${expired}
WHERE idrequestpass = ${idrequestpass}`,
res,
req.originalUrl
);
})
router.patch('/expired', (req, res) => {
const idrequestpass = req.body.idrequestpass;
const expired = req.body.expired;
execSQLQuery(`
UPDATE
requestpass SET
expired = ${expired}
WHERE idrequestpass = ${idrequestpass}`,
res,
req.originalUrl
);
})
router.post('/insertLeadsSite', (req, res) => {
const idleadseventos = parseInt(req.body.idleadseventos);
const idunits = parseInt(req.body.idunits);
const nome = req.body.nome;
let telefone = req.body.telefone;
let email = req.body.email;
const horario = req.body.horario;
const formacontato = req.body.forma;
const contato = req.body.contato;
if(formacontato === "WhatsApp") {
telefone = contato;
req.body.telefone = telefone;
} else if(formacontato === "Email") {
email = contato;
req.body.email = email;
}
mysqlConnection.query(`insert into leadssite (idleadseventos, idunits, nome, telefone, email, horario) values (?, ?, ?, ?, ?, ?);`, [idleadseventos, idunits, nome, telefone, email, horario], function (error, response, fields) {
mysqlConnection.query(`select
un.unityname,
un.manager,
un.email,
le.descricao,
le.idleadseventos
from units un
inner join leadseventos le on le.idleadseventos = ${idleadseventos}
where un.idunits = ${idunits};`, function (error, InfoUnidade, fields) {
const Assunto = InfoUnidade[0]['descricao'];
const Texto = MontaEmailCaptacaoLeadsSite(InfoUnidade[0], req.body);
EnviarEmail(InfoUnidade[0]['email'], Assunto, Texto, function(Sucesso) {
const EmailInterno = "joao.pedro@makereducacao.com.br";
EnviarEmail(EmailInterno, Assunto, Texto, function(Sucesso2) {
res.send(Sucesso);
});
});
});
});
});
function MontaEmailCaptacaoLeadsSite(DadosEvento, DadosLead) {
let TextoEmail = "";
switch (DadosEvento.idleadseventos) {
case 1:
let Contato = DadosLead.contato;
let BotaoContato = "";
let OldContato = Contato;
if(DadosLead.forma === "WhatsApp") {
Contato = Contato.replace("(" , "").replace(")","").replace(" ","").replace("-","");
BotaoContato = `<div style="width:fit-content;background: #08d319;padding: 1rem 2rem;border-radius: 1.5rem;margin: 0 auto;margin-top: 1rem;cursor: pointer;"><a class="button" style="text-decoration: none;color: #fff;font-weight: bold;font-family: sans-serif;font-size: 18px" href="https://wa.me/55${Contato}" target="_blank">Entrar em contato</a></div>`;
} else if (DadosLead.forma === "Email") {
BotaoContato = `<div style="width:fit-content;background: #0c60fd;padding: 1rem 2rem;border-radius: 1.5rem;margin: 0 auto;margin-top: 1rem;cursor: pointer;"><a class="button" style="text-decoration: none;color: #fff;font-weight: bold;font-family: sans-serif;font-size: 18px" href="mailto:${Contato}" target="_blank">Entrar em contato</a></div>`
}
TextoEmail = `
<div style="background-color:#f7f7f7">
<div style="margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:10px;padding-left:40px;padding-right:40px;text-align:center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border:1px solid #d9d9d9;background:white;background-color:white;margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:white;background-color:white;width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0px 0;padding-bottom:30px;text-align:center">
<div style="margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0;padding-left:0;padding-right:0;text-align:center">
<div class="m_-4945555513056369728mj-column-per-100" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top" width="100%">
<tbody>
<tr>
<td align="center" style="font-size:0px;padding:0 0;word-break:break-word">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px">
<tbody>
<tr>
<td style="width:600px">
<img height="auto" src="https://myrobotlab.com.br/assets/images/MailInauguracao/mail-banner.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px" width="600" class="CToWUd a6T" tabindex="0">
<div class="a6S" dir="ltr" style="opacity: 0.01; left: 677.5px; top: 180px;">
<div id=":173" class="T-I J-J5-Ji aQv T-I-ax7 L3 a5q" role="button" tabindex="0" aria-label="Fazer o download do anexo " data-tooltip-class="a1V" data-tooltip="Fazer o download">
<div class="aSK J-J5-Ji aYr"></div>
</div>
</div>
<div style="font-family:sans-serif;font-size:22px;line-height:150%;text-align:center;color:#FF7600;font-weight: bolder;margin-top: 20px;">Nova visita agendada!</div>
<hr style="width: 75%;margin: auto;background-color: #303030;margin-top: 20px;">
<div style="font-family:sans-serif;font-size:18px;line-height:150%;text-align:center;color:#000;margin-top: 15px;"><strong style="color: #303030" >Nome:</strong> ${DadosLead.nome}</div>
<div style="font-family:sans-serif;font-size:18px;line-height:150%;text-align:center;color:#000;margin-top: 5px;"><strong style="color: #303030">Horario:</strong> ${DadosLead.horario}</div>
<div style="font-family:sans-serif;font-size:18px;line-height:150%;text-align:center;color:#000;margin-top: 5px;"><strong style="color: #303030" >Forma de contato:</strong> ${DadosLead.forma}</div>
<div style="font-family:sans-serif;font-size:18px;line-height:150%;text-align:center;color:#000;margin-top: 5px;"><strong style="color: #303030" >Contato:</strong> ${OldContato}</div>
${BotaoContato}
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div style="margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:10px;padding-left:40px;padding-right:40px;text-align:center"></td>
</tr>
</tbody>
</table>
</div>
</div>
`;
break;
case 2:
TextoEmail = ` <div style="background-color:#f7f7f7">
<div style="margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:10px;padding-left:40px;padding-right:40px;text-align:center"></td>
</tr>
</tbody>
</table>
</div>
<div style="border:1px solid #d9d9d9;background:white;background-color:white;margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="background:white;background-color:white;width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0px 0;padding-bottom:30px;text-align:center">
<div style="margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:0;padding-left:0;padding-right:0;text-align:center">
<div class="m_-4945555513056369728mj-column-per-100" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top" width="100%">
<tbody>
<tr>
<td align="center" style="font-size:0px;padding:0 0;word-break:break-word">
<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:collapse;border-spacing:0px">
<tbody>
<tr>
<td style="width:600px">
<img height="auto" src="https://myrobotlab.com.br/assets/images/RoboxOnMail/mail-banner.png" style="border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px" width="600" class="CToWUd a6T" tabindex="0">
<div class="a6S" dir="ltr" style="opacity: 0.01; left: 677.5px; top: 180px;">
<div id=":173" class="T-I J-J5-Ji aQv T-I-ax7 L3 a5q" role="button" tabindex="0" aria-label="Fazer o download do anexo " data-tooltip-class="a1V" data-tooltip="Fazer o download">
<div class="aSK J-J5-Ji aYr"></div>
</div>
</div>
<div style="font-family:sans-serif;font-size:22px;line-height:150%;text-align:center;color:#FF7600;font-weight: bolder;margin-top: 20px;">Novo lead captado!</div>
<hr style="width: 75%;margin: auto;background-color: #303030;margin-top: 20px;">
<div style="font-family:sans-serif;font-size:18px;line-height:150%;text-align:center;color:#000;margin-top: 15px;"><strong style="color: #303030" >Nome:</strong> ${DadosLead.nome}</div>
<div style="font-family:sans-serif;font-size:18px;line-height:150%;text-align:center;color:#000;margin-top: 5px;"><strong style="color: #303030" >Telefone:</strong> ${DadosLead.telefone}</div>
<div style="font-family:sans-serif;font-size:18px;line-height:150%;text-align:center;color:#000;margin-top: 5px;"><strong style="color: #303030" >Email:</strong> ${DadosLead.email}</div>
<div style="width:fit-content;background: #08d319;padding: 1rem 2rem;border-radius: 1.5rem;margin: 0 auto;margin-top: 1rem;cursor: pointer;"><a class="button" style="text-decoration: none;color: #fff;font-weight: bold;font-family: sans-serif;font-size: 18px" href="https://wa.me/55${DadosLead.telefone.replace("(","").replace(")","").replace("-","").replace(" ","")}" target="_blank">Abrir no WhatsApp</a></div>
<div style="width:fit-content;background: #0c60fd;padding: 1rem 2rem;border-radius: 1.5rem;margin: 0 auto;margin-top: 1rem;cursor: pointer;"><a class="button" style="text-decoration: none;color: #fff;font-weight: bold;font-family: sans-serif;font-size: 18px" href="mailto:${DadosLead.email}" target="_blank">Escrever email</a></div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div style="margin:0px auto;max-width:600px">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%">
<tbody>
<tr>
<td style="direction:ltr;font-size:0px;padding:10px;padding-left:40px;padding-right:40px;text-align:center"></td>
</tr>
</tbody>
</table>
</div>
</div>
`;
break;
}
return TextoEmail;
}
return router;
}
module.exports = routes;