-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af0e328
commit a4004e8
Showing
8 changed files
with
102 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,75 @@ | ||
const logger = require('./logger') | ||
|
||
const languageTemplates = require('./languageTemplates') | ||
|
||
const getFrom = require('../provider/getFrom') | ||
|
||
const mailer = require('nodemailer') | ||
|
||
module.exports = async mail => { | ||
let transport | ||
|
||
if (!process.env.TRANSPORT && !process.env.TRANSPORT_HOST) return console.error(new Error('Transport not set.')) | ||
module.exports = async params => { | ||
|
||
if (!process.env.TRANSPORT && !process.env.TRANSPORT_HOST) { | ||
console.warn('No transport method set for mail.') | ||
return; | ||
} | ||
|
||
const email = process.env.TRANSPORT_EMAIL || process.env.TRANSPORT.split(':')[1] | ||
|
||
Object.assign(mail, { | ||
from: email, | ||
sender: email, | ||
}) | ||
if (!transport) { | ||
|
||
transport = process.env.TRANSPORT ? | ||
mailer.createTransport(process.env.TRANSPORT) : | ||
mailer.createTransport({ | ||
host: process.env.TRANSPORT_HOST, | ||
name: email.match(/[^@]*$/)[0], | ||
port: process.env.TRANSPORT_PORT || 587, | ||
secure: false, | ||
requireTLS: process.env.TRANSPORT_TLS && true, | ||
auth: { | ||
user: email, | ||
pass: process.env.TRANSPORT_PASSWORD | ||
} | ||
}) | ||
} | ||
|
||
mail.text = mail.text && mail.text.replace(/^(?!\s+$)\s+/gm, '') | ||
const template = await languageTemplates(params.template, params.language) | ||
|
||
if (!process.env.TRANSPORT_HOST) return mailer.createTransport(process.env.TRANSPORT).sendMail(mail) | ||
if (template.text) { | ||
|
||
const transporter = mailer.createTransport( { | ||
host: process.env.TRANSPORT_HOST, | ||
name: email.match(/[^@]*$/)[0], | ||
port: process.env.TRANSPORT_PORT || 587, | ||
secure: false, | ||
requireTLS: process.env.TRANSPORT_TLS && true, | ||
auth: { | ||
user: email, | ||
pass: process.env.TRANSPORT_PASSWORD | ||
if (Object.hasOwn(getFrom, template.text?.split(':')[0])) { | ||
|
||
template.text = await getFrom[template.text.split(':')[0]](template.text) | ||
} | ||
}) | ||
|
||
const result = await transporter.sendMail(mail).catch(err => console.error(err)) | ||
template.text = replaceStringParams(template.text, params) | ||
|
||
template.text = template.text.replace(/^(?!\s+$)\s+/gm, '') | ||
} | ||
|
||
if (Object.hasOwn(getFrom, template.html?.split(':')[0])) { | ||
|
||
template.html = await getFrom[template.html.split(':')[0]](template.html) | ||
|
||
template.html = replaceStringParams(template.html, params) | ||
} | ||
|
||
template.subject = replaceStringParams(template.subject, params) | ||
|
||
template.to = params.to | ||
template.from = email | ||
template.sender = email | ||
|
||
const result = await transport.sendMail(template).catch(err => console.error(err)) | ||
|
||
logger(result, 'mailer') | ||
} | ||
|
||
function replaceStringParams(string, params) { | ||
|
||
return string.replace(/\$\{{1}(.*?)\}{1}/g, | ||
|
||
// Replace matched params in string values | ||
matched => params[matched.replace(/\$\{{1}|\}{1}/g, '')] || '') | ||
} |