Make sure you have configured your application and authenticated the Mailchain SDK.
In this step you will send a mail to a Protocol address.
You should now also register a Near Protocol address to your account. You can do this by following the steps here Mailchain docs: Near.
Create sendToNear.js
and paste the code below.
Update the to
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 authenticated user address
from: user.address,
// set `to` address to Near protocol address
to: ['[email protected]'],
subject: 'My first sent 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 the authenticated user.
- Set `to` address to your Near protocol address.
- Sends mail.
In your terminal window run node sendToNear.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.