Monday, April 10, 2023

Enable SMTP host and port in AEM Cloud Service

AEM Cloud Service blocks communication to any external host and port by default. As SMTP run on external servers, it need to be allow listed.  Sample SMTP email configuration:

{
"smtp.host": "$[env:AEM_PROXY_HOST;default=proxy.tunnel]",
"smtp.port": 30587,
"smtp.user": "$[env:EMAIL_USERNAME]",
"smtp.password": "$[secret:EMAIL_PASSWORD]",
"from.address": "xxxx@xxxx.com",
"smtp.ssl": false,
"smtp.starttls": true,
"smtp.requiretls":true
}

This article just talks about setting up host and port of SMTP configuration, rest configuration should be done using OSGi configuration file and environment variables.

SMTP User Name and Password should be stored as environment variables as shown below:

SMTP host and port is set using advance networking option which is explained in this article below. Necessary configurations are done through the Advance Networking Options to your cloud service program. Follow below steps to do that: 

Step 1- Create a new project in Adobe I/O - . Inside that add an API integration -> select type as Cloud Manager. 

Step 2- Open the integration and click on Edit Product Profiles. Select appropriate access like below:

Step 3- In left menu, click on "Service Account (JWT). On this screen generate a public private key pair. Download the certificate details.

Step 4- Using this private key, "generate access token" as shown in Step 2.

Step 5- Use this access token to configure Advance Networking Options to your cloud service program and environments.

Step 6- You can use Postman to enable these network settings. The actions are in below sequence:
a. Figure out your server's primary region. Go to Cloud Manager -> Environment Details. This is needed to create primary network. Get region id from this API - API Link.

GET https://cloudmanager.adobe.io/api/program/{program-id}/regions


e.g. region id might be "va7" for East US.

b. Next thing check if Primary Network has been created or not. 

GET https://cloudmanager.adobe.io/api/program/{program-id}/networkInfrastructures

If network infrastructure has been created, you will see response like below:


c. If you don't see primary region network infrastructure in previous step then go ahead and create a new one using this - Create network infrastructure

POST https://cloudmanager.adobe.io/api/program/{program-id}/networkInfrastructures

Your post body should follow below format:

{
"kind": "flexiblePortEgress",
"region": "va7", #Set to value found in step 6#a
"isPrimaryRegion": true
} 

d. First check if advanced networking exists or not for your particular environment using this API.

GET https://cloudmanager.adobe.io/api/program/{program-id}/environment/{environment-id}/advancedNetworking
e. If networkingConfigurations is not configured or you do not see portForwards details in previous step d, then create a networkingConfiguration using Enable Environment Advanced Networking Configuration API.

PUT https://cloudmanager.adobe.io/api/program/{program-id}/environment/{environment-id}/advancedNetworking

Your post body should be in below format. Change smtp host and port values under portForwards as per your SMTP details. Sample Format:
{
"nonProxyHosts": [
"*.my-website.com"
],
"portForwards": [
{
"name": "your-smtp-server.com",
"portDest": 587,
"portOrig": 30587
}
]
}
That's it! Now you can test email functionality in your AEM application. Also, check logs for any further
debugging.

No comments:

Post a Comment

CDN | Clearing Cloudflare cache

In order to clear Cloudflare cache automatically via code, follow below steps: 1. Develop Custom TransportHandler Develop a custom Trans...