module.exports = function(RED) { function EurekaMailNode(config) { os = require("os"); hostName = os.hostname(); RED.nodes.createNode(this,config); var node = this; //hostName="eurekapr"; node.warn("hostname="+ hostName); node.hostname=hostName; node.on('input', function(msg) { //node.warn(msg); let obj = {destinataires: config.destinataires, object: config.object, templates: config.templates, variables: config.variables}; //msg.payload = obj; send_mail(obj, msg); node.send(msg); }); function send_mail(obj, msg){ node.warn(obj); node.warn(msg); message = { "to": "emmanuel.esteves@eureka-solutions.fr", "cc":"", "bcc":"", "subject": "Test Template", "mailTemplateOptions" : { "id": "93d3a804-8768-4f5e-9e88-b6283faed4df", "variables": [ { "key": "Texte", "value": "Salut getzzzz" }, { "key": "Date du jour", "value": "31122022" } ] } }; //msg.payload.subject=msg.Objet; if(msg.pa!=undefined && yload.objetmsg.payload.objet!="") { message.subject = msg.payload.objet; } else { message.subject=obj.object; } if(msg.payload.destinataires!=undefined && msg.payload.destinataires!="") { message.to = msg.payload.destinataires; } else { message.to=obj.destinataires; } if(msg.payload.variables!=undefined && msg.payload.variables!="") { message.mailTemplateOptions.variables = msg.payload.variables; } else { message.mailTemplateOptions.variables=obj.variables; } if(msg.payload.mailTemplateOptions!=undefined && msg.payload.mailTemplateOptions.id!="") { message.mailTemplateOptions.id = msg.payload.mailTemplateOptions.id; } else { message.mailTemplateOptions.id=obj.templates; } node.warn(message); // msg.payload.mailTemplateOptions.id = env.get("Template"); url="http://" + hostName + ":5050/api/mail/send"; let promise = fetch(url, { method: "POST", //ou POST, PUT, DELETE, etc. headers: { "Content-Type": "application/json" }, body: JSON.stringify(message), }) .then(response => response.json()) .then(response => console.log(JSON.stringify(response))) .catch(error => console.log("Erreur : " + error)); } } RED.nodes.registerType("eureka-mail",EurekaMailNode); }