Skip to Content
Technical Articles

Most Easiest way to design and send mail from SAP CAI, SAP S/4HANA & SAP CP

Hello everyone,

In this blog i am going to share one of my experience “Most easiest way to design and send mail from SAP CAI, S/4HANA, SAP Cloud Platform”.

 

You must wondering how we get fancy designed automatic mail from any platform like facebook, gmail, sap community etc whenever we register or we ask for resetting password or some time welcome mail. As a developer you also must be thinking how you can design and integrate such feature in your application. Well the techniques i am going to show you here is working in any kind of business mail service provider (E.G Outlook, Yahoo business).

Well as a application developer when it comes to developing this feature, developer has to go through lots of hassle. To make hassle free life modern developer would always looks for rest api or sdk to integrate this feature in any kind of application. But getting a platform which support all type of mail service provider and at the same time providing rest api or sdk to integrate is really big deal. On top of that designing template is also not easy task as it required html and css knowledge.

 

So How we are going to make developer’s life hassle free?

Answer is very simple Mailjet. Discussing on mailjet doesn’t make any sense because everyone is smart enough ask this to google daddy. To make it crisp and clear it is a platform which gives you drag and drop functionality to design fancy mail template and at the same time it provides API or sdk to integrate sending mail feature in your application with very very minimal configuration.

 

Well there could be n number of business case, to make clear understanding of usage and having a use case lineup with SAP CAI i thought to put below business story.

Business Story: Bestrun is US based company design and sell enterprise software. New Castle Logistics is customer of Bestrun who uses their product and looking for particular feature which is available in their competitor’s product. Unfortunately in the Bestrun’s website there is no update on that particular feature availability in specific release. In this situation New Castle logistics use SAP Conversational AI based smart assistant of Bestrun to know about this and smart assistant bot help New Castle logistics to connect with product owner by sending an automatic mail from SAP CAI. Getting right contact for particular situation add lots of value and save lots of time and make process simpler isn’t it. Customer’s smiling face say this 🙂

 

Well lots of business story, lets get into our main business which is how we will do this. lets make step by step approach.

Step 1: Create an account in Mailjet using your Business Mail or any mail with business domain from where you want to send mail. (e.g noreply@businessdomain.com). On successful registration you will get below automatic activation mail from Mailjet to activate your account.

Step 2: Configuring Mail Sending Method

Select API to use Rest API features

Select Curl for HTTP

Also there are below wrappers or sdk available if you don’t want to use rest API

Validate sender type and address

Step 3: Designing Mail Template using Drag and Drop Features

Go to Dashboard and Create new Transactional template

There will lots of Pre-Designed template which you can edit

 

In your left use template designer to design or edit your template, all options are pretty much self explanatory.

Define your subject and from name

Define all the variables (e.g name, purpose etc) in your template which you will be passing from application in rest api call or any webhook call.

then save and publish.

Step 4: Getting rest API details and API key

On successful publication it will show you the curl payload and url. It also provide the sdk code to implement in different languages.

For Api key go to account and Click on Rest API

Above API key you have to pass as basic authentication during making HTTP call.

Step 5: Integrating this as Webhook for Skill fulfillment in SAP CAI

Well i am not going to show here how to design bot (intent, skill etc), there are lots of blog and you can can follow CAI tutorial also for that. Here i am directly going to show the parameter and url in Webhook.

Let’s have a look at conversation with bot

And Finally in my mailbox it has sent me mail.

Sending mail from S/4HANA System

Well in SAP API hub (api.sap.com) there are multiple example how to call external rest api from abap, still i am attaching one.

DATA: lo_http_client TYPE REF TO if_http_client.
DATA: response TYPE string.

"create HTTP client by url
"API endpoint for API sandbox 
CALL METHOD cl_http_client=>create_by_url
  EXPORTING
    url                = 'https://api.mailjet.com/v3.1/send'
"API endpoint with optional query parameters
    "url                = 'https://sandbox.api.sap.com/concur/api/v3.0/expense/quickexpenses'

  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    OTHERS             = 4.



IF sy-subrc <> 0.
  "error handling
ENDIF.

"setting request method
lo_http_client->request->set_method('POST').

"adding headers
lo_http_client->request->set_header_field( name = 'Content-Type' value = 'application/json' ).
lo_http_client->request->set_header_field( name = 'Accept' value = 'application/json' ).
"API Key for API Sandbox
lo_http_client->request->set_header_field( name = 'Authorization' value = 'Basic Base64 of Apikey:Secret' ).


CALL METHOD lo_http_client->request->set_cdata
  EXPORTING
  data = '{ "Messages":[ { "From": { "Email": "name@businessdomain.com", "Name": "Sarah - An intelligent Assistant" }, "To": [ { "Email": "anothermail@businessdomain.com", "Name": "samplename" } ], "TemplateID": 1040152, "TemplateLanguage": true, "Subject": "Requesting for Information on Product ABC", "Variables": { "name": "Sudip" } } ] }'.


CALL METHOD lo_http_client->send
  EXCEPTIONS
    http_communication_failure = 1
    http_invalid_state         = 2
    http_processing_failed     = 3
    http_invalid_timeout       = 4
    OTHERS                     = 5.

IF sy-subrc = 0.
  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 5.
ENDIF.

IF sy-subrc <> 0.
  "error handling
ENDIF.

response = lo_http_client->response->get_cdata( ).

WRITE: 'response: ', response.

 

Sending Mail from SAP Cloud Platform application is pretty easy you can use node.js sdk or you can  make ajax call from ui5 application. There are n number of articles you will get how to make ajax call for rest api.

 

Conclusion: To me it was the easiest way to design and integrate mail in any application, it may differ person to person. I want to know if you have any other easiest approach let me know in comment section. you can also ask me question in this and also let me know your feedback.

 

Regards,

Sudip

2 Comments
You must be Logged on to comment or reply to a post.