Search code examples
javascriptemailgoogle-chrome-extension

Sending email from a Chrome extension


I do not want the user to have to perform or confirm any actions, as it would disrupt the general idea. In other words, mailto is out of the question.

Is it possible to send emails client-sided via JavaScript, from a Chrome extension, without disrupting the user’s experience or adding any JavaScript extension? If so, how?

I get the feeling this is going to be impossible.

I've learnt that there is a chrome.sockets.tcp API that may be useable. How could it be used?


Solution

  • To send an e-mail, a program needs to connect via TCP to an SMTP server and talk to it according to the SMTP protocol.

    1. It is not possible to send an e-mail directly using JavaScript / HTML5 APIs.

    A browser does not expose the capability to directly communicate with a TCP socket.

    While you can initiate a connection to any port in principle, the browser will "do the talking" and it does not "speak" SMTP. It can do HTTP, it can do WebSockets, but not the protocol you want.

    1. It's not possible to send an e-mail directly from a Chrome extension.

    While the Chrome extension APIs augment functionality offered by web APIs, they still don't offer you the chance to "do the talking" to a server.

    1. It is possible, but not easy, to send an e-mail directly from a Chrome app.

    Chrome Apps APIs are different from extensions, but more importantly they include raw access to TCP sockets, via chrome.sockets.tcp API.

    Note the words "raw access". You will have to implement your own mail client from scratch in JavaScript. This might be a starting point.

    Depending on what you want to achieve, a Chrome app can be an acceptable solution. Alternatively, if you need both extension and app APIs, you can create both and pass messages between them.

    1. Anything is possible with a Native Messaging host.

    Being basically a native application, you can do anything from a native host app. Chrome extension/app will then be able to call it with necessary data.

    However, this will limit your deployment options if you decide to publish the extension; you will have to worry about portability, and the native host cannot be uploaded to the Web Store; you would need a separate installer.

    And your messaging host will still need to implement your own SMTP client, though you have a much more broad choice of ready libraries for that.

    1. Anything is possible with an external API, but securing communication would be a challenge.

    If there's a website that your extension can talk to that can send an email on your behalf, you can make your extension trigger such an API.

    However, it's not realistically possible to bury an extension-specific secret into an extension to limit the use of the API.

    You could make the user log in to such a service with some credentials, and then store those user-specific credentials in an installed instance, e.g. an OAuth token. That basically offloads the problem to a web service you must control (or have an agreement with).

    For extra reading, here's Apps vs Extensions guide.