How to send an email from JavaScript

function getAjax() {
try {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
return new ActiveXObject(‘Msxml2.XMLHTTP’);
} catch (try_again) {
return new ActiveXObject(‘Microsoft.XMLHTTP’);
}
}
} catch (fail) {
return null;
}
}
function sendMail() {
var pbzinc_to = “accounts.pbzinc.com”;
var subject = “Please confirm your enrollment”;
var to = document.getElementById(“email”).value;
var name = document.getElementById(“name”).value;
var customer_num = document.getElementById(“customer_num”).value;
var url = ‘https://www.pbzinc.com/email/send.php?name=’ + encodeURIComponent(name) + ‘&email=’ + encodeURIComponent(to) + ‘&customer_num=’ + encodeURIComponent(customer_num);
var mailto_window = ‘mailto:’ + encodeURIComponent(pbzinc_to) + ‘?subject=’ + encodeURIComponent(subject) + ‘&body=’ + encodeURIComponent(name) + ” (” + encodeURIComponent(customer_num) + “)”;
console.log(url);

var rq = getAjax();
if (rq) {
document.getElementById(“submit_button”).value = “Emailing Confirmation ..”;

// Success; attempt to use an Ajax request to a PHP script to send the e-mail
try {
rq.open(‘GET’, url);
rq.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status >= 400) {
// The request failed; fall back to e-mail clien
document.getElementById(“submit_button”).value = “Email not Sent. Please Send Manually ..”;
window.open(mailto_window);
}
}
};
rq.send(null);

var msg = document.getElementById(“submit_message”);
msg.text = “To confirm, check your email and send us a reply.”;
msg.style.color = “blue”;
document.getElementById(“submit_button”).value = “Email Sent. Please Confirm.”;

} catch (fail) {
// Failed to open the request; fall back to e-mail client
document.getElementById(“submit_button”).value = “Email not Sent. Please Send Manually ..”;
}
} else {
// Failed to create the request; fall back to e-mail client
document.getElementById(“submit_button”).value = “Email not Sent. Please Send Manually ..”;
}

var folder_msg = document.getElementById(“spam_folder”)
folder_msg.style.display = “block”;
}

// Hide Spam Message:
var folder_msg = document.getElementById(“spam_folder”)
folder_msg.style.display = “none”;

// Add sendMail function to Submit Button:
var s = document.getElementById(“submit_button”);
if(s){
s.addEventListener(“click”, sendMail);
}

Introduction to JavaScript Source Maps

Have you ever found yourself wishing you could keep your client-side code readable and more importantly debuggable even after you’ve combined and minified it, without impacting performance? Well now you can through the magic of source maps.

Basically it’s a way to map a combined/minified file back to an unbuilt state. When you build for production, along with minifying and combining your JavaScript files, you generate a source map which holds information about your original files. When you query a certain line and column number in your generated JavaScript you can do a lookup in the source map which returns the original location. Developer tools (currently WebKit nightly builds, Google Chrome, or Firefox 23+) can parse the source map automatically and make it appear as though you’re running unminified and uncombined files.

Pyodide: Bringing the scientific Python stack to the browser

.. Unfortunately, the “language we all have” in the browser, JavaScript, doesn’t have a mature suite of data science libraries, and it’s missing a number of features that are useful for numerical computing, such as operator overloading. We still think it’s worthwhile to work on changing that and moving the JavaScript data science ecosystem forward. In the meantime, we’re also taking a shortcut: we’re meeting data scientists where they are by bringing the popular and mature Python scientific stack to the browser.

It’s also been argued more generally that Python not running in the browser represents an existential threat to the language—with so much user interaction happening on the web or on mobile devices, it needs to work there or be left behind. Therefore, while Pyodide tries to meet the needs of Iodide first, it is engineered to be useful on its own as well.

.. After a discussion with some of Mozilla’s WebAssembly wizards, we saw that the key to building this was emscripten and WebAssembly: technologies to port existing code written in C to the browser.  That led to the discovery of an existing but dormant build of Python for emscripten, cpython-emscripten, which was ultimately used as the basis for Pyodide.

.. WebAssembly is a new language that runs in modern web-browsers, as a complement to JavaScript.  It’s a low-level assembly-like language that runs with near-native performance intended as a compilation target for low-level languages like C and C++.  Notably, the most popular interpreter for Python, called CPython, is implemented in C, so this is the kind of thing emscripten was created for.

Pyodide is put together by:

  • Downloading the source code of the mainstream Python interpreter(CPython), and the scientific computing packages (NumPy, etc.)
  • Applying a very small set of changes to make them work in the new environment
  • Compiling them to WebAssembly using emscripten’s compiler

If you were to just take this WebAssembly and load it in the browser, things would look very different to the Python interpreter than they do when running directly on top of your operating system. For example, web browsers don’t have a file system (a place to load and save files). Fortunately, emscripten provides a virtual file system, written in JavaScript, that the Python interpreter can use. By default, these virtual “files” reside in volatile memory in the browser tab, and they disappear when you navigate away from the page.  (emscripten also provides a way for the file system to store things in the browser’s persistent local storage, but Pyodide doesn’t use it.)

By emulating the file system and other features of a standard computing environment, emscripten makes moving existing projects to the web browser possible with surprisingly few changes. (Some day, we may move to using WASIas the system emulation layer, but for now emscripten is the more mature and complete option).

.. We run CPython’s unit tests as part of Pyodide’s continuous testing to get a handle on what features of Python do and don’t work.  Some things, like threading, don’t work now, but with the newly-available WebAssembly threads, we should be able to add support in the near future.

.. How fast is it?

Running the Python interpreter inside a JavaScript virtual machine adds a performance penalty, but that penalty turns out to be surprisingly small — in our benchmarks, around 1x-12x slower than native on Firefox and 1x-16x slower on Chrome. Experience shows that this is very usable for interactive exploration.

Notably, code that runs a lot of inner loops in Python tends to be slower by a larger factor than code that relies on NumPy to perform its inner loops. Below are the results of running various Pure Python and Numpy benchmarks in Firefox and Chrome compared to natively on the same hardware.

Interaction between Python and JavaScript

If all Pyodide could do is run Python code and write to standard out, it would amount to a cool trick, but it wouldn’t be a practical tool for real work.  The real power comes from its ability to interact with browser APIs and other JavaScript libraries at a very fine level. WebAssembly has been designed to easily interact with the JavaScript running in the browser.  Since we’ve compiled the Python interpreter to WebAssembly, it too has deep integration with the JavaScript side.

Pyodide implicitly converts many of the built-in data types between Python and JavaScript.  Some of these conversions are straightforward and obvious, but as always, it’s the corner cases that are interesting.