home ]
rawfeed
A raw Jekyll theme for minimalists
rawfeed
rawfeed - Verso
rawfeed
rawfeed - Verso
menu » weblog . contact . resume . 

Warning: Email form disabled

To use the email submission form, you need to:

1 - Copy the script below and implement it in Google Apps Script:

Note1: Don't forget to put your gmail in the script.

Note2: Without editing the script in Google Apps Script, you need to deploy it again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// IMPORTANT! You must put your gmail email here.
const TO_ADDRESS = "YOUR EMAIL GMAIL";

// Get the secret key from the Script Properties
const RECAPTCHA_SECRET_KEY = PropertiesService.getScriptProperties().getProperty('RECAPTCHA_SECRET_KEY');

// Function to validate the reCAPTCHA token
function validateRecaptcha(token) {
  if (!token) {
    throw new Error("Missing reCAPTCHA token.");
  }
  const url = "https://www.google.com/recaptcha/api/siteverify";
  const payload = {
    secret: RECAPTCHA_SECRET_KEY,
    response: token
  };

  const response = UrlFetchApp.fetch(url, {
    method: "post",
    payload: payload
  });

  const result = JSON.parse(response.getContentText());

  if (!result.success) {
    throw new Error("reCAPTCHA verification failed: " + (result['error-codes'] || 'Unknown error.'));
  }

  return true;
}


function doPost(e) {
  try {
    const data = JSON.parse(e.postData.contents);

    // 1. Validate the reCAPTCHA token first!
    validateRecaptcha(data['g-recaptcha-response']);

    // 2. If validation passed, continue with the rest of the code
    const { name, email, message } = data;

    if (!name || !email || !message) {
      throw new Error("Missing form data.");
    }

    const subject = "New website message " + name;
    const htmlBody = `
      <p>You have received a new message from your website.:</p><hr>
      <p><b>Name:</b> ${name}</p>
      <p><b>Email:</b> <a href="mailto:${email}">${email}</a></p>
      <p><b>Message:</b></p>
      <p style="white-space: pre-wrap;">${message}</p><hr>
    `;

    MailApp.sendEmail({
      to: TO_ADDRESS,
      subject: subject,
      htmlBody: htmlBody,
      replyTo: email
    });

    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'success', 'message': 'Message sent!' }))
      .setMimeType(ContentService.MimeType.JSON);

  } catch (err) {
    Logger.log(err.toString());
    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'error', 'message': err.toString() }))
      .setMimeType(ContentService.MimeType.JSON);
  }
}

2 - Create a reCaptcha on Google and add the reCaptcha PRIVATE key to the Google Apps Script script property.

3 - Copy the reCaptcha PUBLIC key and the Google Apps Script URL and place them in _config.yml:

1
2
3
4
5
6
7
8
google:
  ###
  ###
  apps_script:
    url: "https://script.google.com/macros/s/BuD..."
  recaptcha:
    pubkey: "8Lci194rAAAAA70Sv..."