-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.js
More file actions
75 lines (59 loc) · 1.97 KB
/
send.js
File metadata and controls
75 lines (59 loc) · 1.97 KB
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
67
68
69
70
71
72
73
74
75
import { styleText } from 'node:util'
import nodemailer from 'nodemailer'
import path from 'path'
import fs from 'fs'
import { getPackageInfo } from 'vituum/utils/common.js'
import process from 'node:process'
const { name, version } = getPackageInfo(import.meta.url)
const envPath = path.resolve(process.cwd(), '.env')
const envLocalPath = path.resolve(process.cwd(), '.env.local')
if (fs.existsSync(envPath)) {
process.loadEnvFile(envPath)
}
if (fs.existsSync(envLocalPath)) {
process.loadEnvFile(envLocalPath)
}
const send = async (userOptions = {}) => {
console.info(`${styleText('cyan', `${name} v${version}`)} ${styleText('green', 'sending test email...')}`)
if (!userOptions.to) {
console.info(`${styleText('cyan', `${name} v${version}`)} ${styleText('red', 'recipient not defined')}`)
return
}
if (!userOptions.user || !userOptions.host || !userOptions.pass) {
console.info(`${styleText('cyan', `${name} v${version}`)} ${styleText('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(`${styleText('cyan', `${name} v${version}`)} ${styleText('red', 'no content to send')}`)
return
}
transport.sendMail({
from: userOptions.from,
to: userOptions.to,
subject,
html,
}, (error, info) => {
if (error) {
return console.error(styleText('red', error.toString()))
}
console.info(`${styleText('cyan', `${name} v${version}`)} ${styleText('green', 'test email sent')} ${styleText('gray', info.messageId)}`)
})
}
export default send