Send from address
Mailchain's SDK provides a simple method to send a mail via your inbox. It uses your Secret Recovery Phrase to access your Mailchain account, meaning you can send from any registered address and the mail is saved in your sent folder.
Alternatively you can send a mail using an address's private messaging key. This is useful when you want to send a mail from an address without needing to expose your Secret Recovery Phrase.
Mail sent using the private messaging key will not be saved in the sent folder. Instead you should keep track of send messages within your application or in another store.
First install the packages needed.
- npm
- Yarn
- pnpm
npm install @mailchain/sdk
yarn add @mailchain/sdk
pnpm add @mailchain/sdk
The code below shows how to send a mail using the private messaging key. To get the private messaging key you can use the SDK's
getPrivateMessagingKey
method.import { MailSender, privateMessagingKeyFromHex } from '@mailchain/sdk/internal';
async function main() {
// securely supply private messaging key hex bytes
const recoveredPrivateMessagingKey = privateMessagingKeyFromHex('...');
const mailSender = MailSender.fromSenderMessagingKey(recoveredPrivateMessagingKey);
const { data: sentMail, error: sendMailError } = await mailSender.sendMail({
// address related private messaging key
from: `[email protected]`,
// list of recipients (blockchain or mailchain addresses)
to: [`0xbb56FbD7A2caC3e4C17936027102344127b7a112@ethereum.mailchain.com`],
subject: 'My first message', // subject line
content: {
text: 'Hello Mailchain 👋', // plain text body
html: '<p>Hello Mailchain 👋</p>', // html body
},
});
if (sendMailError) {
throw sendMailError;
}
console.log(`Message sent successfully: ${sentMail}`);
}
main();infoMake sure you use the correct private messaging key. If you use the wrong key the mail will not be sent.