Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var helper = require('sendgrid').mail,
- ejs = require('ejs'),
- config = require('../config/transport');
- var sendgrid = require('sendgrid')(config.sendgrid.key);
- var request = require('request');
- var fs = require('fs');
- var path = require('path');
- var shortid = require('shortid');
- module.exports = {
- mails: {
- devis: (container, mail, file) => {
- let data_mail_client = {
- from: "contact@tontongaston.fr",
- to: mail,
- subject: " [" + container.name + "] Devis",
- template: container.template_mail_devis
- };
- return sendFile(Object.assign({}, data_mail_client, { values: {} }), file, 'devis.pdf');
- },
- facture: (container, mail, file) => {
- let data_mail_client = {
- from: "contact@tontongaston.fr",
- to: mail,
- subject: " [" + container.name + "] Facture",
- template: container.template_mail_facture
- };
- return sendFile(Object.assign({}, data_mail_client, { values: {} }), file, 'facture.pdf');
- },
- acompte: (container, mail, file, values) => {
- let data_mail_client = {
- from: "contact@tontongaston.fr",
- to: mail,
- subject: " [" + container.name + "] Facture",
- template: container.template_mail_facture,
- values
- };
- return sendFile(Object.assign({}, data_mail_client, { values: {} }), file, 'facture.pdf');
- },
- artisan_artisan: (container, mail, values) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: mail,
- subject: "[" + container.name + " Pro]",
- template: container.template_mail_pro,
- values
- };
- return send(Object.assign({}, data_mail));
- },
- recovery: (template, email, values) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "TontonGaston - Initialisation du mot de passe",
- template,
- values
- };
- return send(Object.assign({}, data_mail));
- },
- master_artisan_proposition: (template, email) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "TontonGaston - Nouvelle offre !",
- template,
- values: {}
- };
- return send(Object.assign({}, data_mail));
- },
- master_artisan_proposition_won: (template, email) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "TontonGaston - Offre good :)",
- template,
- values: {}
- };
- return send(Object.assign({}, data_mail));
- },
- master_artisan_proposition_lost: (template, email) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "TontonGaston - Offre perdu :(",
- template,
- values: {}
- };
- return send(Object.assign({}, data_mail));
- },
- welcome: (container, email, values) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "[" + container.name + "] ",
- template: container.template_mail_welcome,
- values
- };
- return send(Object.assign({}, data_mail));
- },
- newsletter: (container, email, values) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "[" + container.name + "] Newletters",
- template: container.template_mail_newsletter,
- values
- };
- return send(Object.assign({}, data_mail));
- },
- recovery_app: (container, email, values) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "[" + container.name + "] Réinitialisation du mot de passe",
- template: container.template_mail_password,
- values
- };
- return send(Object.assign({}, data_mail));
- },
- contact_app: (container, email) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "[" + container.name + "] demande de contact",
- template: container.template_mail_confirmation
- };
- return send(Object.assign({}, data_mail));
- },
- contact_container_app: (container, body) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: container.mail,
- subject: "[" + container.name + " - CONTACT] " + body.email,
- template: container.template_mail_contact,
- values: {
- message: body.message,
- email: body.email,
- firstname: body.firstname,
- lastname: body.lastname,
- phone: body.phone
- }
- };
- return send(Object.assign({}, data_mail));
- },
- devis_app: (container, email, service) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: " [" + container.name + "] Demande de devis - " + service,
- template: container.template_mail_confirmation
- };
- return send(Object.assign({}, data_mail));
- },
- devis_container_app: (container, body) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: container.mail,
- subject: "[" + container.name + " - " + body.service + "] " + body.email,
- template: container.template_mail_contact,
- values: {
- message: body.message,
- email: body.email,
- firstname: body.firstname,
- lastname: body.lastname,
- phone: body.phone
- }
- };
- return send(Object.assign({}, data_mail));
- },
- order_app: (container, email, service) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "[" + container.name + " - " + service + "]",
- template: container.template_mail_order
- };
- return send(Object.assign({}, data_mail));
- },
- pro_validate_ttg: (email) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "[TontonGaston] Devis en attente",
- template: "Nouveau devis proposé par Tontongaston"
- };
- return send(Object.assign({}, data_mail));
- },
- pro_finished: (email, url) => {
- let data_mail = {
- from: "contact@tontongaston.fr",
- to: email,
- subject: "[TontonGaston] Intervention terminée",
- template: "Intervention terminée, Facture en PJ"
- };
- return sendFile(Object.assign({}, data_mail), url, 'facture.pdf');
- },
- }
- };
- var download = function (url, dest, cb) {
- var file = fs.createWriteStream(dest);
- var sendReq = request.get(url);
- // verify response code
- sendReq.on('response', function (response) {
- if (response.statusCode !== 200) {
- return cb('Response status was ' + response.statusCode);
- }
- });
- // check for request errors
- sendReq.on('error', function (err) {
- fs.unlink(dest);
- return cb(err.message);
- });
- sendReq.pipe(file);
- file.on('finish', function () {
- file.close(cb); // close() is async, call cb after close completes.
- });
- file.on('error', function (err) { // Handle errors
- fs.unlink(dest); // Delete the file async. (But we don't check the result)
- return cb(err.message);
- });
- };
- var send = function (data) {
- return new Promise((resolve, reject) => {
- var str = ejs.render(data.template, data.values);
- var template = ejs.render(fs.readFileSync(path.join(__dirname, "mail.ejs"), 'utf8'), { template: str });
- let
- from = new helper.Email(data.from),
- to = new helper.Email(data.to),
- content = new helper.Content('text/html', template);
- let mail = new helper.Mail(from, data.subject, to, content);
- let request = sendgrid.emptyRequest({
- method: 'POST',
- path: '/v3/mail/send',
- body: mail.toJSON()
- });
- sendgrid.API(request, (error, response) => {
- console.log('EMAIL : ' + response.statusCode, error ? response.body.errors : null);
- resolve();
- });
- })
- }
- var sendFile = function (data, file, filename) {
- return new Promise((resolve, reject) => {
- var str = ejs.render(data.template, data.values);
- var template = ejs.render(fs.readFileSync(path.join(__dirname, "mail.ejs"), 'utf8'), { template: str });
- let from = new helper.Email(data.from),
- to = new helper.Email(data.to),
- content = new helper.Content('text/html', template);
- let mail = new helper.Mail(from, data.subject, to, content);
- var id = shortid.generate();
- download(file, __dirname + '/../tmp/' + id + '.pdf', () => {
- var attachment = new helper.Attachment();
- var file = fs.readFileSync(__dirname + '/../tmp/' + id + '.pdf');
- var base64File = new Buffer(file).toString('base64');
- attachment.setContent(base64File);
- attachment.setType('application/pdf');
- attachment.setFilename(filename);
- attachment.setDisposition('attachment');
- mail.addAttachment(attachment);
- let request = sendgrid.emptyRequest({
- method: 'POST',
- path: '/v3/mail/send',
- body: mail.toJSON()
- });
- sendgrid.API(request, (error, response) => {
- console.log('EMAIL : ' + response.statusCode);
- resolve();
- });
- })
- })
- }
Add Comment
Please, Sign In to add comment