Using Email connector with Wso2 MI

  1. Create a simple Rest API to get XML payload as input
  2. Extract some information from XML (like id, name …)
  3. Send an email via WSO2 Email Connector with the above extracted information — use gmail
  4. Response back once email is sent
  • Create a simple Rest API to get XML payload as input
<?xml version="1.0" encoding="UTF-8"?>
<api context="/emailconnector" name="EmailConnector.xml" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/send">
<inSequence>
<log level="custom">
<property name="STATUS" value="****extracting information*****"/>
</log>
<log level="full"/>
  • Extract the data sent received from the post request and assign the value to a property
<property expression="$body/Customers/CustomerID" name="CustomerID"
scope="default" type="STRING"/>
<property expression="$body/Customers/CompanyName" name="CompanyName"
scope="default" type="STRING"/>
<property expression="$body/Customers/ContactName" name="ContactName"
scope="default" type="STRING"/>
<property expression="$body/Customers/ContactTitle" name="ContactTitle"
scope="default" type="STRING"/>
<property expression="$body/Customers/AddressLine1" name="AddressLine1"
scope="default" type="STRING"/>
<property expression="$body/Customers/AddressLine2" name="AddressLine2"
scope="default" type="STRING"/>
<property expression="$body/Customers/City" name="City"
scope="default" type="STRING"/>
<property expression="$body/Customers/PostalCode" name="PostalCode"
scope="default" type="STRING"/>
<property expression="$body/Customers/Country" name="Country"
scope="default" type="STRING"/>
<property expression="$body/Customers/Phone" name="Phone"
scope="default" type="STRING"/>
<property expression="$body/Customers/Fax" name="Fax"
scope="default" type="STRING"/>
  • Use the script mediator to prepare the content to be included in the email
