user4826496
user4826496

Reputation:

How to format a text in a mailto function

I have two arrays and an object. One array conatins product codes, and another contains the quantities thereof. For example, the first quantity in the quantities array is the quantity of the product code that is the first one in the product code array. I also have an object that contains customer data. It would look something like this:

customer={
name:' firstname lastname',
email: '[email protected]',
company: "company name",
phone_number: 'phone number',
}

the arrays would look like this:

product_codes=[code_1; code_2; code_3];
quantities=[23, 56, 45];

Say that all of this is being mailed to [email protected].

I am familiar with the basics of the mailto function, but I would like to know if there is a way to format the body of the email so that it looks something like this:

...................................

Name: customer.name

email: customer.email

company name: customer.company

phone number: customer.phone_number

product code 1: corresponding quantity

product code 2: corresponding quantity

product code 3: corresponding quantity

...............................................

I would also like to be able to add any other given codes and quantities, as I am not sure of how many there will be. Is this even possible? If so, how? Please explain so that I can not only use it, but also understand how it works. Thanks!

If I'm not being clear enough, please let me know so I can edit it for greater clarity.

Upvotes: 1

Views: 958

Answers (3)

Felipe Brahm
Felipe Brahm

Reputation: 3160

var sendEmail = function() {

  var customer, body, quantities, product_codes;    

  customer = {
    name: 'First Last',
    email: '[email protected]',
    company: 'Company',
    phone_number: 'phone number',
  }

  body =  'Name: '+ customer.name;
  body += '\nEmail: '+ customer.email;
  body += '\nCompany: '+ customer.company;
  body += '\nPhone Number: '+ customer.phone_number;

  product_codes = ['code_1', 'code_2', 'code_3'];
  quantities = [23, 56, 45];

  for(var i = 0; i < product_codes.length; i += 1) {
    body += '\nProduct Code '+ product_codes[i] +': '+ quantities[i];
  }

  subject = 'Your Subject';

  window.location = 'mailto:[email protected]?body='+ encodeURIComponent(body) +'&subject='+ encodeURIComponent(subject);

};

// execute this function when the user clicks the #send-email button
var button = document.getElementById('send-email');
button.addEventListener('click', sendEmail);

This is how the email will look like

Upvotes: 2

rbtLong
rbtLong

Reputation: 1572

It sounds to me like you want to construct the body of a message. If this is the case, you can create a function that builds the body of your message by taking in the 3 objects you mentioned: customer, codes, and quantity.

For example, you can do something like

function buildBody(cust, codes, quant){
    var body = "";

    body += "Name: " + cust.name + "\n";
    body += "Email: " + cust.email + "\n";
    body += "Company Name: " + cust.companyname + "\n";

    for(var i=0; i<codes.length; ++i)
        body += "Product Code " + codes[i] + ": " quant[i] + "\n";

    return body;
}

I have not tested this code, but hopefully you get the idea.

Upvotes: 0

Marc
Marc

Reputation: 11633

I would build the string in a function:

HTML:

<a href="#" id="thelink">Click to Email</a>

JAVASCRIPT:

//stuff you specified...
var customer={
  name:' firstname lastname',
  email: '[email protected]',
  company: "company name",
  phone_number: 'phone number',
}
var product_codes=['alpha', 'beta', 'gamma'];
var quantities=[23, 56, 45];

/* Assign a click action onto the link */
var yourLink = document.getElementById("thelink");
yourLink.onclick = function() {
   var elf = "%0A"; //Encoded Line Feed
   mailtoBody = "Name: " + customer.name + elf
              + "Email: " + customer.email + elf
              + "Company Name: " + customer.company + elf
              + "Phone Number: " + customer.phone_number + elf;
   for (var i=0; i < product_codes.length; i++) {
        mailtoBody += product_codes[i] + ": " + quantities[i] + elf;
   }
   location.href = "mailto:[email protected]?body=" + mailtoBody;
}

Here's a working example: http://jsbin.com/kigutuhiqa/edit?html,js,output

Upvotes: 2

Related Questions