SMTP can be replaced by a 19 line Ethereum contract

Posted September 12, 2015 by Jonathan Brown ‐ 3 min read

Originally published on jonathanpatrick.me. Retrieved from the Wayback Machine.

One of the advantages of the blockchain is that, unlike conventional Internet services, you do not have to maintain a backend in order to offer a service. You do however need to pay to write a transaction onto the blockchain.

Message delivery is certainly something that can benefit hugely from the blockchain. Note that in this blog post I am talking about delivering messages to a mailbox for retrieval, not direct device to device messaging.

In order to implement a mailbox on Ethereum in the Solidity language, this is all the code that is required:

/**
* @title Mailbox
* @author Jonathan Brown <jbrown@bluedroplet.com>
*/
contract Mailbox {

  event Message(address indexed recipient, bytes message);

  /**
   * @notice Send message to account `recipient`.
   * @param to The address of the account to send the message to.
   * @param message The message in MIME type application/pkcs7-mime.
   */
  function send(address recipient, bytes message) {
    // Store the message in the transaction log.
    Message(recipient, message);
  }

}

When a message is to be delivered to a specific account, it simply writes it onto the transaction log via an event. This is an order of magnitude cheaper than storing the message in contract storage.

As an experiment I sent a 5kB email using this technique.

It cost 0.02 ether which is currently worth ~ 0.02 USD.

Once Swarm is live, it will be possible to reduce storage costs even further.

It should be noted that many emails sent today are really just push notifications and are better suited to other Ethereum technologies such as Whisper.

The transaction ID can be considered as the unique identifier for the message.

The recipient can simply watch the transaction log for messages intended for them. Of course, just like with email, the messages need to be encrypted otherwise anyone can read them. S/MIME can be used for this purpose in the same way as it is used for email.

The sender of the transaction could be considered as the sender as the message, but this leads to a loss of privacy. The sender is recorded in the message using S/MIME digital signatures. This means the transaction can originate from any address.

A further advantage of emailing via the blockchain is that the small fee must be paid to the network. This greatly reduces the economic incentive of spam. Client software could implement any kind of spam filter / whitelisting as normal.

Also, because the blockchain is decentralized, you never have to worry about the service being unavailable.