Skip to main content

Part 4 - Send from a Near address

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

In this step you will receive a mail sent from a Near Protocol address.

Create sendFromNear.js and paste the code below.

Update the from address with the Near Protocol address registered to your account.

// 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 Near protocol address
from: '[email protected]',
// set `to` address to authenticated user address
to: [user.address],
subject: 'My first received Near mail',
content: {
text: 'Hello Near 👋',
html: '<p>Hello Near 👋</p>',
},
});

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

This code does the following:

  • Authenticates the SDK.
  • Gets current user's address.
  • Set `from` address to your Near protocol address.
  • Set `to` address to the authenticated user.
  • Send mail.

In your terminal window run node sendFromNear.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: '290a3a5328edc15384be74b82be6c0817700ab527a8fdb54ed1ad063768bb755',
sentMailDeliveryRequests: [ [Object] ]
}
}

This means the mail has been sent successfully, open Mailchain and check your inbox to see the new mail. The inbox contains different mailboxes, there will be one for your Near address. You will see the mail in this folder too. The sent folder also contains the mail you sent.