@live-change/email-service 0.1.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.md +11 -0
- package/README.md +1 -0
- package/auth.js +42 -0
- package/clientEmailValidator.js +5 -0
- package/definition.js +9 -0
- package/emailValidator.js +5 -0
- package/index.js +47 -0
- package/package.json +31 -0
- package/render.js +98 -0
- package/send.js +133 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2019-2020 Michał Łaszczewski
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# email-service
|
package/auth.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const definition = require('./definition.js')
|
|
2
|
+
|
|
3
|
+
const User = definition.foreignModel('user', 'User')
|
|
4
|
+
|
|
5
|
+
const Email = definition.model({
|
|
6
|
+
name: 'Email',
|
|
7
|
+
userItem: {
|
|
8
|
+
userReadAccess: () => true
|
|
9
|
+
},
|
|
10
|
+
indexes: {
|
|
11
|
+
byEmail: {
|
|
12
|
+
property: 'email'
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
definition.trigger({
|
|
18
|
+
name: "checkNewEmail",
|
|
19
|
+
properties: {
|
|
20
|
+
email: {
|
|
21
|
+
type: String
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
async execute({ email }, context, emit) {
|
|
25
|
+
const emailData = await Email.get(email)
|
|
26
|
+
if(emailData) throw 'taken'
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
definition.trigger({
|
|
32
|
+
name: "createEmail",
|
|
33
|
+
properties: {
|
|
34
|
+
email: {
|
|
35
|
+
type: String
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
async execute(props, context, emit) {
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
module.exports = { Email }
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export default (settings) => (email) => {
|
|
2
|
+
if(!email || !email.trim()) return
|
|
3
|
+
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
4
|
+
if (!re.test(String(email).toLowerCase())) return "wrongEmail"
|
|
5
|
+
}
|
package/definition.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
module.exports = (settings) => (email) => {
|
|
2
|
+
if(!email || !email.trim()) return
|
|
3
|
+
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
4
|
+
if (!re.test(String(email).toLowerCase())) return "wrongEmail"
|
|
5
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const app = require("@live-change/framework").app()
|
|
2
|
+
|
|
3
|
+
const definition = require('./definition.js')
|
|
4
|
+
|
|
5
|
+
require('./send.js')
|
|
6
|
+
require('./auth.js')
|
|
7
|
+
|
|
8
|
+
definition.processor(function(service, app) {
|
|
9
|
+
service.validators.email = require('./emailValidator.js')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
definition.action({
|
|
13
|
+
name: "sendContactFormMail",
|
|
14
|
+
properties: {
|
|
15
|
+
from: { type: String, validation: ['nonEmpty'] },
|
|
16
|
+
name: { type: String, validation: ['nonEmpty']},
|
|
17
|
+
subject: { type: String, validation: ['nonEmpty'] },
|
|
18
|
+
text: { type: String, validation: ['nonEmpty'] },
|
|
19
|
+
html: { type: String },
|
|
20
|
+
},
|
|
21
|
+
async execute({ from, name, subject, text, html }, { client, service }, emit) {
|
|
22
|
+
if(!html) {
|
|
23
|
+
const encodedStr = text.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
|
|
24
|
+
return '&#'+i.charCodeAt(0)+';'
|
|
25
|
+
})
|
|
26
|
+
const multiline = encodedStr.replace(/\n/gi, /*'↵*/'<br>')
|
|
27
|
+
const withLinks = multiline.replace(
|
|
28
|
+
/(?![^<]*>|[^<>]*<\/)((https?:)\/\/[a-z0-9&#%=.\/?_,-]+)/gi, '<a href="$1" target="_blank">$1</a>')
|
|
29
|
+
html = withLinks
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
await service.trigger({
|
|
33
|
+
type:"sendEmailMessage",
|
|
34
|
+
email: {
|
|
35
|
+
from: `${name} <${process.env.CONTACT_FORM_FROM_EMAIL}>`,
|
|
36
|
+
to: `${ process.env.CONTACT_FORM_TARGET_NAME} <${process.env.CONTACT_FORM_TARGET_EMAIL}>`,
|
|
37
|
+
subject: subject,
|
|
38
|
+
text,
|
|
39
|
+
html,
|
|
40
|
+
replyTo: `${name} <${from}>`
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
module.exports = definition
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@live-change/email-service",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "NODE_ENV=test tape tests/*"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/live-change/email-service.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/live-change/email-service/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/live-change/email-service",
|
|
18
|
+
"author": {
|
|
19
|
+
"email": "michal@laszczewski.pl",
|
|
20
|
+
"name": "Michał Łaszczewski",
|
|
21
|
+
"url": "https://www.viamage.com/"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@live-change/framework": "^0.5.7",
|
|
25
|
+
"nodemailer": "^6.7.2",
|
|
26
|
+
"got": "^11.8.3",
|
|
27
|
+
"jsdom": "^18.1.1",
|
|
28
|
+
"inline-css": "3.0.0",
|
|
29
|
+
"html-to-text": "8.1.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/render.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const got = require('got')
|
|
2
|
+
const inlineCss = require('inline-css')
|
|
3
|
+
const { JSDOM } = require("jsdom")
|
|
4
|
+
const { convert: htmlToText } = require('html-to-text')
|
|
5
|
+
const path = require('path')
|
|
6
|
+
const { URL } = require('url')
|
|
7
|
+
|
|
8
|
+
const definition = require('./definition.js')
|
|
9
|
+
const config = definition.config
|
|
10
|
+
|
|
11
|
+
const baseUrl = `http://${config.ssrHost||process.env.SSR_HOST||'localhost'}`+
|
|
12
|
+
`:${config.ssrHost||process.env.SSR_PORT||'8001'}`
|
|
13
|
+
const publicDir = config.publicDir || 'front/public/'
|
|
14
|
+
|
|
15
|
+
function processElement(element, images) {
|
|
16
|
+
for(let i = 0; i < element.attributes.length; i++) {
|
|
17
|
+
const attribute = element.attributes[i]
|
|
18
|
+
const remove = attribute.nodeName == 'class' || attribute.nodeName.slice(0, 4) == 'data'
|
|
19
|
+
if(remove) {
|
|
20
|
+
element.removeAttribute(attribute.nodeName)
|
|
21
|
+
i--
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
for(let i = 0; i < element.children.length; i++) {
|
|
25
|
+
processElement(element.children[i], images)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const src = element.getAttribute('src')
|
|
29
|
+
if(src) {
|
|
30
|
+
let contentId = images.get(src)
|
|
31
|
+
if(!contentId) {
|
|
32
|
+
contentId = 'image-'+(images.size + 1)
|
|
33
|
+
images.set(src, contentId)
|
|
34
|
+
}
|
|
35
|
+
element.setAttribute('src', 'cid:' + contentId)
|
|
36
|
+
}
|
|
37
|
+
const backgroundImage = element?.style?.backgroundImage
|
|
38
|
+
if(backgroundImage) {
|
|
39
|
+
let contentId = images.get(backgroundImage)
|
|
40
|
+
if(!contentId) {
|
|
41
|
+
contentId = 'image-'+(images.size+1)
|
|
42
|
+
images.set(backgroundImage, contentId)
|
|
43
|
+
}
|
|
44
|
+
element.setAttribute('src', 'cid:' + contentId)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function renderEmail(data) {
|
|
49
|
+
const encodedData = encodeURIComponent(JSON.stringify(data))
|
|
50
|
+
const url = `${baseUrl}/_email/${data.action}/${data.contact}/${encodedData}`
|
|
51
|
+
console.log("RENDER EMAIL", data, "URL", url)
|
|
52
|
+
const response = await got(url)
|
|
53
|
+
let body = response.body
|
|
54
|
+
console.log("BASE URL", baseUrl)
|
|
55
|
+
body = await inlineCss(body, { url })
|
|
56
|
+
console.log("HTML", body)
|
|
57
|
+
const dom = new JSDOM(body)
|
|
58
|
+
const headers = JSON.parse(dom.window.document.querySelector('[data-headers]').textContent)
|
|
59
|
+
const messageElements = dom.window.document.querySelectorAll("[data-html],[data-text]")
|
|
60
|
+
const email = { ...headers }
|
|
61
|
+
const images = new Map()
|
|
62
|
+
for(let messageElement of messageElements) {
|
|
63
|
+
const toHtml = messageElement.getAttribute('data-html')
|
|
64
|
+
const toText = messageElement.getAttribute('data-text')
|
|
65
|
+
if(toHtml !== null) {
|
|
66
|
+
processElement(messageElement, images)
|
|
67
|
+
email.html = messageElement.outerHTML
|
|
68
|
+
}
|
|
69
|
+
if(toText !== null) {
|
|
70
|
+
email.text = htmlToText(messageElement.outerHTML)
|
|
71
|
+
if(messageElement.tagName == 'PRE') {
|
|
72
|
+
indentation = email.text.match(/^ */)[0]
|
|
73
|
+
const indentationRegex = new RegExp('\n' + indentation, 'g')
|
|
74
|
+
email.text = email.text.slice(indentation.length).replace(indentationRegex, '\n')
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
console.log("IMAGES", Array.from(images.entries()))
|
|
79
|
+
const imageAttachments = Array.from(images.entries()).map(([imagePath, contentId]) => {
|
|
80
|
+
const imageUrl = new URL(imagePath, url)
|
|
81
|
+
const file = path.resolve(publicDir, imageUrl.pathname.slice(1))
|
|
82
|
+
const filename = path.basename(file)
|
|
83
|
+
return {
|
|
84
|
+
filename,
|
|
85
|
+
path: file,
|
|
86
|
+
cid: contentId
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
email.attachments = email.attachments || []
|
|
91
|
+
email.attachments.push(...imageAttachments)
|
|
92
|
+
|
|
93
|
+
console.log("EMAIL", email)
|
|
94
|
+
|
|
95
|
+
return email
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = renderEmail
|
package/send.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
const definition = require('./definition.js')
|
|
2
|
+
|
|
3
|
+
const nodemailer = require('nodemailer')
|
|
4
|
+
const app = require('@live-change/framework').app()
|
|
5
|
+
|
|
6
|
+
const renderEmail = require('./render.js')
|
|
7
|
+
|
|
8
|
+
const config = definition.config
|
|
9
|
+
|
|
10
|
+
const smtp = nodemailer.createTransport(config.transport || {
|
|
11
|
+
host: config.host || process.env.SMTP_HOST,
|
|
12
|
+
port: +(config.port || process.env.SMTP_PORT),
|
|
13
|
+
auth: {
|
|
14
|
+
user: (config.user || process.env.SMTP_USER),
|
|
15
|
+
pass: (config.password || process.env.SMTP_PASSWORD)
|
|
16
|
+
},
|
|
17
|
+
secure: !(config.insecure || process.env.SMTP_INSECURE), // secure:true for port 465, secure:false for port 587
|
|
18
|
+
tls: {
|
|
19
|
+
// do not fail on invalid certs
|
|
20
|
+
rejectUnauthorized: !(config.ignoreTLS || process.env.SMTP_IGNORE_TLS)
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const SentEmail = definition.model({
|
|
25
|
+
name: "SentEmail",
|
|
26
|
+
properties: {
|
|
27
|
+
email: {
|
|
28
|
+
type: Object
|
|
29
|
+
},
|
|
30
|
+
error: {
|
|
31
|
+
type: Object
|
|
32
|
+
},
|
|
33
|
+
smtp: {
|
|
34
|
+
type: Object
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
definition.trigger({
|
|
40
|
+
name: "sendEmailMessage",
|
|
41
|
+
properties: {
|
|
42
|
+
emailId: {
|
|
43
|
+
type: String
|
|
44
|
+
},
|
|
45
|
+
email: {
|
|
46
|
+
type: Object
|
|
47
|
+
},
|
|
48
|
+
render: {
|
|
49
|
+
type: Object
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
async execute({ emailId = app.generateUid(), email, render }, context, emit) {
|
|
53
|
+
if(render) {
|
|
54
|
+
email = await renderEmail(render)
|
|
55
|
+
}
|
|
56
|
+
if(!email) throw new Error('email must be defined')
|
|
57
|
+
|
|
58
|
+
if(email.to.match(/@test\.com>?$/)) {
|
|
59
|
+
console.log("TEST EMAIL TO", email.to)
|
|
60
|
+
emit({
|
|
61
|
+
type: 'sent',
|
|
62
|
+
emailId,
|
|
63
|
+
email: email,
|
|
64
|
+
smtp: {
|
|
65
|
+
test: true
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
const doSendEmail = async () => { // async it can be very slow :/
|
|
71
|
+
try {
|
|
72
|
+
console.log("SEND EMAIL", email);
|
|
73
|
+
const info = await smtp.sendMail(email)
|
|
74
|
+
emit({
|
|
75
|
+
type: 'sent',
|
|
76
|
+
emailId,
|
|
77
|
+
email: email,
|
|
78
|
+
smtp: {
|
|
79
|
+
messageId: info.messageId,
|
|
80
|
+
response: info.response
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
console.log("EMAIL SENT!", info)
|
|
84
|
+
} catch(error) {
|
|
85
|
+
console.error("EMAIL ERROR", error)
|
|
86
|
+
emit({
|
|
87
|
+
type: 'error',
|
|
88
|
+
emailId,
|
|
89
|
+
email: email,
|
|
90
|
+
error: error
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
doSendEmail()
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
definition.event({
|
|
99
|
+
name: "sent",
|
|
100
|
+
properties: {
|
|
101
|
+
emailId: {
|
|
102
|
+
type: String
|
|
103
|
+
},
|
|
104
|
+
email: {
|
|
105
|
+
type: Object
|
|
106
|
+
},
|
|
107
|
+
smtp: {
|
|
108
|
+
type: Object
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
async execute(event) {
|
|
112
|
+
await SentEmail.create({ id: event.emailId, email: event.email, smtp: event.smtp })
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
definition.event({
|
|
118
|
+
name: "error",
|
|
119
|
+
properties: {
|
|
120
|
+
emailId: {
|
|
121
|
+
type: String
|
|
122
|
+
},
|
|
123
|
+
email: {
|
|
124
|
+
type: Object
|
|
125
|
+
},
|
|
126
|
+
error: {
|
|
127
|
+
type: Object
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
async execute(event) {
|
|
131
|
+
await SentEmail.create({ id: event.emailId, email: event.email, error: event.error })
|
|
132
|
+
}
|
|
133
|
+
})
|