Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// you will need to run emailjs-tcp-socket server
// https://github.com/emailjs/emailjs-tcp-socket
// NODE_ENV=integration node ws-proxy/server.js
// Also for gmail turn on less secure app option
// https://www.google.com/settings/security/lesssecureapps
// In case cert is revoked/expired generate new cert
// https://support.google.com/a/answer/6180220?hl=en
// fill your gmail credentials and change ws-proxy url at very bottom
// then you can just copy paste into your favourite browser console
// and yeah, it is not very clever to load scripts from github, but didn't found any CDN.
var gmailca =
'-----BEGIN CERTIFICATE-----\n'+
'MIIEgDCCA2igAwIBAgIIfMman9+czWkwDQYJKoZIhvcNAQELBQAwSTELMAkGA1UE\n'+
'BhMCVVMxEzARBgNVBAoTCkdvb2dsZSBJbmMxJTAjBgNVBAMTHEdvb2dsZSBJbnRl\n'+
'cm5ldCBBdXRob3JpdHkgRzIwHhcNMTYwNjMwMTQ1ODM5WhcNMTYwOTIyMTQ1MzAw\n'+
'WjBoMQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwN\n'+
'TW91bnRhaW4gVmlldzETMBEGA1UECgwKR29vZ2xlIEluYzEXMBUGA1UEAwwOc210\n'+
'cC5nbWFpbC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCG2pVu\n'+
'NQl/MS3rRXTfsvMVqRwnq1Wp5lfZQzirPFDq5zVPZdUjFuvXWSARG3jTgWQmImxF\n'+
'5LHadoDT/1hUcvXdHVMDToGidd7oS34uB0tJMzg5V5EYngpNapk5tG+1RnmATMGp\n'+
'/FadP/ua5w4SQSy+HelVQDLEPBw69vaCvwVUpE/++36fJejxd3PFQTgOyxzfK7hO\n'+
'q6kjD5CU22lhdg+/DBlO6jt/3k3s0870JJDzG5yPhRTQray/q82W0rdi7nSIBrpz\n'+
'q0QjwrcqeTvEXn+WIMTKNar2pYMJ+3FM/IzMLxOZBdbkDIeC8hdOfc4nDCRHF9xr\n'+
'GCBaa1mIoVWfOatlAgMBAAGjggFLMIIBRzAdBgNVHSUEFjAUBggrBgEFBQcDAQYI\n'+
'KwYBBQUHAwIwGQYDVR0RBBIwEIIOc210cC5nbWFpbC5jb20waAYIKwYBBQUHAQEE\n'+
'XDBaMCsGCCsGAQUFBzAChh9odHRwOi8vcGtpLmdvb2dsZS5jb20vR0lBRzIuY3J0\n'+
'MCsGCCsGAQUFBzABhh9odHRwOi8vY2xpZW50czEuZ29vZ2xlLmNvbS9vY3NwMB0G\n'+
'A1UdDgQWBBRuqJafObl4pMJL80jVAaHoQvtvyTAMBgNVHRMBAf8EAjAAMB8GA1Ud\n'+
'IwQYMBaAFErdBhYbvPZotXb1gba7Yhq6WoEvMCEGA1UdIAQaMBgwDAYKKwYBBAHW\n'+
'eQIFATAIBgZngQwBAgIwMAYDVR0fBCkwJzAloCOgIYYfaHR0cDovL3BraS5nb29n\n'+
'bGUuY29tL0dJQUcyLmNybDANBgkqhkiG9w0BAQsFAAOCAQEAZWAOHh4qrhVxHqfZ\n'+
'kZGrVKOksEZcjqB1sS1R3vue7L9BcReufk/SsdQHn+Uo3UKgC0BJpiP0cZ+Du4ni\n'+
'T4vUFdylXdz+N8eHCOx9lct2rcg2cEZI9nVCiQxKjXDF0eHguOrSOMt5v7cnKFei\n'+
'C8fM2kfpmXSNyWpj/P38AOtjskBHBwfzdNEnro9xODMRNnp66bnGWXnOlAgi3vup\n'+
'uTzYMTID/qE+uBKuXEvX050AqMM/DcB1DKUy97iJe8MjyHwrELJbBkIqP5gkNQsY\n'+
'jOJ6ZzgW2aeCVCCoBw/EfpDYXCL6oKnn/2NJalWAfwCL7hO+DVsCKoXRBJA+eTob\n'+
'UmrB4g==\n'+
'-----END CERTIFICATE-----';
var load = (function() {
// Function which returns a function: https://davidwalsh.name/javascript-functions
function _load(tag) {
return function(url) {
// This promise will be used by Promise.all to determine success or failure
return new Promise(function(resolve, reject) {
var element = document.createElement(tag);
var parent = 'body';
var attr = 'src';
// Important success and error for the promise
element.onload = function() {
resolve(url);
};
element.onerror = function() {
reject(url);
};
// Need to set different attributes depending on tag type
switch(tag) {
case 'script':
element.async = false;
break;
case 'link':
element.type = 'text/css';
element.rel = 'stylesheet';
attr = 'href';
parent = 'head';
}
// Inject into document to kick off loading
element[attr] = url;
document[parent].appendChild(element);
});
};
}
return {
css: _load('link'),
js: _load('script'),
img: _load('img')
}
})();
function sendMail(mail, options) {
function smtpSend(mail, options) {
options = options || {};
var alreadySending = false;
var client = new SmtpClient(options.host, options.port, options.connectionOptions);
client.onidle = function() {
console.log('Connection has been established');
if (alreadySending){
return;
}
alreadySending = true;
client.useEnvelope({
from: options.from,
to: options.to
});
};
client.onready = function(failedRecipients) {
if (failedRecipients.length){
console.log('The following addresses were rejected:', failedRecipients);
}
client.send('Content-Type: text/plain; charset="iso-8859-1"; format="flowed"\r\n');
client.send('Subject: ' + mail.subject + '\r\n');
client.send('\r\n');
client.send(mail.body);
client.end();
client.quit();
};
client.ondone = function(success){
if (success){
console.log('Email has been sent.' + response);
} else {
// show the last message
console.log(client.log.slice(-1));
}
}
client.connect();
}
// will load jQuery if you not using it in your app
if (window.SmtpClient) {
smtpSend(mail, options);
return;
}
Promise.all([
load.js('https://cdn.socket.io/socket.io-1.4.5.js'),
load.js('http://datadrive24.com/js/lib/Forge/forge.min.js'),
// shim for safari
load.js('https://rawgit.com/emailjs/emailjs-stringencoding/master/src/emailjs-stringencoding.js'),
//end of safari shim
load.js('https://rawgit.com/emailjs/emailjs-tcp-socket/master/src/emailjs-tcp-socket-tls.js'),
load.js('https://rawgit.com/emailjs/emailjs-tcp-socket/master/src/emailjs-tcp-socket.js'),
load.js('https://rawgit.com/emailjs/emailjs-smtp-client/master/src/emailjs-smtp-client-response-parser.js'),
load.js('https://rawgit.com/mauron85/emailjs-smtp-client/master/src/emailjs-smtp-client.js')
]).then(function() {
console.log('Everything has loaded!');
window.SmtpClient = window['emailjs-smtp-client'];
// window.SmtpClientResponseParser = window['emailjs-smtp-client-response-parser'];
// window.TCPSocket = navigator.TCPSocket;
smtpSend(mail, options);
}).catch(function() {
console.log('Oh no, epic failure!');
});
}
sendMail({ subject: 'Hello', body: 'sent from browser' }, {
host: 'smtp.gmail.com',
port: 465,
to: ['somebody@somewhere.com'],
connectionOptions: {
ws: {
url: 'http://ws-proxy:8889'
},
useSecureTransport: true,
ca: gmailca,
// ignoreTLS: true
auth: {
user: 'my@gmail.com',
pass: 'mygmailpassw'
},
authMethod: 'PLAIN'
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment