Skip to main content

Part 2 - Send first message

Make sure you have configured your application and authenticated the Mailchain SDK.

In this step you will send your first mail, a "Hello World" to yourself.

Create sendFirstMessage.js and paste the code below.

// import Mailchain SDK
const { Mailchain } = require('@mailchain/sdk');
// import .env environment variables
require('dotenv').config();

// 24 word secret recovery phrase
const secretRecoveryPhrase = process.env.SECRET_RECOVERY_PHRASE;
// create a new instance of the Mailchain SDK with the secret recovery phrase
const mailchain = Mailchain.fromSecretRecoveryPhrase(secretRecoveryPhrase);

async function main() {
// get authenticated user
const user = await mailchain.user();

const result = await mailchain.sendMail({
// set `from` address to authenticated user address
from: user.address,
// set `to` address to authenticated user address
to: [user.address],
subject: 'My first mail',
content: {
text: 'Hello Mailchain 👋',
html: '<p>Hello Mailchain 👋</p>',
},
});

console.log(result);
}
main();

This code does the following:

  • Authenticates the SDK.
  • Gets current user's address.
  • Set from and to address to the authenticated user.
  • Sends mail.

In your terminal window run node sendFirstMessage.js to run your application. You should see a message in your terminal window that looks similar to below (the `savedMessageId` will be different each time you send).

{
data: {
savedMessageId: '0763ac70f44b24370b08abfc5f56ab396c09ef854eab5da2a35154241a983bdf',
sentMailDeliveryRequests: [ [Object] ]
}
}

This means the mail has been sent successfully, open Mailchain and check your inbox the new mail. The sent folder shows the mail that was sent.

info
Mailchain is committed to using existing standards wherever possible to maximize interoperability. The Mailchain SDK standard message format follows the Internet Message Format (RFC5322) and supports MIME content headers.