Skip to Content
0
Feb 08, 2022 at 08:18 PM

Send Email via SMTP On Premise Server in SAP CAP - Cloud Foundry

1708 Views

Hello SAP Community!

  • We are trying to use a SMTP Server that is on-premise , we have configured our SMTP Server as a MAIL in our destinations in SAP BTP Cockpit as you can see in this image:

destination.png

We were trying to use the "sap-cf-mailer" library , we tried an example and it worked fine.

Link: Example that we've used.

Everything was pretty good until we've found that On-Premise SMTP servers are not supported by this library (yet).

We noticed it because CAP error log throwed this message:

microsoftteams-image-7.png

Now , our question is . Is there any workaround , way , or a blog that explains how to send emails from CAP via consuming an OnPremise SAP BTP Destination?

Best regards, Rodrigo

PD:

Below is our code that we use in our CAP App:

Package.json

{
    "name": "smtp_",
    "version": "1.0.0",
    "description": "A simple CAP project.",
    "repository": "<Add your repository here>",
    "license": "UNLICENSED",
    "private": true,
    "dependencies": {
        "@sap/cds": "^5",
        "express": "^4",
        "hdb": "^0.18.3",
        "sap-cf-mailer": "^0.0.5"
    },
    "devDependencies": {
        "sqlite3": "^5.0.2"
    },
    "scripts": {
        "start": "cds run"
    },
    "eslintConfig": {
        "extends": "eslint:recommended",
        "env": {
            "es2020": true,
            "node": true,
            "jest": true,
            "mocha": true
        },
        "globals": {
            "SELECT": true,
            "INSERT": true,
            "UPDATE": true,
            "DELETE": true,
            "CREATE": true,
            "DROP": true,
            "CDL": true,
            "CQL": true,
            "CXL": true,
            "cds": true
        },
        "rules": {
            "no-console": "off",
            "require-atomic-updates": "off"
        }
    },
    "cds": {
        "requires": {
            "db": {
                "kind": "sql"
            }
        },
        "hana": {
            "deploy-format": "hdbtable"
        }
    }
}
MTA.yaml
## Generated mta.yaml based on template version 0.4.0
## appName = smtp_
## language=nodejs; multitenant=false
## approuter=
_schema-version: '3.1'
ID: smtp_
version: 1.0.0
description: "A simple CAP project."
parameters:
  enable-parallel-deployments: true
build-parameters:
  before-all:
   - builder: custom
     commands:
      - npm ci
      - npx -p @sap/cds-dk cds build --production
modules:
 # --------------------- SERVER MODULE ------------------------
 - name: smtp_-srv
 # ------------------------------------------------------------
   type: nodejs
   path: gen/srv
   parameters:
     buildpack: nodejs_buildpack
   build-parameters:
     builder: npm-ci
     memory: 512M
     disk-quota: 1024M
   requires:
    # Resources extracted from CAP configuration
    - name: smtp_-db
    - name: demo_connectivity
    - name: demo-destination
    - name: demo-uaa
   provides:
    - name: srv-api      # required by consumers of CAP services (e.g. approuter)
      properties:
        srv-url: ${default-url}
 # -------------------- SIDECAR MODULE ------------------------
 - name: smtp_-db-deployer
 # ------------------------------------------------------------
   type: hdb
   path: gen/db  
   parameters:
     buildpack: nodejs_buildpack
   requires:
    # 'hana' and 'xsuaa' resources extracted from CAP configuration
    - name: smtp_-db
    - name: demo-uaa
resources:
 # services extracted from CAP configuration
 # 'service-plan' can be configured via 'cds.requires.<name>.vcap.plan'
# ------------------------------------------------------------
 - name: smtp_-db
# ------------------------------------------------------------
   type: com.sap.xs.hdi-container
   parameters:
     service: hana  # or 'hanatrial' on trial landscapes
     service-plan: hdi-shared
   properties:
     hdi-service-name: ${service-name}
 - name: demo-uaa
   type: org.cloudfoundry.existing-service
   parameters:
     service: xsuaa
     service-plan: application 
 - name: demo-destination
   parameters:
     service-plan: lite
     service: destination
   type: org.cloudfoundry.existing-service
 - name: demo_connectivity
   type: org.cloudfoundry.existing-service
SERVICE.JS
const cds = require("@sap/cds");
const SapCfMailer = require('sap-cf-mailer').default;
const transporter = new SapCfMailer("SMTP");
module.exports = cds.service.impl(async (srv) => {
    srv.on("sendmail", async (req) => {
        // use sendmail as you should use it in nodemailer
        const result = await transporter.sendMail({
          to: 'some@mail.com',
          subject: `This is the mail subject`,
          text: `body of the email`
        });
        return JSON.stringify(result);
      });
});
SERVICE.cds
service smtp {
    action sendmail() returns String;
}

Attachments

destination.png (25.9 kB)