Skip to main content

Overview

The COM (Communications) service manages email and notification delivery for the HERCULES SGI system:
  • Email Management - Create, send, and track emails
  • Template Support - Email templates with parameter substitution
  • Attachments - Support for file attachments
  • Recipients - Multiple recipient types (To, CC, BCC)
  • Status Tracking - Monitor email delivery status
Base URL: /emails

Authentication

The COM service is primarily used by other SGI services for sending notifications. Direct access may be restricted to system administrators and authorized applications.

Emails

List Emails

curl -X GET "http://localhost:4280/emails?q=subject=like='Research%'" \
  -H "Authorization: Bearer {token}"
Returns a paginated list of emails.
q
string
RSQL filter query (e.g., subject=like='Grant%';sent==true)
s
string
Sort specification (e.g., createdDate,desc)
page
integer
default:"0"
Page number (zero-based)
size
integer
default:"10"
Items per page
content
array
Array of Email objects

Get Email by ID

curl -X GET "http://localhost:4280/emails/123" \
  -H "Authorization: Bearer {token}"
id
long
required
Email identifier
Returns complete email details including recipients, attachments, and parameters.

Create Email

curl -X POST "http://localhost:4280/emails" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "subject": "Grant Application Approved",
    "content": "<p>Your application has been approved.</p>",
    "contentType": "HTML",
    "recipients": [
      {
        "address": "[email protected]",
        "name": "Dr. Jane Smith",
        "type": "TO"
      }
    ],
    "attachments": [],
    "params": [
      {
        "name": "APPLICATION_ID",
        "value": "APP-2024-001"
      }
    ]
  }'
Request Body:
subject
string
required
Email subject line
content
string
required
Email body content (HTML or plain text)
contentType
string
default:"HTML"
Content type: HTML or TEXT
recipients
array
required
Array of recipient objects
attachments
array
Array of attachment objects
params
array
Template parameters for variable substitution
Response: Returns the created Email object with HTTP 201 status.

Update Email

curl -X PUT "http://localhost:4280/emails/123" \
  -H "Authorization: Bearer {token}" \
  -H "Content-Type: application/json" \
  -d '{
    "subject": "Updated Subject",
    "content": "Updated content",
    "recipients": [...]
  }'
Updates an existing email. Note: Only unsent emails can be updated.
id
long
required
Email identifier

Delete Email

curl -X DELETE "http://localhost:4280/emails/123" \
  -H "Authorization: Bearer {token}"
Deletes an email record.
id
long
required
Email identifier
Response: HTTP 204 No Content

Send Email

curl -X GET "http://localhost:4280/emails/123/send" \
  -H "Authorization: Bearer {token}"
Sends a previously created email.
id
long
required
Email identifier
Response:
status
string
Sending status: SUCCESS, FAILED, or PENDING
message
string
Status message or error description
timestamp
datetime
Timestamp of the send operation
{
  "status": "SUCCESS",
  "message": "Email sent successfully",
  "timestamp": "2024-03-05T10:30:00Z"
}

Email Templates

The COM service supports template-based emails with parameter substitution.

Using Templates

Templates are managed separately through the EmailTplController. To use a template:
  1. Create an email referencing a template
  2. Provide parameters that match template variables
  3. The service will substitute parameters in the template content
{
  "templateRef": "GRANT_APPROVAL",
  "recipients": [
    {
      "address": "[email protected]",
      "type": "TO"
    }
  ],
  "params": [
    {
      "name": "RESEARCHER_NAME",
      "value": "Dr. Jane Smith"
    },
    {
      "name": "GRANT_TITLE",
      "value": "Novel Cancer Research"
    },
    {
      "name": "AMOUNT",
      "value": "€50,000"
    }
  ]
}

Common Use Cases

Sending Notification Email

const email = {
  subject: 'Application Status Update',
  content: '<p>Your application status has changed to: <strong>Approved</strong></p>',
  contentType: 'HTML',
  recipients: [
    {
      address: '[email protected]',
      name: 'John Doe',
      type: 'TO'
    }
  ],
  params: [
    { name: 'APPLICATION_ID', value: 'APP-2024-001' },
    { name: 'NEW_STATUS', value: 'Approved' }
  ]
};

// Create email
const response = await fetch('http://localhost:4280/emails', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(email)
});

const created = await response.json();

// Send email
const sendResponse = await fetch(`http://localhost:4280/emails/${created.id}/send`, {
  headers: { 'Authorization': `Bearer ${token}` }
});

const status = await sendResponse.json();
console.log('Send status:', status);

Sending Email with Attachment

import base64
import requests

# Read PDF file and encode
with open('document.pdf', 'rb') as f:
    pdf_content = base64.b64encode(f.read()).decode('utf-8')

email = {
    'subject': 'Grant Agreement Document',
    'content': 'Please find attached your grant agreement.',
    'contentType': 'TEXT',
    'recipients': [
        {
            'address': '[email protected]',
            'type': 'TO'
        }
    ],
    'attachments': [
        {
            'name': 'grant-agreement.pdf',
            'content': pdf_content,
            'contentType': 'application/pdf'
        }
    ]
}

# Create and send
response = requests.post(
    'http://localhost:4280/emails',
    json=email,
    headers={'Authorization': f'Bearer {token}'}
)

email_id = response.json()['id']

# Send it
send_response = requests.get(
    f'http://localhost:4280/emails/{email_id}/send',
    headers={'Authorization': f'Bearer {token}'}
)

print(send_response.json())

Error Handling

Common Error Responses

400 Bad Request
Invalid email data (missing required fields, invalid email addresses)
404 Not Found
Email with specified ID does not exist
409 Conflict
Cannot modify email that has already been sent
500 Internal Server Error
Email sending failed due to SMTP server error

Validation Rules

  • Email addresses must be valid format
  • At least one TO recipient is required
  • Subject cannot be empty
  • Content cannot be empty
  • Attachment content must be valid Base64
  • Template parameters must match template requirements

Best Practices

  1. Test emails first - Use test email addresses during development
  2. Validate recipients - Ensure email addresses are valid before creating emails
  3. Use templates - Leverage templates for consistent formatting and easier maintenance
  4. Monitor status - Check send status after sending to handle failures
  5. Handle attachments carefully - Large attachments may cause timeouts or failures
  6. Limit recipients - Avoid sending to too many recipients in a single email

Configuration

The COM service requires SMTP server configuration (typically done by system administrators):
  • SMTP host and port
  • Authentication credentials
  • SSL/TLS settings
  • Default sender address
  • Email templates location

Build docs developers (and LLMs) love