Kavorka
Kavorka

Reputation: 376

Creating Populated email from Javascript

I want to start off by saying I have no idea what I am doing with HTML and Javascript but I am trying to learn. What I am creating will not be hosted by any server , it is more of a HTML web form(I think that is what i would call it) for employees to fill out and create a standardized email. I have 97% of it working but need a little help with the last part. Below is the Javascript that works:

function populateEmail() { 
    
    let bl = document.getElementById("blurb").value;
    let a = document.getElementById("reg").value;
    let b = document.getElementById("lvl").value;
    let c = document.getElementById("node").value;
    let i = document.getElementById("cust").value;

    let d = document.getElementById("rea").value;
    let e = document.getElementById("ma").value;
    let f = document.getElementById("start_dt").value.replace("T", " ");

    let g = document.getElementById("poc").value;
    let h = document.getElementById("appr").value;

    let m_to = "DL-ListOne; DL-ListTwo; DL-ListThree"
    let m_cc = "DL-ListFour; DL-ListFive;"
    
    let today = new Date();
    let dd = String(today.getDate()).padStart(2, '0');
    let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    let yyyy = today.getFullYear();
    today = mm + '/' + dd + '/' + yyyy;
    
    let notify = document.getElementById("notify_lvl").value;
    
    if (notify == "Initial"){
        let deap = "Activation ";
    }
    else if (notify == "Update"){
        let deap = "Update ";
    }
    else{
        let deap = "De-Activation ";
    }

    document.location.href = "mailto:" + encodeURIComponent(m_to) + "?cc=" + encodeURIComponent(m_cc)
    + " &subject=DEAP " + encodeURIComponent(b) + ": Any Region " + today
    + " "
    + "&body=" 
    + "%0D%0A%0D%0A"
    + encodeURIComponent(bl) + "%0D%0A%0D%0A"
    + "Name: " + encodeURIComponent(a) + "%0D%0A"
    + "Activation – (" + encodeURIComponent(b)
    + "Current Impact = " + encodeURIComponent(c) + " modules, " + encodeURIComponent(i) + " customers) %0D%0A"
    + "Reason: " + encodeURIComponent(d) + "%0D%0A"
    + "Event Geographical Area: " + encodeURIComponent(e) + "%0D%0A"
    + "Event Start: " + encodeURIComponent(f) + "%0D%0A"
    + "POC: " + encodeURIComponent(g) + "%0D%0A"
    + "Approved by: " + encodeURIComponent(h) + "%0D%0A"

}

Now when I try and add the Variable deap to the subject line it stops creating the email. Here are the different ways I have tried to add it.

+ " &subject=DEAP " + deap + encodeURIComponent(b) + ": NE Region " + today

+ " &subject=DEAP " + encodeURIComponent(deap) + encodeURIComponent(b) + ": NE Region " + today

then I thought that maybe I had to add some text or space in there to have it take affect so I tried adding + " " + after the variable deap

I am trying to keep the post to a minimum, if you need my ugly looking HTML I will be happy to post it but I am still trying to figure out how to load div from Javascript so my code isn't DRY.

Thank you in advance for your time

Upvotes: 0

Views: 37

Answers (1)

Dan Knights
Dan Knights

Reputation: 8368

It's because you're declaring deap inside your if statements:

if ('notify' == "Initial") {
  let deap = "Activation ";
} else if ('notify' == "Update") {
  let deap = "Update ";
} else {
  let deap = "De-Activation ";
}

console.log(deap);

If you declare it outside and reassign it inside your if blocks, it should work:

let deap;

if ('notify' == "Initial") {
  deap = "Activation ";
} else if ('notify' == "Update") {
  deap = "Update ";
} else {
  deap = "De-Activation ";
}

console.log(deap)

Upvotes: 2

Related Questions