newsletter plugin done

This commit is contained in:
2023-09-16 15:11:17 +02:00
parent 892772bcb9
commit 94ee69642a
4 changed files with 150 additions and 99 deletions

View File

@@ -0,0 +1,74 @@
body {
font-family: sans-serif;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-color: rgb(180, 191, 255);
}
.material-symbols-outlined {
font-variation-settings:
'FILL' 0,
'wght' 400,
'GRAD' 0,
'opsz' 48
}
.mail-app {
width: 80%;
margin-left: 5%;
margin-top: 1%;
background-color: rgb(144, 160, 255);
border-radius: 50px;
padding: 5%;
}
#toolbar, #editor {
background-color: white;
}
#top-bar {
color: white;
background-color: rgb(0, 8, 53);
margin-bottom: 0;
grid-area: header;
height: 10vh;
display: flex;
width: 100%;
justify-content: center;
align-items: center;
flex-direction: row;
}
#title {
font-size: 120%;
}
#spacer {
width: 10%;
}
.top-toolbar {
display: inline-flex;
flex-direction: row;
justify-content: center;
align-items: center;
margin-right: 10%;
gap: 10%;
}
.top-buttons {
background-color: rgba(0, 0, 0, 0);
color: white;
border-radius: 5px;
border-color: white;
border-style: solid;
padding: 0.5%;
cursor: pointer;
}
.top-buttons:hover {
color: blue;
border-color: blue;
}

View File

@@ -4,7 +4,7 @@
<!-- Include Quill stylesheet -->
<meta charset="utf-8">
<title>New Mail :: Webmail | Language School Hossegor - Admin</title>
<link rel="stylesheet" href="/admin/css/webmail-compose.css">
<link rel="stylesheet" href="/admin/plugins/newsletter/css/mailCompose.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link href="https://cdn.quilljs.com/1.0.0/quill.snow.css" rel="stylesheet" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200">
@@ -12,36 +12,14 @@
<body>
<div id="top-bar">
<div class="top-toolbar">
<a href="/admin/panel" title="Back to admin panel" id="back" class="top-buttons"><span class="material-symbols-outlined">arrow_back</span></a>
<a href="/admin/mail" title="Back to webmail" id="new" class="top-buttons"><span class="material-symbols-outlined">arrow_back_ios_new</span></a>
<a href="/admin/plugins" title="Back to admin panel" id="back" class="top-buttons"><span class="material-symbols-outlined">arrow_back</span></a>
</div>
<p id="title">Webmail - Language School Hossegor</p>
<p id="title">Newsletter plugin - libreevent</p>
<p id="spacer"></p>
</div>
<div class="mail-app">
<h1>New message</h1>
<p>All mails written here can get one of four different styles. Please choose your style in the dropdown below. You can preview them by clicking <a href="/admin/mail/preview" target="_blank">here</a></p>
<p>If you select "Newsletter", the mail will automatically be sent to everybody that subscribed to the newsletter with the correct styling. Note that you need to create a newsletter in German and English and send them separately</p>
<select id="mailTypeSelector" onchange="updateType()" title="Mail template">
<option value="default">Default</option>
<option value="booking">Booking</option>
<option value="newsletter">Newsletter</option>
<option value="plain">Plain text</option>
</select>
<select id="mailLangSelector" title="Language">
<option value="de">Deutsch</option>
<option value="en">English</option>
</select>
<!--<button onclick="getContent()">Get html</button>--><br>
<h1>New newsletter</h1>
<table>
<tr id="to">
<td>
<label for="mail">To: </label>
</td>
<td>
<input type="email" name="mail" id="mail"><br>
</td>
</tr>
<tr>
<td>
<label for="subject">Subject:</label>
@@ -104,57 +82,17 @@
<!-- Initialize Quill editor -->
<script>
var editor = new Quill('#editor', {
var editor = new Quill( '#editor', {
modules: { toolbar: '#toolbar' },
theme: 'snow',
});
function getContent () {
console.log( document.getElementsByClassName( 'ql-editor' )[0].innerHTML );
}
setTimeout( updateType, 200 );
function updateType () {
if ( document.getElementById( 'mailTypeSelector' ).value === 'newsletter' ) {
$( '#to' ).slideUp();
$( '#mailLangSelector' ).slideDown();
} else {
$( '#to' ).slideDown();
$( '#mailLangSelector' ).slideUp();
};
if ( window.location.search.slice( 1, 8 ) === 'replyTo' ) {
document.getElementById( 'subject' ).value = 'Re: ' + sessionStorage.getItem( 'mailSubject' );
document.getElementById( 'mail' ).value = sessionStorage.getItem( 'mailAddress' );
}
};
} );
function sendMail () {
let ok = false;
if ( document.getElementById( 'mailTypeSelector' ).value !== 'newsletter' ) {
if ( document.getElementById( 'mail' ).value != '' && document.getElementById( 'subject' ).value != '' ) {
ok = true;
} else {
alert( 'A mail address and a subject are required!' );
};
} else {
if ( document.getElementById( 'subject' ).value != '' ) {
if ( confirm( 'Do you really want to send the newsletter?' ) ) {
ok = true;
};
} else {
alert( 'A subject is required!' );
};
};
if ( ok ) {
let dat = { 'receiver': document.getElementById( 'mail' ).value, 'subject': document.getElementById( 'subject' ).value, 'mode': document.getElementById( 'mailTypeSelector' ).value, 'lang': document.getElementById( 'mailLangSelector' ).value };
dat[ 'message' ] = document.getElementsByClassName( 'ql-editor' )[0].innerHTML;
if ( window.location.search.slice( 1, 8 ) === 'replyTo' ) {
dat[ 'inReplyTo' ] = sessionStorage.getItem( 'replyTo' );
sessionStorage.removeItem( 'replyTo' );
sessionStorage.removeItem( 'mailSubject' );
sessionStorage.removeItem( 'mailAddress' );
window.location = '/admin/mail/compose';
};
if ( !document.getElementById( 'subject' ).value ) {
alert( 'An email subject is required!' );
return;
}
let dat = { 'subject': document.getElementById( 'subject' ).value, 'message': document.getElementsByClassName( 'ql-editor' )[0].innerHTML };
let options = {
method: 'post',
body: JSON.stringify( dat ),
@@ -163,18 +101,19 @@
'charset': 'utf-8'
},
};
fetch( '/admin/mail/send', options ).then( res => {
fetch( '/admin/plugins/newsletter/send', options ).then( res => {
if ( res.status === 200 ) {
alert( 'Mail sent successfully' );
alert( 'Newsletter sent successfully' );
} else if ( res.status === 403 ) {
alert( 'It appears that you have been logged out or have logged out somewhere else. Please ensure that you are logged in and try again!' );
} else {
alert( 'There was an error sending the mail. Please try again. If the error persists, please contact the developer and tell this status code: ' + res.status );
};
}
} );
document.getElementById( 'subject' ).value = '';
document.getElementById( 'mail' ).value = '';
document.getElementsByClassName( 'ql-editor' )[0].innerHTML = '';
};
}
</script>
</body>
</html>

