{
  "name": "Email Task - Simple",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "email-task",
        "responseMode": "responseNode"
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ],
      "id": "webhook-node"
    },
    {
      "parameters": {
        "model": "gpt-3.5-turbo",
        "messages": {
          "messageType": "multipleMessages",
          "messages": [
            {
              "role": "system",
              "message": "You are a professional email assistant. Generate emails in JSON format with 'subject' and 'body' fields only. Example: {\"subject\": \"Meeting Request\", \"body\": \"Dear Recipient,\\n\\nContent...\\n\\nBest regards\"}"
            },
            {
              "role": "user",
              "message": "=Create an email for {{ $json.recipientEmail }} about: {{ $json.content }}. Tone: {{ $json.tone || 'professional' }}. From: {{ $json.senderName || 'User' }}. Return JSON with subject and body fields only."
            }
          ]
        },
        "options": {
          "temperature": 0.7,
          "maxTokens": 1000
        }
      },
      "name": "OpenAI",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [
        460,
        300
      ],
      "id": "openai-node"
    },
    {
      "parameters": {
        "jsCode": "// Process OpenAI response\nconst webhookData = $('Webhook').first().json;\nconst aiResponse = $input.first().json;\n\nlet emailData;\ntry {\n  const content = aiResponse.choices[0].message.content.trim();\n  \n  // Try to parse JSON\n  try {\n    emailData = JSON.parse(content);\n  } catch {\n    // Fallback parsing\n    const lines = content.split('\\n').filter(l => l.trim());\n    const subject = lines[0]?.replace(/.*subject[:\\s]*/i, '') || 'Email';\n    const body = lines.slice(1).join('\\n') || webhookData.content;\n    emailData = { subject, body };\n  }\n  \n  return {\n    success: true,\n    recipientEmail: webhookData.recipientEmail,\n    subject: emailData.subject.replace(/[\"']/g, ''),\n    body: emailData.body.replace(/\\\\n/g, '\\n'),\n    senderName: webhookData.senderName || 'User',\n    timestamp: new Date().toISOString(),\n    executionId: 'email_' + Date.now()\n  };\n  \n} catch (error) {\n  return {\n    success: false,\n    error: 'Failed to process: ' + error.message,\n    recipientEmail: webhookData.recipientEmail,\n    timestamp: new Date().toISOString()\n  };\n}"
      },
      "name": "Process Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        680,
        300
      ],
      "id": "process-node"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": {{ $json.success }},\n  \"data\": {\n    \"id\": \"{{ $json.executionId }}\",\n    \"recipient\": \"{{ $json.recipientEmail }}\",\n    \"subject\": \"{{ $json.subject }}\",\n    \"body\": \"{{ $json.body }}\",\n    \"timestamp\": \"{{ $json.timestamp }}\",\n    \"message\": \"Email content generated successfully\"\n  }\n}"
      },
      "name": "Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        900,
        300
      ],
      "id": "response-node"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "Process Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Response": {
      "main": [
        [
          {
            "node": "Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "timezone": "Asia/Kolkata"
  },
  "versionId": "1",
  "id": "email-simple",
  "tags": [
    "email",
    "ai",
    "simple"
  ]
}