In this article, we will learn how to send emails in node.js. We all receive email on daily basis, and it is one of the most crucial forms of communication today so knowing how to send one using node.js is quite awesome and useful
I am going to use
mkdir sendEmail
cd sendEmail
npm init -y
npm i @superfaceai/one-sdk
Then choose your use case. We are going to use
Now you have to configure the provider you want to use I am going with SendGrid. First, create your account
I am using
Back in Superface, copy and paste the code from the example and replace the token with your key.
const { SuperfaceClient } = require('@superfaceai/one-sdk');
const sdk = new SuperfaceClient();
export default async function run(req,res) {
const profile = await sdk.getProfile('communication/[email protected]')
// Use the profile
const result = await profile
.getUseCase('SendEmail')
.perform({
from: '[email protected]',
to: '[email protected]',
subject: 'Your order has been shipped!',
text: 'Hello Cedes, your recent order on Our Shop has been shipped.',
}, {
provider: 'sendgrid',
security: {
bearer_token: {
token: '<your token from sendgrid>'
}
}
});
try {
const data = result.unwrap();
res.send(data)
} catch (error) {
console.error(error);
}
}
run()
Now to check if everything works, run the application:
node index.js
We can see the email is received.
You can also send attachments like pdf files and more.
To learn how to send email in Node.js —
Also Published Here