Passing a value to agent- how?

Hey group,
I’ve never done any C programming… Been using PIC Basic for several years, so all my programming reference is based on BASIC.
So please help me to understand how to pass a value to the Agent and THEN to assign that value to a variable to be used in the agent code.

I’m trying to sent an email that contains a temperature value.

Device Code… (partial)
agent.send("mailgun", farenheit);

Agent Code… (complete)
`
local EMAIL_MESSAGE
const API_KEY = “key-a4n6bfb44505c002b8b90daa839836a1”;
const MAILGUN_URL = “api.mailgun.net/v2/”;
const MAILGUN_DOMAIN = “sandboxc7947bdaf04ae0a0771d8f541f9f8b.mailgun.org”;
const FROM = "imppower@sandboxc793347af04ae0a0771d8f541f9f8b.mailgun.org";
const TO = "derkley@gmail.com";
const SUBJECT = “Testing Mailgun”;
//const EMAIL_MESSAGE = “I am sending email through MailGun from an Electric Imp!”;

function data(EMAIL_MESSAGE) {
};

function send_mailgun_email() {
local url = format(“https://api:%s@%s%s/messages”, API_KEY, MAILGUN_URL, MAILGUN_DOMAIN);
local headers = {“Content-Type”: “application/x-www-form-urlencoded”};
local body = http.urlencode({from=FROM, to=TO, subject=SUBJECT, text=EMAIL_MESSAGE});
http.post(url,headers,body).sendasync(function(res) {
if (res.statuscode != 200) {
server.error("Mailgun error: " + res.statuscode + " => " + res.body);
} else {
try {
server.log(res.body);
} catch (e) {
server.error("Mailgun error: " + e)
}
}
})
}

// Use this with example device code
// Uncomment the three lines below
device.on(“mailgun”, function(data) {
send_mailgun_email();
});
`

This is working to the extent that the agent is able to send the email, but the EMAIL_MESSAGE shows up as “(nul :(nil))”.
I remarked out the line in the agent “//const EMAIL_MESSAGE = “I am sending email through MailGun from an Electric Imp!”;” because I don’t want that to be static text. I want to use the variable “EMAIL_MESSAGE” to contain the temperature value.

What I don’t understand is what to do at the “device.on” statement in my agent.
How do I assign the passed value to the “EMAIL_MESSAGE” variable??
I have seen several different versions of the “device.on” statement,

//device.on("event", function(data) { // send_mailgun_email(); //});

device.on("motionDetected", motionOnDevice);

I need help in understanding the various things that one can do with the “device.on” statement.
and exactly how to implement them.

thank you in advance for your time.
dwight

send_mailgun_email(data);
function send_mailgun_email(data) {
local body = http.urlencode({from=FROM, to=TO, subject=SUBJECT, text="Anything" + data});

this comment/question moved to a new thread…