Sending Emails with SendGrid
What You'll Need
Backbench account - Sign Up, the personal account will always remain free to use.
A SendGrid account, sign up for free to send up to 40,000 emails for the first 30 days.
Prerequisites for the app
Sign In to Backbench account.
Select +, in the upper right corner to create a Bench. For example, say "bench_one" and select CREATE or hit Enter.
Frontend
Select +, in the upper right corner of file manager to create a html file. For example, say "index.html" and select CREATE or hit Enter.
Copy and paste the code module from below.
Select save.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>mail service</title>
</head>
<body>
<div id="contact">
<h1>Send an email</h1>
<form action="/sg" method="post">
<fieldset>
<label for="from">From:</label>
<input type="text" id="from" name="from" placeholder="From email_id" />
<label for="Toemail">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email address" />
<label for="subject">subject:</label>
<input type="subject" id="subject" name="subject" placeholder="subject" />
<label for="message">Message:</label>
<textarea id="message" placeholder="Message" name="message"></textarea>
<input type="submit" value="Send message" />
</fieldset>
</form>
</div>
</body>
</html>
Adding Dependencies
For nodejs only.
Create a module name package.json and paste following code.
{
"dependencies":{
"sendgrid":"1.3.1"
}
}
Backend
Select +, in the upper right corner to create a Module. For example, say "Send_email.js" and select CREATE or hit Enter.
Copy and paste the code module from below.
Replace an API key from SendGrid setting section (create a new api key using create button).
Select save.
var helper = require("sendgrid").mail;
module.exports.endpoint = function(req, cb) {
var fromEmail = new helper.Email(req.body.from);
var toEmail = new helper.Email(req.body.email);
var subject = req.body.subject;
var content = new helper.Content("text/plain", req.body.message);
var mail = new helper.Mail(fromEmail, subject, toEmail, content);
var sg = require("sendgrid")("YOUR_API_KEY");
var request = sg.emptyRequest({
method: "POST",
path: "/v3/mail/send",
body: mail.toJSON()
});
sg.API(request, function(error, response) {
if (error) {
console.log("Error response received");
}
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
cb(undefined, {
status: "sucessfully sent"
});
});
};
Last updated