DEV Community

Cover image for Sending Email in Node.js
Dexter
Dexter

Posted on • Edited on • Originally published at blog.lamtell.com

Sending Email in Node.js

Superface.ai is a language and a protocol for abstracting integrations to application use-cases. It allows use-case discovery and distribution of integration code at runtime.

Implementing API Integration becomes easy with Superface.ai because you just need to learn it and you can implement more just 40 use-cases without having to learn how to implement all of them separately.

Now I'll show you how you can send email with node.js using Superface.ai. First create a node.js package.json file using

npm init -y
Enter fullscreen mode Exit fullscreen mode

Then you have to install the superface sdk you need to install this to use superface

npm install --save @superfaceai/one-sdk
Enter fullscreen mode Exit fullscreen mode

Image description

Then choose your use-case we are going to use Send Email in the Communication section. Install communication/send-email for this use-case. Depending upon the what you want you can install different packages like for face detection computer-vision/face-detection etc.

npx @superfaceai/cli install communication/send-email
Enter fullscreen mode Exit fullscreen mode

Image description

Now you have configure the provider you want to use I am going with sendgrid. First create your on sendgrid account get your api key and verify Single Sender Verification

npx @superfaceai/cli configure sendgrid -p communication/send-email
//use set for Win 10
export SENDGRID_TOKEN=<your-value-from-sendgrid>
Enter fullscreen mode Exit fullscreen mode

I am using https://emailfake.com/ to get some temporary email. Some alternative options

https://10minutemail.com/

https://mytemp.email/

After setting up your provider copy paste the code from the example

const { SuperfaceClient } = require('@superfaceai/one-sdk');

        const sdk = new SuperfaceClient();

        async function run() {
          // Load the installed profile
          const profile = await sdk.getProfile('communication/send-email');

          // Use the profile
          const result = await profile
            .getUseCase('SendEmail')
            .perform({
              from: 'cedesdxesxd@24mail.top',
              to: 'cedesdxesxd@omdiaco.com',
              subject: 'Your order has been shipped!',
              text: 'Hello Cedes, your recent order on Our Shop has been shipped.',
            });

            try {
                const data = result.unwrap();
                console.log(data)
              } catch (error) {
                console.error(error)
              }
        }

run();
Enter fullscreen mode Exit fullscreen mode

Everything is done now just run your code as we can see email is received

Image description

Now you can implement API integration for more than 40 use-cases learning just Superface.ai

To learn how to send email in Node.js

Original Blog - https://blog.lamtell.com/blog/superfaceai-new-era-for-api

Github Code - https://github.com/cigar-galaxy82/Email-Node.js

Top comments (3)

Collapse
 
steve-lebleu profile image
Steve Lebleu

Thanks for sharing. Really interesting. I implemented something similar, a little bit in a different way but to tackle exactly same purposes.

There is the beast: github.com/steve-lebleu/cliam

It's a clear response to heterogeneous complixity of email sending. SMTP or API provider, same interface. If you switch the way you send emails, your code doesn't care.

With this package, you just need to define your configuration:

{
  "id": "hosting-smtp",
  "auth": {
    "username": "USERNAME",
    "password": "¨PASSWORD"
  },
  "options": {
    "host": "mail.host.com",
    "port": 587,
    "secure": true
  }
}
Enter fullscreen mode Exit fullscreen mode

And to shoot:

import { Cliam } from 'cliam';

const payload = {
  transporterId: 'hosting-smtp',
  meta: {...},
  content: [
    { type: 'text/html', value: '<html><p>Email content</p></html>' }
  ]
};

Cliam.mail('user.welcome', payload)
  .then(res => {
    console.log('Email has been delivered: ', res);
  })
  .catch(err => {
    console.log('Error while mail sending: ', err)
  });
Enter fullscreen mode Exit fullscreen mode

The nodemailer core library is still used for SMTP sendings, API providers are reached through a small HTTP client.

Try it ;-)

Collapse
 
jnv profile image
Jan Vlnas

Convenient, thanks for sharing. Unfortunately the AGPL license makes it unsuitable for any closed-source project.

Collapse
 
steve-lebleu profile image
Steve Lebleu

True, but it's not engraved in the stone. I think I'll enlarge this license in a near future.