#!/bin/bash
EMAIL="$1"
SUBJECT="$2"
BODY="$3"
OUTRIGHT_USERNAME="outrightcrm19@gmail.com"
OUTRIGHT_PASSWORD="whsbchnnqizapajq"
EMAIL_API_URL="https://crm.outrightsystems.org/index.php?entryPoint=send_email&api_key=hr8jobGGDGXCanKKZuRSKfQAiHkhyHEyOZO9r"
if [[ -z "$EMAIL" || -z "$SUBJECT" || -z "$BODY" ]]; then
    echo "Error: Missing email parameters.  $EMAIL $SUBJECT $BODY"
    exit 1
fi

# Email JSON Payload
EMAIL_JSON=$(jq -n \
    --arg recipients "$EMAIL" \
    --arg subject "$SUBJECT" \
    --arg body "$BODY" \
    --arg username "$OUTRIGHT_USERNAME" \
    --arg password "$OUTRIGHT_PASSWORD" \
    '{
        recipients: $recipients,
        subject: $subject,
        body: $body,
        attachments: [],
        username: $username,
        password: $password
    }')

# Debugging: Print JSON
echo "Generated JSON Payload:"
echo "$EMAIL_JSON"

# Send email and capture response
RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d "$EMAIL_JSON" "$EMAIL_API_URL")

# Debugging: Print API response
echo "Email API Response: $RESPONSE"

if [[ "$RESPONSE" == *"success"* ]]; then
    echo "Email sent successfully to $EMAIL"
else
    echo "Failed to send email. Response: $RESPONSE"
fi

