Getting started with Mailchain: A step-by-step guide

Getting started with Mailchain: A step-by-step guide

Revolutionizing Email Communication with Mailchain: A Comprehensive Guide for Developers

Have you ever had trouble communicating with the holders of your NFT project, DAO members, or customers of your Dapp who want to remain anonymous? It can be a challenge to share updates and new features with them when they haven't shared their email address. But don't worry, Mailchain is here to help. It's a communication protocol that allows you to send messages to blockchain addresses just like an email service. With Mailchain, you can communicate with one person or a group of people, all while maintaining end-to-end encryption and privacy.

Creating Mailchain Account

To get started with Mailchain, you first need to create an account using a Secret Recovery Phrase, which is similar to how you would set up a wallet. The Secret Recovery Phrase is a randomly generated sequence of words that is unique to your Mailchain account. When you register for Mailchain, you will be prompted to create a Secret Recovery Phrase that you should store safely, just like you would with a wallet's recovery phrase. Some usernames are reserved so try to get yourself a suitable one and then register yourself.

Create your Account here.

Once you have created your Mailchain account, you can use the signature from your wallet to prove ownership of your blockchain address for importing it and enable it for Mailchain. This ensures that only you have access to your Mailchain account and messages and this would be used to generate private keys to make your communication end-to-end encrypted.

After connecting, You would get this onboarding mail from Mailchain Founder.

Setting up a Mailchain account and sending emails through the Mailchain network is quite easy.

Send your First Mail

After you have created your Mailchain account and logged in, you can start sending messages by tapping on the Compose button. This will open the Mailchain mail interface, where you can write and send your message just like you would with a regular email.

Mailchain allows you to send messages to any blockchain address, including regular addresses and those registered on ENS or Unstoppable Domains. It also supports other blockchain protocols such as NEAR, and Lens Handles which can be used to identify users across multiple networks and many other domain services. This makes it easy to manage your communications and reach out to users on different blockchains.

Once you have added the recipient's address or ENS domain name, you can write your message in the message box. You can write a personalized message just like you would with a regular email, and you can even attach files in the form of URLs if needed.

Yes, that's right! Mailchain transactions are free and do not require any gas fees since they are off-chain messages. This makes it a cost-effective and efficient way to communicate with your users on the blockchain.

Reading your Mail

Once you've received a message on Mailchain, you can easily view it by logging into your Mailchain account and connecting your wallet.

To access your inbox, simply click on the Inbox button, and you'll see all the messages that you've received. You can then click on a message to read it and reply to the sender if needed.

Since Mailchain messages are end-to-end encrypted, only the intended recipient can read the message, ensuring that your communication is secure and private.

Getting started with Mailchain SDK

We have covered how to use Mailchain's GUI to send and receive messages just like we use normal email websites. However, for developers who want to integrate Mailchain's messaging functionality into their amazing blockchain-based applications, Mailchain provides a software development kit (SDK).

Developers can use the SDK to create their own Mailchain clients, and add messaging functionality to their DApps, NFT marketplaces, or any other blockchain-based application.

The SDK includes a set of APIs that developers can use to send and receive messages, manage keys, and integrate Mailchain into their existing applications.

By using the Mailchain SDK, developers can take advantage of the benefits of the Mailchain protocol, such as end-to-end encryption and multi-chain support, and build powerful, decentralized communication systems for their users.

Configuring SDK

To get started with Mailchain development, you'll need to initialize a new npm project or can skip it if you are already working on a project so make sure you have node installed on your system.

npm init

You can use npm or yarn to add to your project. I will be using npm.

npm install @mailchain/sdk

To store and access your Mailchain Secret Phrase securely, you can use a package like dotenv. dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.

 npm i dotenv

To add your secret recovery phrase to the environment variables, you can create a file named .env in the root directory of your project. This will allow you to access the phrase in your code using process.env.SECRET_RECOVERY_PHRASE.

export SECRET_RECOVERY_PHRASE="staff gorilla ......."

Authenticate User

To authenticate the user, we can use the Mailchain SDK and the secret recovery phrase that we created earlier through the Mailchain website.

const { Mailchain } = require("@mailchain/sdk");
require("dotenv").config();

async function main() {
  const secretRecoveryPhrase = process.env.SECRET_RECOVERY_PHRASE; // 25 word mnemonicPhrase

  const mailchain = Mailchain.fromSecretRecoveryPhrase(secretRecoveryPhrase);
  const user = await mailchain.user();
  console.log(`username: ${user.username}, address: ${user.address}`);
}
main();

This code snippet imports the Mailchain SDK and the dotenv package using the require() method in Node.js.

It then defines an asynchronous function main() that retrieves the SECRET_RECOVERY_PHRASE from the .env file using process.env creates an instance of Mailchain using the secret recovery phrase retrieves the user's information using mailchain.user().

At last, it logs the username and address of the user to the console. Finally, it invokes the main() function to run the code.

npx run authenticate.js

This output shows how we get our Mailchain username and address on the console indicating the connection is established and the SDK is successfully integrated.

Sending Mail from Command Line

Finally, we would send this mail from our formidable weapon, the command line, so buckle up your seats and Behold the power of the Command line.

const { Mailchain } = require("@mailchain/sdk");
require("dotenv").config();

async function main() {
  const secretRecoveryPhrase = process.env.SECRET_RECOVERY_PHRASE; // 25 word mnemonicPhrase

  const mailchain = Mailchain.fromSecretRecoveryPhrase(secretRecoveryPhrase);
  const user = await mailchain.user();
  console.log(`username: ${user.username}, address: ${user.address}`);

  const { data, error } = await mailchain.sendMail({
    from: user.address, // sender address
    to: [
      `0xd5ba400e732b3d769aa75fc67649ef4849774bb1@ethereum.mailchain.com`, //Recipeint 1
      `0x507e27d7191c556e0fd40799cdf4abcc9c7f0706@ethereum.mailchain.com`, //Recipient 2
    ], // list of recipients (blockchain or mailchain addresses)
    subject: "Testing from Command Line", // subject line
    content: {
      text: "Hello World from Command Line", // plain text body
      html: "<p>Hello World from Command Line</p>", // html body
    },
  });

  if (error) {
    // handle error
    console.warn("Mailchain error", error);
    return;
  }
  // handle success send mail result
  console.log("Message Sent");
  console.log(data);
}
main();

In the code, we use the sendMail method to send an object that contains all the relevant information about the mail, such as sender and recipient addresses, the subject of the email, the body of the email, and any other optional parameters. By invoking this method with the necessary parameters, we can easily send a mail from the command line.

Basic error handling is added to ensure that any errors are caught and logged to the console. On successful execution, the data of the mail is logged to the console along with a message Message Sent.

npx run sendMail.js

This is a great achievement! By successfully sending an email using the command line, you have demonstrated a strong understanding of both the Mailchain SDK and the command line interface. With this skill, you can easily automate and streamline your email communication process. Keep up the great work!

With Mailchain, it is now easy to send and receive secure, decentralized email using blockchain technology. The Mailchain SDK allows developers to easily integrate Mailchain into their applications and workflows, providing a secure and decentralized messaging solution.

Conclusion

To wrap up, we hope this blog has been a helpful introduction to Mailchain and its capabilities. With its ability to send and receive encrypted messages on blockchain networks, Mailchain provides a unique solution to the problem of secure messaging. Its ease of use and integration with popular development tools make it a promising option for developers looking to build secure decentralized applications. So why not give it a try and see how Mailchain can work for you?