Using external services in Demo Notification App
In this example, we will showcase how to use External services with an application that can send mail via SMTP.
You can try the application yourself by following the instructions at project source and deployed application. If you wish to follow along with the tutorial, we recommend cloning the application.
Why external services?
The issue with this application is that the connection information for the e-mail account is not stored in a variable, it's hardcoded in the application.yaml
file.
Imagine we would like to deploy our application to a different environment and indicate sending notifications from that environment by using another account. Or perhaps to connect another application to the same account.
Wouldn't it be much easier to have some kind of object for each account, where the credentials could be accessed with {object.username}
and {object.password}
? And that's exactly what you can do with External services!
Steps
Create an external service
For a step-by-step tutorial on creating external services in CodeNOW, see the External services manual.
The process here consists of two main steps:
- Create a service template called
mail-account
that will hold variablesHOSTNAME
,PORT
,USERNAME
andPASSWORD
. - Create a service based on our template (we will call it
public-gmail-account
) and set its variables to the ones in theapplication.yaml
file.
Connect the external service
Now, we need to:
- Create a new connection to a
mail-account
service. We will name the connectionMAIL
, therefore the variables can be accessed with${MAIL_%VARIABLE-NAME%}
. - Update the
application.yaml
source code. - Build and deploy the application to see if everything is set up correctly.
- in the last step of the deployment process, choose the
public-gmail-account
for ourMAIL
connection
- in the last step of the deployment process, choose the
Connect another account
If you're using Gmail, note that you cannot use the password for your account for third-party-apps. Instead, you need to create a new App password, which you can then pass to our PASSWORD
variable.
- Choose an account that will send the notifications.
- Create a new service based on the
mail-account
template for that account and fill in the variables accordingly. - Deploy the application to another environment, choose the newly created service in the last step.
Project source and deployed application
Project is deployed in CodeNOW and can be accessed with public demo account. Sign in here with the following credentials, if you want to see the application work in action for yourself.
- username: public-user
- password: Public-user123.
This example project can be cloned from here
How to test
You can test the app easily using Swagger:
- Open Swagger endpoint.
- Choose the sendMail method.
- Click on "Try it out".
- Fill in the mail body, subject and to (recipient).