masohi-mail 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/bajo/.alias +1 -0
- package/bajo/.dependencies +1 -0
- package/bajo/config.json +3 -0
- package/bajo/init.js +27 -0
- package/bajo/method/format-message.js +31 -0
- package/bajo/start.js +9 -0
- package/lib/conn/hostinger.js +16 -0
- package/lib/conn/smtp.js +11 -0
- package/lib/send.js +12 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ardhi Lukianto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# masohi-mail
|
package/bajo/.alias
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mail
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
masohi
|
package/bajo/config.json
ADDED
package/bajo/init.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import smtp from '../lib/conn/smtp.js'
|
|
2
|
+
import hostinger from '../lib/conn/hostinger.js'
|
|
3
|
+
|
|
4
|
+
const handlers = {
|
|
5
|
+
smtp,
|
|
6
|
+
hostinger
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async function handler ({ item }) {
|
|
10
|
+
const { omit, keys } = this.app.bajo.lib._
|
|
11
|
+
item.type = item.type ?? 'smtp'
|
|
12
|
+
const types = keys(handlers)
|
|
13
|
+
if (!types.includes(item.type)) throw this.error('invalidConnType%s', item.type)
|
|
14
|
+
await handlers[item.type].call(this, { item })
|
|
15
|
+
const result = {
|
|
16
|
+
name: item.name,
|
|
17
|
+
options: omit(item, ['type', 'name'])
|
|
18
|
+
}
|
|
19
|
+
return result
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function init () {
|
|
23
|
+
const { buildCollections } = this.app.bajo
|
|
24
|
+
this.connections = await buildCollections({ ns: this.name, handler, container: 'connections' })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default init
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// based on: https://gist.github.com/fernandosavio/ff4285f772041d2fb102ff4bece62c20
|
|
2
|
+
function stripTags (html, allowed) {
|
|
3
|
+
allowed = allowed.trim()
|
|
4
|
+
if (allowed) {
|
|
5
|
+
allowed = allowed.split(/\s+/).map(tag => {
|
|
6
|
+
return '/?' + tag
|
|
7
|
+
})
|
|
8
|
+
allowed = '(?!' + allowed.join('|') + ')'
|
|
9
|
+
}
|
|
10
|
+
return html.replace(new RegExp('(<' + allowed + '.*?>)', 'gi'), '')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async function autoFormat (message) {
|
|
14
|
+
const { find } = this.app.bajo.lib._
|
|
15
|
+
const handler = find(this.app.bajo.configHandlers, { ext: '.yml' })
|
|
16
|
+
if (handler && handler.writeHandler) return await handler.writeHandler(message, true)
|
|
17
|
+
return JSON.stringify(message, null, 4)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function formatMessage (message, options = {}) {
|
|
21
|
+
const { isPlainObject } = this.app.bajo.lib._
|
|
22
|
+
const mpa = this.app.waibuMpa
|
|
23
|
+
const stripper = mpa ? mpa.stripHtmlTags.bind(mpa) : stripTags
|
|
24
|
+
if (!isPlainObject(message)) return { text: stripper(message) }
|
|
25
|
+
let html = await autoFormat.call(this, message)
|
|
26
|
+
if (!(mpa && options.tpl && options.req)) return { text: stripper(html), html }
|
|
27
|
+
html = await mpa.render(options.tpl, message, options)
|
|
28
|
+
return { text: stripper(html), html }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default formatMessage
|
package/bajo/start.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
async function hostinger ({ item }) {
|
|
2
|
+
const { has } = this.app.bajo.lib._
|
|
3
|
+
item.host = 'smtp.hostinger.com'
|
|
4
|
+
item.port = 587
|
|
5
|
+
item.secure = false
|
|
6
|
+
if (!has(item, 'user')) throw this.error('isRequired%s', 'user')
|
|
7
|
+
if (!has(item, 'pass')) throw this.error('isRequired%s', 'pass')
|
|
8
|
+
item.auth = {
|
|
9
|
+
user: item.user,
|
|
10
|
+
pass: item.pass
|
|
11
|
+
}
|
|
12
|
+
delete item.user
|
|
13
|
+
delete item.pass
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default hostinger
|
package/lib/conn/smtp.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
async function smtp ({ item }) {
|
|
2
|
+
const { has } = this.app.bajo.lib._
|
|
3
|
+
if (!has(item, 'host')) throw this.error('isRequired%s', 'host')
|
|
4
|
+
item.port = item.port ?? 587
|
|
5
|
+
item.secure = item.secure ?? false
|
|
6
|
+
item.auth = item.auth ?? {}
|
|
7
|
+
if (!has(item.auth, 'user')) throw this.error('isRequired%s', 'user')
|
|
8
|
+
if (!has(item.auth, 'pass')) throw this.error('isRequired%s', 'pass')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default smtp
|
package/lib/send.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
async function send ({ to, from, subject, message, conn, options = {} }) {
|
|
2
|
+
const { find } = this.app.bajo.lib._
|
|
3
|
+
const c = find(this.connections, { name: conn })
|
|
4
|
+
if (!c) throw this.error('notFound%s%s', this.print.write('connection'), `${conn}@masohiMail`)
|
|
5
|
+
from = c.options.auth.user // can't change from if using smtp
|
|
6
|
+
const { text, html } = await this.formatMessage(message, options)
|
|
7
|
+
if (options.subject) subject = options.subject
|
|
8
|
+
const resp = await c.instance.sendMail({ from, to, subject, text, html })
|
|
9
|
+
return resp
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default send
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "masohi-mail",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Mail Handler for Masohi Messaging",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/ardhi/masohi-mail.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"mail",
|
|
16
|
+
"messaging",
|
|
17
|
+
"pubsub",
|
|
18
|
+
"event",
|
|
19
|
+
"bajo",
|
|
20
|
+
"framework"
|
|
21
|
+
],
|
|
22
|
+
"author": "Ardhi Lukianto <ardhi@lukianto.com>",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/ardhi/masohi-mail/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/ardhi/masohi-mail#readme",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"nodemailer": "^6.10.0"
|
|
30
|
+
}
|
|
31
|
+
}
|