Skip to Content
Technical Articles

Send an email from a nodejs application

Hi All,

For a small business application we want to send e-mails from our nodejs application. In this blog post I will show you how you can use sap-cf-mailer based on nodemailer to send emails through an SMTP server defined in your destinations.

Test SMTP server

Instead of testing the code with your own email account and loading your mailbox full with test emails, you can use mailtrap as test SMTP server. It’s free for one inbox.

Create an account on mailtrap and create a new inbox. Open the inbox and take a look at the settings. All mails send over this SMTP server will be delivered here without sending them to the real receiver.


MAIL destination

We need to create a mail destination that links to our SMTP server.

In this destination we configured following properties:

  • Name: a name for the destination
  • Type: MAIL
  • username and password from your mailtrap SMTP server
  • mail.smtp: smtp server address
  • mail.port: smtp server port
  • mail.from: if there is no mail from specified in the code, we will use this email address to send the mails.

NodeJS application

To send mails, we first have to read this destination properties and then send them to the nodemailer library. To make things a lot easier, I created a new library that will read the destination service and add it to the nodemailer for you.

Your application needs a reference to a destination service to be able to read the mailtrap-destination. this is how my mta.yaml file looks like

ID: sap-cf-mailer-example
_schema-version: "2.1"
description: Example sap cf mailer
version: 0.0.1
  - name: approuter
    type: approuter.nodejs
    path: approuter
      disk-quota: 256M
      memory: 256M
      - name: uaa
      - name: destination_service
      - name: mail_api
        group: destinations
          forwardAuthToken: true
          name: mail_api
          strictSSL: false
          url: '~{url}'
  - name: srv
    type: nodejs
    path: srv
      memory: 512M
      disk-quota: 256M
      - name: mail_api
          url: ${default-url}
      - name: uaa
      - name: destination_service
  - name: destination_service
    type: org.cloudfoundry.existing-service
  - name: uaa
    type: org.cloudfoundry.existing-service


In the srv folder, just install the sap-cf-mailer library with:

$ npm install -s sap-cf-mailer


in your nodeJS application you can use it to send text or HTML emails.

const express = require('express');
const app = express();
const SapCfMailer = require('sap-cf-mailer').default;

const transporter = new SapCfMailer("MAILTRAP");


app.get('/sendmail', async (req, res) => {
    // use sendmail as you should use it in nodemailer
    const result = await transporter.sendMail({
        to: '',
        subject: `This is the mail subject`,
        text: `body of the email`


const port = process.env.PORT || 3000;;
app.listen(port, function () {
    console.log('myapp listening on port ' + port);


The result

After running the application you can check in the mailtrap inbox which email is sent to who!


So, now you can send emails in your nodejs cloud foundry application using the sap-cf-mailer module. For more advanced features like sending html emails and attachments your can consult the nodemailer documentation.




Be the first to leave a comment
You must be Logged on to comment or reply to a post.