Skip to content

Instantly share code, notes, and snippets.

@gaurangrshah
Created May 3, 2021 02:45
Show Gist options
  • Save gaurangrshah/c4f1d7ce80c402ebdae0266b592482ec to your computer and use it in GitHub Desktop.
Save gaurangrshah/c4f1d7ce80c402ebdae0266b592482ec to your computer and use it in GitHub Desktop.
/**
* A simple method of demonstrating how you can use the Next.js
* internal API for 'server-less' functions. We export an async function.
*
* We import the nodemailer package to handle SMTP transactional mail.
*
* In a production environment, you could create a .env.production
* to hold your secure details, e.g.,
* HOST, AUTH_USER, AUTH_PASS
*
*/
// https://blog.mailtrap.io/nodemailer-gmail/#Configuring_a_Gmail_account
import nodemailer from "nodemailer";
export default async (req, res) => {
console.log("🚀 ~ file: email.js ~ line 17 ~ req", req.body);
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
// host: "smtp.ethereal.email",
// port: 587,
// secure: false, // true for 465, false for other ports
service: "gmail",
auth: {
user: "[email protected]", // generated ethereal user
pass: process.env.EMAIL_PASS, // generated ethereal password
// user: process.env.SMTP_USER, // generated ethereal user
// pass: process.env.SMTP_PASS, // generated ethereal password
},
});
if (req.method === "POST") {
// ❌ disabled nodemailer email in favor of airtable automated email
let info = await transporter.sendMail({
from: '"Falcon Driving " <[email protected]>', // sender address
to: "[email protected]", // comma-separated list of receivers
subject: " 🦅 Falcon Driving School - New Driver Form", // Subject line
text: req.body, // plain text body
// html: "<b>Hello world?</b>", // html body
});
// We can also extract the form variables from our req.body
const { email = "", name = "" } = req.body;
res.json({
message: `
Ethereal Mail Message ID: ${info.messageId}
Email from form: ${email}
Name from form: ${name}
`,
});
} else {
res.status(200).json({ message: `Response from /api/emails.` });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment