preface

When I was doing my graduation project, I needed a function to send emails to users at the time of registration.

The SMTP service needs to be configured

I take QQ mailbox as an example:

Settings -> AccountfindPOP3 / IMAP/SMTP/Exchange/CardDAV/CalDAV service

Make sure you remember your license code. You’ll need it later.

The node to introduce nodemailer

First, you need to download the plugin

$ npm install nodemailer --save
Copy the code

Configuration nodemailer

// Reference the send mail plug-in
const nodeMailer = require('nodemailer')
// Import the certificate file (get my authorization code and qq mailbox)
const credential = require('.. /config/credentials')

// Create a transport mode
const transporter = nodeMailer.createTransport({
  service: 'qq'.auth: {
    user: credential.qq.user,  
    pass: credential.qq.pass   // This requires your authorization code!!}})// Register to send emails to users
exports.emailSignUp = function (email, res) {
  // Send the content of the message
  let options = {
    from: '[email protected]'.// Here is your QQ email number to enable SMTP service
    to: email,  // This is the mailbox number entered in the front-end registration page
    subject: 'Thank you for registering at XXXX'.html: ' XXXX welcome to join us!   Click  '
  }

  // Send an email
  transporter.sendMail(options, function (err, msg) {
    if (err) {
      res.send(err)
      // console.log(err)
    } else {
      res.send('Email sent successfully! ')
      // console.log(' mailbox sent successfully ')}})}Copy the code

For example, if I visit /email, I get it

// Note that you need to download the Body-Parser plug-in to get the body content using POST

router.post('/mail'.(req,res) = >{
    let mail = req.body.mail
    // console.log(mail)
    emialserve.emailSignUp(mail,res)
    // res.send(mail)
  })
Copy the code

Front-end code request: (Uni-app framework used)

  uni.request({
          url: "http://10.200.120.197:3000/mail".data: {
		  mail:this.user
		  },
          method: 'POST'.success: (data) = > {
            console.log(data)
          }
        })
Copy the code