node基于nodemailer发送邮件

  • 安装依赖

    1
    npm install nodemailer
  • 发送gmail邮件, 请点击我先设置允许安全性较低应用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    const nodemailer = require('nodemailer');

    const transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    auth: {
    user: 'my@gmail.com',
    pass: 'my password'
    }
    });

    const HelperOption = {
    from: '"Navgurukul"<my@gmail.com>',
    to: 'you@gmail.cn',
    subject: 'this is testing',
    text: 'Hi Gyus! I am just checking my code is .'
    };

    transporter.sendMail(HelperOption, (error, info) =>{
    if(error){
    return console.log(error);
    } else {
    console.log("The message was sent ");
    console.log(info);
    }
    });