每日情话之跟着大佬学node邮件服务器

node-mail-service

基于node的邮件服务
定时每日给女友们发送情话嘿嘿嘿
就是照着大佬学习的
Github项目地址html

启动服务

  1. git clone 本项目
  2. yarn install 或 npm install
  3. yarn serve 或 npm run serve

建立你的邮件服务

  1. 找一个你喜欢的文件夹执行 yarn init 建立一个package.json
  2. yarn add nodemailer axios node-schedule 下载要用到的三个依赖:获取情话、邮件服务、定时任务
  3. 去你的邮箱开启邮件服务并获取受权码
  4. 建立index并依据本项目实现邮件服务
  5. node index 或 yarn serve 启动服务;终端依次显示爱心启动、你的情话、即将发出、发送成功|发送失败

配置邮箱服务

以163邮箱为例node

function sendMail(text, title = "亲爱的小宝贝") {
  const user = "你的163邮箱@163.com"; // 用163邮件服务就使用你的163邮箱,用qq邮件服务就用qq邮箱
  const pass = "你的受权码"; // 邮箱受权码,见下①
  const to = "对方的邮箱@qq.com"; // 对方的邮箱,任意邮箱
  const transporter = nodemailer.createTransport({
    service: '163',
    host: "smtp.163.com",
    port: 994, // 不一样的邮箱端口号不一样,见下经常使用邮箱服务器地址及端口②
    secure: true,
    auth: {
      user: user, // 用户帐号
      pass: pass, //受权码
    },
  });
  console.log('即将发出');
  transporter.sendMail({
    from: user,
    to: to,
    subject: title,
    text: text,
  }).then(res => {
    console.log('发送成功:', res);
  }).catch(err => {
    console.log('发送失败:', err);
  });
}

注:
① 进入你的邮箱,找到左上角帐号后面的设置,选择POP3/SMTP/IMAP设置,开启IMAP/SMTP服务、POP3/SMTP服务发个短信便可,
短信发完上面会显示你的受权码,163邮箱只显示一次,注意保存。
其余邮箱步骤大体相同。ios

经常使用邮箱服务器地址及端口git

常见问题

  1. 163有限不能定制邮件的from和to
transporter.sendMail({
    from: user, // 163若是from和auth中的user不一致会发送失败,而qq邮箱能够加定制话语`你的爱人${user}`
    to: to,
    subject: title,
    text: text,
  })

上传到服务器

  1. 选一个你喜欢的服务器
  2. 选一个你喜欢的xshell或其余什么玩意
  3. 选一个你喜欢的pm2下载姿式
  4. 选一个你喜欢的pm2运行node index

Github项目地址github

相关文章
相关标签/搜索