nthmail / test / send.js
all-in-one smtp+imap with minimal setup
git clone http://git.nthia.dev/nthmail

const assert = require('assert')
const Imap = require('imap')
const NthMail = require('../')
const { tmpdir } = require('os')
const path = require('path')
const fs = require('fs')
const net = require('net')
const { randomBytes } = require('crypto')

let dir = path.join(tmpdir(), randomBytes(4).toString('hex'))
fs.mkdirSync(path.join(dir,'inbox'), { recursive: true })
fs.mkdirSync(path.join(dir,'outbox'), { recursive: true })
fs.mkdirSync(path.join(dir,'unbox'), { recursive: true })

let nthmail = new NthMail({
  accounts: [
    {
      id: 0,
      inbox: path.join(dir, 'inbox'),
      outbox: path.join(dir, 'outbox'),
      unbox: path.join(dir, 'unbox'),
      names: ['test'],
    }
  ],
  logins: [
    {
      type: 'scrypt',
      username: 'hi',
      salt: 'aed5c7437c31a507f36bd4a402081092',
      key: 'efb61e7b8dcf99044509e4704e8577bc850fc462df0ff695a4df22945c530e141fc'
        + '3a59c1bd381859eaa1a3de512aad1bc1286cb1f5c8665a8ecaab9a1baf14b',
      account: 0,
    },
  ],
  domains: ['localhost'],
  createServer: require('net').createServer,
})
nthmail.listen({ imap: [0], smtp: [0] }, (err,servers) => {
  let s = net.connect(servers.smtp.clear[0].address().port, 'localhost')
  s.end(`
    ehlo 0xff
    mail from: hi@0xff
    rcpt-to: test@localhost
    data
    subject: what

    ok.
    .
    quit
  `.trim().replace(/^\s+/mg,'') + '\r\n')
  s.resume()
  s.once('end', () => {
    let imap = new Imap({
      user: 'hi',
      password: 'hunter2',
      host: 'localhost',
      port: servers.imap.clear[0].address().port,
      tls: false,
    })
    imap.once('ready', () => {
      imap.openBox('INBOX', true, (err,box) => {
        let f = imap.seq.fetch('1:1', {
          bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
          struct: true,
        })
        f.on('message', (msg, seqnum) => {
          console.log('msg=',msg)
        })
        //imap.end()
        //nthmail.close()
      })
    })
    imap.connect()
  })
})