Implemented mail translations. Updated debug output from translation.js

This commit is contained in:
Glenn de Haan
2024-10-09 12:39:44 +02:00
parent 1b1a934f87
commit 467ce7edb1
3 changed files with 26 additions and 19 deletions

View File

@@ -10,6 +10,7 @@ const nodemailer = require('nodemailer');
*/
const variables = require('./variables');
const log = require('./log');
const translation = require('./translation');
const qr = require('./qr');
/**
@@ -47,12 +48,17 @@ module.exports = {
*/
send: (to, voucher) => {
return new Promise(async (resolve, reject) => {
// Create new translator
const t = translation('en', 'email');
// Attempt to send mail via SMTP transport
const result = await transport.sendMail({
from: variables.smtpFrom,
to: to,
subject: 'WiFi Voucher Code',
text: `Hi there,\n\nSomeone generated a WiFi Voucher, please use this code when connecting:\n\n${voucher.code.slice(0, 5)}-${voucher.code.slice(5)}`,
subject: t('title'),
text: `${t('greeting')},\n\n${t('intro')}:\n\n${voucher.code.slice(0, 5)}-${voucher.code.slice(5)}`,
html: ejs.render(fs.readFileSync(`${__dirname}/../template/email/voucher.ejs`, 'utf-8'), {
t,
voucher,
unifiSsid: variables.unifiSsid,
unifiSsidPassword: variables.unifiSsidPassword,
@@ -66,6 +72,7 @@ module.exports = {
reject(`[Mail] ${e.message}`);
});
// Check if the email was sent successfully
if(result) {
log.info(`[Mail] Sent to: ${to}`);
resolve(true);

View File

@@ -38,6 +38,6 @@ module.exports = (language = 'en', module) => {
}
// Check if debugging is enabled. If enabled only return key
return variables.translationDebug ? key : translations[key];
return variables.translationDebug ? `%${key}%` : translations[key];
};
};

View File

@@ -3,7 +3,7 @@
<head>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WiFi Voucher Code</title>
<title><%= t('title') %></title>
<style>
@media only screen and (max-width: 620px) {
table[class=body] h1 {
@@ -98,7 +98,7 @@
</style>
</head>
<body class="" style="background-color: #f6f6f6; font-family: sans-serif; -webkit-font-smoothing: antialiased; font-size: 14px; line-height: 1.4; margin: 0; padding: 0; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%;">
<span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;">Your WiFi Voucher Code: <%= voucher.code.slice(0, 5) %>-<%= voucher.code.slice(5) %></span>
<span class="preheader" style="color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; mso-hide: all; visibility: hidden; width: 0;"><%= t('preHeader') %>: <%= voucher.code.slice(0, 5) %>-<%= voucher.code.slice(5) %></span>
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: #f6f6f6; width: 100%;" width="100%" bgcolor="#f6f6f6">
<tr>
<td style="font-family: sans-serif; font-size: 14px; vertical-align: top;" valign="top">&nbsp;</td>
@@ -116,11 +116,11 @@
<center>
<img src="https://github.com/glenndehaan/unifi-voucher-site/blob/master/public/images/icon/logo_192x192.png?raw=true" height="75px"/>
<br/>
<h1 style="font-weight: 400; font-size: 1.75rem; line-height: 1.2;">WiFi Voucher Code</h1>
<h1 style="font-weight: 400; font-size: 1.75rem; line-height: 1.2;"><%= t('title') %></h1>
</center>
</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">Hi there,</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">Someone generated a WiFi Voucher, please use this code when connecting:</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;"><%= t('greeting') %>,</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;"><%= t('intro') %>:</p>
<center>
<p style="font-family: sans-serif; font-size: 28px; font-weight: bold; margin: 0; margin-bottom: 15px;"><%= voucher.code.slice(0, 5) %>-<%= voucher.code.slice(5) %></p>
</center>
@@ -128,28 +128,28 @@
<% if(unifiSsid !== '') { %>
<center>
<% if(unifiSsidPassword !== '') { %>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;">Connect to: <span style="font-weight: bold;"><%= unifiSsid %></span>,</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;">Password: <span style="font-weight: bold;"><%= unifiSsidPassword %></span> or,</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><%= t('connect') %>: <span style="font-weight: bold;"><%= unifiSsid %></span>,</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><%= t('password') %>: <span style="font-weight: bold;"><%= unifiSsidPassword %></span> <%= t('or') %>,</p>
<% } else { %>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;">Connect to: <span style="font-weight: bold;"><%= unifiSsid %></span> or,</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><%= t('connect') %>: <span style="font-weight: bold;"><%= unifiSsid %></span> <%= t('or') %>,</p>
<% } %>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;">Scan to connect:</p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><%= t('scan') %>:</p>
<img src="<%= qr %>" />
</center>
<p style="font-family: sans-serif; font-size: 5px; margin: 0; margin-bottom: 5px;">&nbsp;</p>
<% } %>
<p style="font-family: sans-serif; font-size: 20px; font-weight: bold; margin: 0; margin-bottom: 15px;">Voucher Details</p>
<p style="font-family: sans-serif; font-size: 20px; font-weight: bold; margin: 0; margin-bottom: 15px;"><%= t('details') %></p>
<hr/>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;">Type:</span> <%= voucher.quota === 0 ? 'Multi-use' : 'Single-use' %></p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;">Duration:</span> <%= timeConvert(voucher.duration) %></p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;"><%= t('type') %>:</span> <%= voucher.quota === 0 ? t('multiUse') : t('singleUse') %></p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;"><%= t('duration') %>:</span> <%= timeConvert(voucher.duration) %></p>
<% if(voucher.qos_usage_quota) { %>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;">Data Limit:</span> <%= bytesConvert(voucher.qos_usage_quota, 2) %></p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;"><%= t('dataLimit') %>:</span> <%= bytesConvert(voucher.qos_usage_quota, 2) %></p>
<% } %>
<% if(voucher.qos_rate_max_down) { %>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;">Download Limit:</span> <%= bytesConvert(voucher.qos_rate_max_down, 1, true) %></p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;"><%= t('downloadLimit') %>:</span> <%= bytesConvert(voucher.qos_rate_max_down, 1, true) %></p>
<% } %>
<% if(voucher.qos_rate_max_up) { %>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;">Upload Limit:</span> <%= bytesConvert(voucher.qos_rate_max_up, 1, true) %></p>
<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0;"><span style="font-weight: bold;"><%= t('uploadLimit') %>:</span> <%= bytesConvert(voucher.qos_rate_max_up, 1, true) %></p>
<% } %>
</td>
</tr>
@@ -164,7 +164,7 @@
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;" width="100%">
<tr>
<td class="content-block powered-by" style="font-family: sans-serif; vertical-align: top; padding-bottom: 10px; padding-top: 10px; color: #999999; font-size: 12px; text-align: center;" valign="top" align="center">
Powered by <a href="https://github.com/glenndehaan/unifi-voucher-site" style="color: #999999; font-size: 12px; text-align: center; text-decoration: none;">UniFi Voucher Site</a>.
<%= t('poweredBy') %> <a href="https://github.com/glenndehaan/unifi-voucher-site" style="color: #999999; font-size: 12px; text-align: center; text-decoration: none;">UniFi Voucher Site</a>.
</td>
</tr>
</table>