如何在 mailto 函数中格式化文本

How to format a text in a mailto function

我有两个数组和一个对象。一个数组包含产品代码,另一个数组包含其数量。例如,数量数组中的第一个数量是产品代码数组中第一个产品代码的数量。我还有一个包含客户数据的对象。它看起来像这样:

customer={
name:' firstname lastname',
email: 'example@domain.com',
company: "company name",
phone_number: 'phone number',
}

数组看起来像这样:

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

假设所有这些都邮寄到 customersupport@example.com。

我熟悉 mailto 功能的基础知识,但我想知道是否有一种方法可以将电子邮件正文格式化为如下所示:

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

姓名:customer.name

电子邮件:customer.email

公司名称:customer.company

phone个数:customer.phone_number

产品代码1:对应数量

商品代码2:对应数量

商品代码3:对应数量

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

我还希望能够添加任何其他给定的代码和数量,因为我不确定会有多少。这可能吗?如果是这样,如何?请解释一下,这样我不仅可以使用它,还可以了解它是如何工作的。谢谢!

如果我说得不够清楚,请告诉我,以便我进行编辑以使其更加清晰。

我会在函数中构建字符串:

HTML:

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

JAVASCRIPT:

//stuff you specified...
var customer={
  name:' firstname lastname',
  email: 'example@domain.com',
  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:you@example.com?body=" + mailtoBody;
}

这是一个工作示例: http://jsbin.com/kigutuhiqa/edit?html,js,output

我觉得您想构建邮件正文。如果是这种情况,您可以创建一个函数,通过接收您提到的 3 个对象来构建消息正文:客户、代码和数量。

例如,您可以这样做

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;
}

我没有测试过这段代码,但希望你能理解。

var sendEmail = function() {

  var customer, body, quantities, product_codes;    

  customer = {
    name: 'First Last',
    email: 'example@example.com',
    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:customersupport@example.com?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);