<script description="" language="js">
<![CDATA[var CustomerID =mc.getProperty('CustomerID');
var CompanyName=mc.getProperty('CompanyName');
var ContactName=mc.getProperty('ContactName');
var ContactTitle=mc.getProperty('ContactTitle');
var AddressLine1=mc.getProperty('AddressLine1');
var AddressLine2=mc.getProperty('AddressLine2');
var City=mc.getProperty('City');
var PostalCode=mc.getProperty('PostalCode');
var Country=mc.getProperty('Country');
var Phone=mc.getProperty('Phone');
var Fax=mc.getProperty('Fax');
mc.setPayloadXML(<ns:text xmlns:ns="http://ws.apache.org/commons/ns/payload">
Dear User,
Following are the details

CustomerID : [{CustomerID}]
CompanyName : [{CompanyName}]
ContactName : [{ContactName}]
ContactTitle : [{ContactTitle}]
AddressLine1 : [{AddressLine1}]
AddressLine2 : [{AddressLine2}]
City : [{City}]
PostalCode : [{PostalCode}]
Country : [{Country}]
Phone : [{Phone}]
Fax : [{Fax}]

Yours Truly,
ESB Support Team</ns:text>);]]></script>
<property expression="$body//m0:text" name="content" scope="default" type="STRING" xmlns:m0="http://ws.apache.org/commons/ns/payload" xmlns:ns="http://org.apache.synapse/xsd"/>
<log level="custom">
<property name="STATUS" value="****emailconnector started*****"/>
</log>
<email.send configKey="smtpsconnection">
<from>tt277489@gmail.com</from>
<to>randini.senanayake@gmail.com</to>
<subject>Customer Details</subject>
<content>{$ctx:content}</content>
<contentType>text/plain</contentType>
</email.send>
<log level="full"/>
<log level="custom">
<property name="STATUS" value="****emailconnector ended*****"/>
</log>
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="smtpsconnection" xmlns="http://ws.apache.org/ns/synapse">
<email.init>
<connectionType>SMTPS</connectionType>
<port>465</port>
<host>smtp.gmail.com</host>
<username>tt2774891@gmail.com</username>
<password>xxxxxxx</password>
<name>smtpsconnection</name>
</email.init>
</localEntry>
curl -H "Content-Type: text/xml" --request POST --data @data.xml http://localhost:8290/emailconnector/send -vv
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8290 (#0)
> POST /emailconnector/send HTTP/1.1
> Host: localhost:8290
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Type: text/xml
> Content-Length: 455
>
* upload completely sent off: 455 out of 455 bytes
< HTTP/1.1 200 OK
< Accept: */*
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST
< Host: localhost:8290
< Access-Control-Allow-Headers: content-type
< Content-Type: text/xml; charset=UTF-8
< Date: Fri, 19 Feb 2021 05:11:32 GMT
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
<result><success>true</success></result>
<Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<AddressLine1>Obere Str. 57</AddressLine1>
<AddressLine2>Greg road</AddressLine2>
<City>Berlin</City>
<Region></Region>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
  1. Go to your (Google Account).
  2. On the left navigation panel, click Security.
  3. On the bottom of the page, in the Less secure app access panel, click Turn on access.
  4. If you don’t see this setting, your administrator might have turned off less secure app account access (check the instruction above).
  5. Click the Save button.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/emailconnector" name="EmailConnector.xml" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/send">
<inSequence>
<log level="custom">
<property name="STATUS" value="****extracting information*****"/>
</log>
<log level="full"/>
<property expression="$body/Customers/CustomerID" name="CustomerID"
scope="default" type="STRING"/>
<property expression="$body/Customers/CompanyName" name="CompanyName"
scope="default" type="STRING"/>
<property expression="$body/Customers/ContactName" name="ContactName"
scope="default" type="STRING"/>
<property expression="$body/Customers/ContactTitle" name="ContactTitle"
scope="default" type="STRING"/>
<property expression="$body/Customers/AddressLine1" name="AddressLine1"
scope="default" type="STRING"/>
<property expression="$body/Customers/AddressLine2" name="AddressLine2"
scope="default" type="STRING"/>
<property expression="$body/Customers/City" name="City"
scope="default" type="STRING"/>
<property expression="$body/Customers/PostalCode" name="PostalCode"
scope="default" type="STRING"/>
<property expression="$body/Customers/Country" name="Country"
scope="default" type="STRING"/>
<property expression="$body/Customers/Phone" name="Phone"
scope="default" type="STRING"/>
<property expression="$body/Customers/Fax" name="Fax"
scope="default" type="STRING"/>

<script description="" language="js">
<![CDATA[var CustomerID =mc.getProperty('CustomerID');
var CompanyName=mc.getProperty('CompanyName');
var ContactName=mc.getProperty('ContactName');
var ContactTitle=mc.getProperty('ContactTitle');
var AddressLine1=mc.getProperty('AddressLine1');
var AddressLine2=mc.getProperty('AddressLine2');
var City=mc.getProperty('City');
var PostalCode=mc.getProperty('PostalCode');
var Country=mc.getProperty('Country');
var Phone=mc.getProperty('Phone');
var Fax=mc.getProperty('Fax');
mc.setPayloadXML(<ns:text xmlns:ns="http://ws.apache.org/commons/ns/payload">
Dear User,
Following are the details

CustomerID : [{CustomerID}]
CompanyName : [{CompanyName}]
ContactName : [{ContactName}]
ContactTitle : [{ContactTitle}]
AddressLine1 : [{AddressLine1}]
AddressLine2 : [{AddressLine2}]
City : [{City}]
PostalCode : [{PostalCode}]
Country : [{Country}]
Phone : [{Phone}]
Fax : [{Fax}]

Yours Truly,
ESB Support Team</ns:text>);]]></script>
<property expression="$body//m0:text" name="content" scope="default" type="STRING" xmlns:m0="http://ws.apache.org/commons/ns/payload" xmlns:ns="http://org.apache.synapse/xsd"/>
<log level="custom">
<property name="STATUS" value="****emailconnector started*****"/>
</log>
<email.send configKey="smtpsconnection">
<from>tt2774891@gmail.com</from>
<to>randini.aathmaja@gmail.com</to>
<subject>Customer Details</subject>
<content>{$ctx:content}</content>
<contentType>text/plain</contentType>
</email.send>
<log level="full"/>
<log level="custom">
<property name="STATUS" value="****emailconnector ended*****"/>
</log>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store