Created
October 21, 2019 12:42
-
-
Save canvaspixels/b00cb11317d64c0d4163e72d3bceac0d to your computer and use it in GitHub Desktop.
Automate sending mass Emails using Gmail UI
This file contains hidden or 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
function simulateClick(el) { | |
var evt = document.createEvent("MouseEvents"); | |
evt.initMouseEvent("click", true, true, window, | |
0, 0, 0, 0, 0, false, false, false, false, 0, null); | |
var cb = el; | |
var canceled = !cb.dispatchEvent(evt); | |
} | |
function getElementByXpath(path) { | |
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; | |
} | |
function mouseEvs(el){ | |
const mouseDownEv = new MouseEvent('mousedown'); | |
el.dispatchEvent(mouseDownEv) | |
const mouseUpEv = new MouseEvent('mouseup'); | |
el.dispatchEvent(mouseUpEv) | |
} | |
function sendEmail() { | |
composeBtn = getElementByXpath('//*[text()="Compose"]') | |
mouseEvs(composeBtn) | |
setTimeout(() => { | |
toField = getElementByXpath('//span[text()="To"]/ancestor-or-self::td/following-sibling::td//textarea') | |
toField.innerText = contacts[contactIndex].email | |
subjectField = document.querySelector('[name="subjectbox"]') | |
subjectField.value = subject | |
bodyField = getElementByXpath('//span[text()="To"]/ancestor-or-self::form/following-sibling::table//div[@contenteditable="true"]') | |
bodyField.innerHTML = getBody(contacts[contactIndex].name) | |
setTimeout(() => { | |
simulateClick(getElementByXpath('//div[text()="Send"]')) // hit send | |
if (contacts[contactIndex + 1]) { | |
contactIndex++ | |
setTimeout(sendEmail, 3000) | |
} | |
}, 1000) | |
}, 3000) | |
} | |
contacts = [{ | |
email: '[email protected]', | |
name: 'Courgette Testing', | |
},{ | |
email: '[email protected]', | |
name: 'Courgette Testing', | |
}] | |
subject = 'Courgette Testing Rocks! Please star the github if you like it!' | |
getBody = (name) => `Hi ${name} | |
Testing 123 | |
Regards | |
Alex | |
`.replace(/\n/g, '<br />') | |
sendEmail() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment