Code Gửi Email (Đơn Đặt Hàng)

Code 1: mặc định chưa chỉnh sửa

 function guiBieuMau(e) {
    var email = "gmailcuaban@gmail.com";
    var subject = "Đơn đặt hàng";
    var s = SpreadsheetApp.getActiveSheet();
    var columns = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0];
    var message = "";
    for (var keys in columns) {
        var key = columns[keys];
        if (e.namedValues[key] && (e.namedValues[key] != "")) {
            message += key + ' :: ' + e.namedValues[key] + "\n\n";
        }
    }
    MailApp.sendEmail(email, subject, message);
}

Code 2: Chỉ gửi email tới một địa chỉ cố định (ví dụ: admin)

function sendFormResponse(e) {

  const config = {

    toEmail: "veoprotv@gmail.com", // Email cố định duy nhất nhận email

    subject: "Tư vấn Mua hàng",

    styles: {

      container: "font-family: Arial, sans-serif; max-width: 600px; margin: 20px auto; background-color: #f9f9f9; padding: 15px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);",

      header: "color: #1a73e8; margin: 0 0 15px; font-size: 22px; text-align: center;",

      content: "background-color: #fff; padding: 10px; border-radius: 5px; border: 1px solid #eee;",

      row: "display: flex; align-items: center; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px dashed #ddd;",

      label: "color: #1a73e8; font-weight: bold; font-size: 14px; min-width: 120px;",

      value: "color: #333; font-size: 14px; flex: 1; word-wrap: break-word;",

      footer: "color: #777; font-size: 11px; text-align: center; margin-top: 15px;"

    }

  };


  const rows = Object.entries(e.namedValues).filter(([_, value]) => value?.toString().trim() !== "").map(([key, value]) => `

      <div style="${config.styles.row}">

        <span style="${config.styles.label}">${key}:</span>

        <span style="${config.styles.value}">${value}</span>

      </div>

    `).join("");

  const message = `

    <html>

      <body style="${config.styles.container}">

        <h2 style="${config.styles.header}">${config.subject}</h2>

        <div style="${config.styles.content}">${rows}</div>

        <p style="${config.styles.footer}">Email được gửi từ hệ thống tự động - Vui lòng không trả lời trực tiếp</p>

      </body>

    </html>

  `;

  try {

    MailApp.sendEmail({

      to: config.toEmail, 

      subject: config.subject,

      htmlBody: message

    });

  } catch (error) {

    Logger.log(`Lỗi gửi email: ${error.message}`);

  }

}


Code 3: Gửi email tới hai địa chỉ: một email cố định (admin) và email do khách hàng điền trong biểu mẫu.

function sendFormResponse(e) {

  const config = {

    toEmail: "veoprotv@gmail.com",

    subject: "Tư vấn Mua hàng",

    styles: {

      container: "font-family: Arial, sans-serif; max-width: 600px; margin: 20px auto; background-color: #f9f9f9; padding: 15px; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);",

      header: "color: #1a73e8; margin: 0 0 15px; font-size: 22px; text-align: center;",

      content: "background-color: #fff; padding: 10px; border-radius: 5px; border: 1px solid #eee;",

      row: "display: flex; align-items: center; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px dashed #ddd;",

      label: "color: #1a73e8; font-weight: bold; font-size: 14px; min-width: 120px;",

      value: "color: #333; font-size: 14px; flex: 1; word-wrap: break-word;",

      footer: "color: #777; font-size: 11px; text-align: center; margin-top: 15px;"

    }

  };

  const bccEmail = e.namedValues["Email"]?.toString() || "";

  const rows = Object.entries(e.namedValues).filter(([_, value]) => value?.toString().trim() !== "").map(([key, value]) => `

      <div style="${config.styles.row}">

        <span style="${config.styles.label}">${key}:</span>

        <span style="${config.styles.value}">${value}</span>

      </div>

    `).join("");

  const message = `

    <html>

      <body style="${config.styles.container}">

        <h2 style="${config.styles.header}">${config.subject}</h2>

        <div style="${config.styles.content}">${rows}</div>

        <p style="${config.styles.footer}">Email được gửi từ hệ thống tự động - Vui lòng không trả lời trực tiếp</p>

      </body>

    </html>

  `;

  try {

    MailApp.sendEmail({

      to: config.toEmail,

      bcc: bccEmail,

      subject: config.subject,

      htmlBody: message

    });

  } catch (error) {

    Logger.log(`Lỗi gửi email: ${error.message}`);

  }

}