Nodemailer profile

Nodemailer is an easy-to-use Node.js mail sending component

Nodemailer.com

GitHub address: github.com/nodemailer/…

Nodemailer’s key features include:

  • Support Unicode encoding
  • Windows operating system is supported
  • Support HTML content and plain text content
  • Supporting attachments (sending large attachments)
  • Support for embedding images in HTML content
  • Supports SSL/STARTTLS secure email sending
  • Support for built-in transport methods and transport methods implemented by other plug-ins
  • Support for custom plug-ins to handle messages
  • XOAUTH2 login authentication is supported

Installation and use:

1. Download first

npm install nodemailer --save
Copy the code

2. The packagingsendEmail.js

Here I send mail for a simple encapsulation, use only need to pass parameters

const nodemailer = require('nodemailer'); // Import modules
let transporter = nodemailer.createTransport({
    / / node_modules/nodemailer/lib/well - known/services. The json view related configuration, if you use qq mailbox, just look at the qq email related configuration
	service: 'qq'.// Type qq mailbox
	port: 465.secure: true.// true for 465, false for other ports
	auth: {
		user: '[email protected]'.// Sender's email address
		pass: 'xxxx' // SMTP authorization code}});//pass is not the password of the email account but the STMP authorization code (it must be the STMP authorization code of the corresponding email)
// Email -- set -- Account --POP3/SMTP service -- enable -- get STMP authorization code

function sendMail(mail, code, call) {
	// The configuration item to send
	let mailOptions = {
		from: '"Express-demo" <[email protected]>'./ / the sender
		to: mail, // The recipient's mailbox. Multiple mailboxes are separated by commas
		subject: 'Welcome to "Express-Demo."./ / title
		text: 'Hello world? '.// Text content
		html: '< p > this is the "Express - demo" for details, please click: < / p > < a href = "https://www.jianshu.com/u/5cdc0352bf01" > click jump < / a >'.// Page content
		// attachments: [{// to send files
		// filename: 'index.html', // filename
		// path: './index.html' // File path
		/ /},
		// 	{
		// filename: 'sendemail.js ', // filename
		// Content: 'sendemail.js' // file path
		/ /}
		// ]
	};

	// Send function
	transporter.sendMail(mailOptions, (error, info) => {
		if (error) {
			call(false)}else {
			call(true) // Because it is asynchronous, all callback functions need to be notified of success results}}); }module.exports = {
	sendMail
}

Copy the code

3. Usage

const express = require('express')
const app = express()
const email = require('./sendEmail.js'); // Introduce the wrapped function
const bodyParser = require('body-parser')
const cors = require('cors'); // Resolve cross-domain issues
const styles = {
	'bold': ['\x1B[1m'.'\x1B[22m'].'italic': ['\x1B[3m'.'\x1B[23m'].'underline': ['\x1B[4m'.'\x1B[24m'].'inverse': ['\x1B[7m'.'\x1B[27m'].'strikethrough': ['\x1B[9m'.'\x1B[29m'].'white': ['\x1B[37m'.'\x1B[39m'].'grey': ['\x1B[90m'.'\x1B[39m'].'black': ['\x1B[30m'.'\x1B[39m'].'blue': ['\x1B[34m'.'\x1B[39m'].'cyan': ['\x1B[36m'.'\x1B[39m'].'green': ['\x1B[32m'.'\x1B[39m'].'magenta': ['\x1B[35m'.'\x1B[39m'].'red': ['\x1B[31m'.'\x1B[39m'].'yellow': ['\x1B[33m'.'\x1B[39m'].'whiteBG': ['\x1B[47m'.'\x1B[49m'].'greyBG': ['\x1B[49;5;8m'.'\x1B[49m'].'blackBG': ['\x1B[40m'.'\x1B[49m'].'blueBG': ['\x1B[44m'.'\x1B[49m'].'cyanBG': ['\x1B[46m'.'\x1B[49m'].'greenBG': ['\x1B[42m'.'\x1B[49m'].'magentaBG': ['\x1B[45m'.'\x1B[49m'].'redBG': ['\x1B[41m'.'\x1B[49m'].'yellowBG': ['\x1B[43m'.'\x1B[49m']}; app.use(bodyParser.urlencoded({extended: false
}))
app.use(bodyParser.json())
app.use(cors());
app.use(express.static(__dirname + '/'));// Static directory
const check = {} // Declare an object cache mailbox and a verification code
app.post('/email'.function(req, res, next) {
	console.log(req.body)
	const mail = req.body.email
	const data = {
		rst: true.data: "".msg: ""
	}
	if(! mail) {return res.send('Parameter error')}// If the email is in error or empty
	const code = parseInt(Math.random(0.1) * 10000) // Generate a random verification code
	check[mail] = code
	// Send an email
	email.sendMail(mail, code, (state) => {
		if (state) {
			return res.send("Sent successfully")}else {
			return res.send("Failure")}})});/* GET home page. */

app.listen(3001, () = > {console.log('\x1B[33m%s\x1b[0m'."Service started successfully"); //yellow

})

Copy the code

The source address

Reference Link 1

Reference Link 2

website