Last active
July 28, 2019 21:29
-
-
Save regevbr/5fa642fe5adac35e66b48c6887795084 to your computer and use it in GitHub Desktop.
mautic auto generate email text version
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
'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