Skip to content

Instantly share code, notes, and snippets.

@regevbr
Last active July 28, 2019 21:29
Show Gist options
  • Save regevbr/5fa642fe5adac35e66b48c6887795084 to your computer and use it in GitHub Desktop.
Save regevbr/5fa642fe5adac35e66b48c6887795084 to your computer and use it in GitHub Desktop.
mautic auto generate email text version
'use strict';
import * as textVersion from 'html-to-text';
import * as mysql from 'mysql';
import * as _ from 'underscore';
const connection = mysql.createConnection({
host: '<host>',
user: '<user>',
password: '<password>',
database: 'mautic',
});
connection.connect();
connection.query('SELECT id, custom_html FROM emails', (error, results) => {
if (error) {
throw error;
}
console.log(`found ${results.length}`);
const promises = _.chain(results)
.each((result) => {
result.plain_text = textVersion
.fromString(result.custom_html, {
noLinkBrackets: false,
preserveNewlines: true,
ignoreImage: true,
uppercaseHeadings: false,
wordwrap: false,
})
.replace(/(\n)\[cid:.*?\] |\[cid:.*?\]/g, '$1');
delete result.custom_html;
})
.map((result) => {
console.log(`handling ${result.id}`);
return new Promise((resolve, reject) => {
connection.query('UPDATE emails SET plain_text = ? WHERE id = ?', [result.plain_text, result.id], (updateError) => {
if (updateError) {
console.log(`error ${result.id} - ${updateError}`);
reject(updateError);
} else {
console.log(`handled ${result.id}`);
resolve();
}
});
});
})
.value();
Promise.all(promises).then(() => {
console.log('done');
connection.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment