How to Send Email Using Twilio SendGrid API in Oracle Integration Cloud (OIC)
Use Case
You want to send transactional or notification emails from an OIC integration using SendGrid, a cloud-based email service by Twilio. This method is REST-based, reliable, and gives full control over the email content and delivery.
Why we should opt for Twilio sendgrid over traditional OIC built-in SMTP mail.
- Rich HTML & Attachments: SendGrid supports advanced HTML emails, attachments, and templates; OIC SMTP is basic.
- Email Tracking: SendGrid gives open/click stats; OIC SMTP has no tracking.
- High Deliverability: SendGrid ensures better inbox delivery; SMTP can be throttled or blocked.
- Secure Auth: SendGrid uses API keys; SMTP often uses plain credentials.
- Scalability: SendGrid handles bulk emails; SMTP suits low volume.
- Use SMTP for simple, internal alerts.
- Use SendGrid for professional, scalable, and trackable emails.
Solution Steps
1. Get Your SendGrid API Key
- Log in to SendGrid Dashboard.
- Go to Settings > API Keys > Create API Key.
- Choose “Full Access” or customize scopes as needed.
- Copy and save the API key securely.
2. Create a REST Connection in OIC for SendGrid
- Go to Connections > Create in OIC.
- Choose REST Adapter.
- Enter a name like
SendGrid_REST_API
. - Set Connection Type: Trigger and Invoke.
- In Connection Properties:
- Configure URL:
https://api.sendgrid.com
- Configure URL:
- Under Security:
- Security Policy: API key based authentication
- Provide api key
- Test and save the connection.
3. Use the Connection in Your Integration
- Create an App-Driven or Scheduled Integration.
- Drag the SendGrid_REST_API as an Invoke.
- Choose operation:
POST
- Resource Path:
/v3/mail/send
- Request Media Type:
application/json or text/html
4. Map Your Email Payload
Sample JSON:
{
"personalizations": [
{
"to": [
{
"email": "test@test1.com,test1@test2.com",
"name": "Test"
}
],
"cc": [
{
"email": "test@test.com",
"name": "test"
}
]
}
],
"from": {
"email": "test@test.com",
"name": "Example Order Confirmation"
},
"subject": "Your Example Order Confirmation",
"content": [
{
"type": "text/html",
"value": "
<p>Hello from Twilio SendGrid!</p>
<p>Sending with the email service trusted by developers and marketers for
<strong>time-savings</strong>,
<strong>scalability</strong>,
and <strong>delivery expertise</strong>.
</p><p>%open-track%</p>"
}
],
"attachments": [
{
"content": "Base64 content",
"filename": "index.html",
"type": "text/html",
"disposition": "attachment"
}
],
"categories": ["cake", "pie", "baking"]
}
Map values dynamically if needed using XSLT mapper.
5. Test the Integration
- Activate and run the integration.
- Check your SendGrid dashboard for delivery status.
In our integration, if no files are found while listing from the SFTP location, an error is thrown and caught by the global fault handler. From there, we invoke a common SendGrid email service to send the error details via email.