Skip to content

Instantly share code, notes, and snippets.

@clairefro
Last active February 25, 2026 22:05
Show Gist options
  • Select an option

  • Save clairefro/9b58a234ac88508f833b7c4afc32a075 to your computer and use it in GitHub Desktop.

Select an option

Save clairefro/9b58a234ac88508f833b7c4afc32a075 to your computer and use it in GitHub Desktop.
google apps script contact form submission handling
/* APPS SCRIPT */
function doPost(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("inqueries");
var name = e.parameter.name.trim();
var email = e.parameter.email;
var phone = e.parameter.phone || ""; // Optional, may be empty
var message = e.parameter.message;
sheet.appendRow([new Date(), name, email, phone, message]);
// Send email notification to sheet owner
var ownerEmail = "<OWNER EMAIL>";
var subject = "[Catering Inquiry] New Submission from " + name;
var ownerMessage = "Name: " + name + "\nEmail: " + email + "\nPhone: " + phone + "\nMessage: " + message;
MailApp.sendEmail(ownerEmail, subject, ownerMessage);
// Send confirmation email to the person inquiring
var firstName = name.split(" ")[0];
var userSubject = "Thank you for contacting Iovino's Catering!";
var phoneLineUser = phone ? "\nPhone: " + phone : "";
var userMessage = "Hi " + firstName + ",\n\nThank you for your inquiry! Here's what you sent:\n\n\"" + message + "\"" + phoneLineUser + "\n\nWe will get back to you soon.\n\nBest,\nIovino's Catering";
MailApp.sendEmail(email, userSubject, userMessage);
return ContentService
.createTextOutput(JSON.stringify(e.parameter))
.setMimeType(ContentService.MimeType.JSON);
}
function testDoPost() {
var e = {
parameter: {
name: "Test User",
email: "test@email.com",
phone: "555-555-5555",
message: "This is a test message."
}
};
var result = doPost(e);
Logger.log(result.getContent());
}
/* SUBMISSION LOGIC IN CLIENT */
// cannot do is:inline if using ViewTransitions (ClientRouter)
document.addEventListener("astro:page-load", () => {
document
.getElementById("contactForm")
?.addEventListener("submit", async function (e) {
e.preventDefault();
const form = e.target as HTMLFormElement;
const submitBtn = document.getElementById(
"contactSubmitBtn",
) as HTMLButtonElement;
const status = document.getElementById("formStatus");
if (!status || !submitBtn || !form) return;
const originalText = submitBtn.textContent;
submitBtn.disabled = true;
submitBtn.textContent = "Submitting...";
submitBtn.style.cursor = "not-allowed";
try {
const formData = new FormData(form);
const response = await fetch(
"<GOOGLE APP SCRIPT WEB APP URL>",
{
method: "POST",
body: formData,
// No headers!
},
);
if (response.ok) {
status.textContent = "Thank you! Your message was sent.";
form.reset();
} else {
status.textContent = "Error sending message. Please try again.";
}
} catch (err) {
status.textContent = "Error sending message. Please try again.";
} finally {
submitBtn.disabled = false;
submitBtn.textContent = originalText;
submitBtn.style.cursor = "pointer";
}
});
}); // astro:page-load
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment