Send email from the user defined address

Send email from the user defined address

@Brugnetti brugnetti

Hello,
as explained in the examples ,I can send an email to the address indicated in the setup the “Environment Variable table”.

Now I would like send an email at the address defined by the user via the dashboard. I created a field (that the user can fill). I can read this variable by the Node.js code running in Analysis.

My problem is that I can’t define the user address as email address ==> (const email = new Services({ token: context.token }).email_csv; // this dont works) ??

/*
 * Analysis and exportation
 * Temperature Email export
 */

const { Analysis, Device, Services, Utils } = require("@tago-io/sdk");

// The function myAnalysis will run when you execute your analysis
async function myAnalysis(context) {
  // reads the values from the environment and saves it in the variable env_vars
  const env_vars = Utils.envToJson(context.environment);
  if (!env_vars.device_token) {
    return context.log("device_token environment variable not found");
  }
  if (!env_vars.email) {
    return context.log("email environment variable not found");
  }

  const device = new Device({ token: env_vars.device_token });

  // Get the [qty] last records of the variables in the device bucket.
  const email_list = await device.getData({ variable: "email", qty: 1 });
  const tempc_list = await device.getData({ variable: "tempc", qty: 1 });

  let email_csv = "Dernier email inseré :";
  let tempc_csv = "Derniere temperature lue :";
  // For each record , add the value in the csv text.
  // Use \n to break the line.
  for (const item of email_list) {
    email_csv = `${email_csv},\n${item.value}`;
  }
  for (const item of tempc_list) {
    tempc_csv = `${tempc_csv},\n${item.value}`;
  }
 // create the email message 
  const email_message = ' ==> '+ `\n${email_csv}`;
  const tempc_message = ' ==> '+ `\n${tempc_csv}`;
  context.log(email_message);
  context.log(tempc_message);

  const message = ' ATTENTION  ==>  '+ `${tempc_csv}`;

  // Start the email service
  //const email = new Services({ token: context.token }).email; // this works
  const email = new Services({ token: context.token }).email_csv; // this dont works
  
  // Send the email.
  /*
  const service_response = await email.send({
    message,
    subject: 'Temperature Mesurée par le Grive KTH01 ',
    to: env_vars.email,
  });
  context.log(service_response);
*/
  context.log('Script end.');
}

module.exports = new Analysis(myAnalysis);

Thanks