1
0
mirror of https://github.com/microsoft/TypeScript-Node-Starter.git synced 2026-03-22 11:17:38 +00:00
Files
TypeScript-Node-Starter/src/controllers/contact.ts
xiangxiang 9ed0760ae9 fit error TS2559: Type '4' has no properties in common with type 'MinMaxOptions'.
fix err TypeScript-Node-Starter/src/controllers/api.ts[6, 9]: missing whitespace, 8 files
fix bugs of issue 15
2017-07-19 20:36:10 +08:00

54 lines
1.3 KiB
TypeScript

import * as nodemailer from "nodemailer";
import { Request, Response } from "express";
const transporter = nodemailer.createTransport({
service: "SendGrid",
auth: {
user: process.env.SENDGRID_USER,
pass: process.env.SENDGRID_PASSWORD
}
});
/**
* GET /contact
* Contact form page.
*/
export let getContact = (req: Request, res: Response) => {
res.render("contact", {
title: "Contact"
});
};
/**
* POST /contact
* Send a contact form via Nodemailer.
*/
export let postContact = (req: Request, res: Response) => {
req.assert("name", "Name cannot be blank").notEmpty();
req.assert("email", "Email is not valid").isEmail();
req.assert("message", "Message cannot be blank").notEmpty();
const errors = req.validationErrors();
if (errors) {
req.flash("errors", errors);
return res.redirect("/contact");
}
const mailOptions = {
to: "your@email.com",
from: `${req.body.name} <${req.body.email}>`,
subject: "Contact Form",
text: req.body.message
};
transporter.sendMail(mailOptions, (err) => {
if (err) {
req.flash("errors", { msg: err.message });
return res.redirect("/contact");
}
req.flash("success", { msg: "Email has been sent successfully!" });
res.redirect("/contact");
});
};