View File

@@ -8,11 +8,12 @@
*/
const path = require( 'path' );
const mm = require( '../../../mail/mailSender.js' );
const sendMail = new mm();
const mm = require( './sender.js' );
const bodyParser = require( 'body-parser' );
module.exports = ( app ) => {
app.get( '/admin/mail/compose', ( request, response ) => {
module.exports = ( app, settings ) => {
const sendMail = new mm( settings );
app.get( '/admin/plugins/newsletter', ( request, response ) => {
if ( request.session.loggedInAdmin ) {
response.sendFile( path.join( __dirname + '/html/compose.html' ) );
} else {
@@ -20,10 +21,14 @@ module.exports = ( app ) => {
}
} );
app.post( '/admin/mail/send', ( request, response ) => {
app.get( '/admin/plugins/newsletter/css/:file', ( req, res ) => {
res.sendFile( path.join( __dirname + '/css/' + req.params.file ) );
} );
app.post( '/admin/plugins/newsletter/send', bodyParser.json(), ( request, response ) => {
if ( request.session.loggedInAdmin ) {
response.send( 'ok' );
sendMail.send( request.body.message, request.body.subject, request.body.mode, request.body.replyTo, request.body.receiver, request.body.lang );
sendMail.send( request.body.message, request.body.subject );
} else {
response.status( 403 ).send( 'unauthenticated' );
}
@@ -34,7 +39,7 @@ module.exports = ( app ) => {
} );
app.post( '/mail/unsubscribe/go', ( request, response ) => {
if ( request.body.mail == '' ) {
if ( !request.body.mail ) {
response.sendFile( path.join( __dirname + '/html/unsubscribeError.html' ) );
} else {
sendMail.unsubscribe( request.body.mail );

View File

@@ -0,0 +1,33 @@
/*
* libreevent - sender.js
*
* Created by Janis Hutz 09/16/2023, Licensed under the GPL V3 License
* https://janishutz.com, development@janishutz.com
*
*
*/
const mm = require( '../../../mail/mailSender.js' );
const mailManager = new mm();
const db = require( '../../../db/db.js' );
class sendMail {
constructor( settings ) {
this.settings = settings;
}
send ( message, subject ) {
db.getDataSimple( 'users', 'marketing', 'true' ).then( users => {
for ( let user in users ) {
mailManager.sendMail( users[ user ].email, message, subject, this.settings.mailSender );
}
} );
}
unsubscribe ( mail ) {
db.writeDataSimple( 'users', 'email', mail, { 'marketing': false } );
}
}
module.exports = sendMail;