-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.js
66 lines (52 loc) · 1.75 KB
/
send.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { config as dotenv } from 'dotenv'
import pc from 'picocolors'
import nodemailer from 'nodemailer'
import path from 'path'
import fs from 'fs'
import { getPackageInfo } from 'vituum/utils/common.js'
const { name, version } = getPackageInfo(import.meta.url)
dotenv()
const send = async (userOptions = {}) => {
console.info(`${pc.cyan(`${name} v${version}`)} ${pc.green('sending test email...')}`)
if (!userOptions.to) {
console.info(`${pc.cyan(`${name} v${version}`)} ${pc.red('recipient not defined')}`)
return
}
if (!userOptions.user || !userOptions.host || !userOptions.pass) {
console.info(`${pc.cyan(`${name} v${version}`)} ${pc.red('SMTP credentials not defined')}`)
return
}
let subject = 'Vituum Email'
let html = userOptions.content
const transport = nodemailer.createTransport({
host: userOptions.host,
port: 465,
auth: {
user: userOptions.user,
pass: userOptions.pass
}
})
if (userOptions.filename) {
const file = path.resolve(process.cwd(), userOptions.filename)
subject = path.basename(file)
if (!userOptions.content) {
html = fs.readFileSync(file).toString()
}
}
if (!userOptions.content) {
console.info(`${pc.cyan(`${name} v${version}`)} ${pc.red('no content to send')}`)
return
}
await transport.sendMail({
from: userOptions.from,
to: userOptions.to,
subject,
html
}, (error, info) => {
if (error) {
return console.error(pc.red(error))
}
console.info(`${pc.cyan(`${name} v${version}`)} ${pc.green('test email sent')} ${pc.gray(info.messageId)}`)
})
}
export default send