-
Notifications
You must be signed in to change notification settings - Fork 21
5. Send Email
leonardo Rico edited this page Dec 3, 2017
·
3 revisions
install the following npm packages
npm install nodemailer nodemailer-smtp-transport --save
Add in src/config/index.js and src/config/production.js
nodemailer: {
host: 'hostexample',
secure: true,
port: 465,
auth: {
user: '[email protected]',
pass: 'examplePassword'
}
}
Create a folder called nodemailer in src/lib/, and inside it create an index.js file with the following code:
import * as nodemailer from 'nodemailer';
import smtpTransport from 'nodemailer-smtp-transport';
import config from '../../config';
// create reusable transporter object using the default SMTP transport
const transporter = nodemailer.createTransport({ service: 'gmail', auth: config.nodemailer.auth });
// const transporter = nodemailer.createTransport(smtpTransport(config.nodemailer));
// function send email
export async function send(message) {
let r = await transporter.sendMail(message);
await transporter.close();
return r;
}
Use the function to send email from a controller, example from src/api/exampleSendEmail.js
import { result, error } from 'express-easy-helper';
import { send } from '../../lib/nodemailer';
// Send a email
export function index(req, res) {
// Options
let options = {
from: '"Your-app-name 👻" <[email protected]>', // sender address
to: `${req.swagger.params.email.value}`, // list of receivers -> [email protected], [email protected], ...
subject: 'Welcome ✔', // Subject line
text: 'Hello world!', // plain text body
html: '<h1>Hello World!</h1>' // html body
};
// Send
send(options).then(result(res)).catch(error(res));
}
Create router in swagger src/api/swagger/exampleSendEmail.yaml
/api/email/send:
x-swagger-router-controller: exampleSendEmail
# index
get:
operationId: index
tags:
- Examples
summary: Send a email
description: Send a email
parameters:
- name: email
description: receiver
in: query
required: true
type: string
responses:
200:
description: Success
500:
description: Error
Nodetomic Api Swagger