'use client'; import React, { useState, useEffect } from 'react'; import { Mail, Send, ShieldCheck, Server, Lock, RefreshCw, CheckCircle2, AlertCircle, Key, } from 'lucide-react'; import { toast } from 'sonner'; import { IntegrationsHeaderNav } from '@/components/integrations/IntegrationsHeaderNav'; import { integrationService } from '@/services/api/integrationService'; export default function EmailIntegrationsPage() { const [creds, setCreds] = useState({ active_provider: 'ZeptoMail', sender_email: '', sender_name: 'iFixKart', smtp_server: '', smtp_port: 587, smtp_username: '', smtp_password: '', use_tls: true, source: 'unconfigured', is_locked_by_config: false, }); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); // Test send state const [testRecipient, setTestRecipient] = useState(''); const [testSubject, setTestSubject] = useState('iFixKart ERP Email Verification Test'); const [testBody, setTestBody] = useState('This is a verified test email sent from iFixKart ERP Email Gateway.'); const [sendingTest, setSendingTest] = useState(false); const fetchEmailConfig = async () => { setLoading(true); try { const data = await integrationService.getEmailCredentials(); setCreds(data); } catch (err: any) { toast.error('Failed to load email settings'); } finally { setLoading(false); } }; useEffect(() => { fetchEmailConfig(); }, []); const handleSaveCredentials = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); try { await integrationService.saveEmailCredentials(creds); toast.success('Email settings saved successfully'); } catch (err: any) { toast.error(err.message || 'Failed to save email settings'); } finally { setSaving(false); } }; const handleSendTest = async (e: React.FormEvent) => { e.preventDefault(); if (!testRecipient.trim() || !testRecipient.includes('@')) { toast.error('Please enter a valid recipient email address'); return; } setSendingTest(true); try { const res = await integrationService.sendTestEmail({ recipient: testRecipient.trim(), subject: testSubject.trim(), body: testBody.trim(), }); if (res.status === 'SUCCESS') { toast.success(`Verification email dispatched to ${testRecipient}!`); } else { toast.error(`Email send failed: ${res.detail || JSON.stringify(res)}`); } } catch (err: any) { toast.error(err.message || 'Failed to send test email'); } finally { setSendingTest(false); } }; return (
{/* Top Banner */}

Transactional Email Gateway (ZeptoMail & SMTP)

Manages automated order confirmations, password reset links, quotation invoices, and staff alerts.

{creds.is_locked_by_config ? ( Active via Config.py ({creds.active_provider}) ) : ( Custom Database SMTP )}
{/* Credentials Form (Cols 1-7) */}

Provider Configuration

Provider: {creds.active_provider}
setCreds({ ...creds, active_provider: e.target.value })} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 disabled:cursor-not-allowed focus:ring-2 focus:ring-sky-500 focus:outline-none" />
setCreds({ ...creds, sender_name: e.target.value })} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 disabled:cursor-not-allowed focus:ring-2 focus:ring-sky-500 focus:outline-none" />
setCreds({ ...creds, sender_email: e.target.value })} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60 disabled:cursor-not-allowed focus:ring-2 focus:ring-sky-500 focus:outline-none" />
{creds.active_provider === 'ZeptoMail' ? (
Zoho ZeptoMail CPaaS Active

ZeptoMail API Token is loaded from production environment variable ZEPTOMAIL_API_KEY. High delivery inbox rate with SSL/TLS encryption.

Endpoint: {creds.endpoint || 'https://api.zeptomail.in/v1.1/email'}
) : (
setCreds({ ...creds, smtp_server: e.target.value })} placeholder="e.g. smtp.gmail.com" className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60" />
setCreds({ ...creds, smtp_port: parseInt(e.target.value) || 587 })} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60" />
setCreds({ ...creds, smtp_username: e.target.value })} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60" />
setCreds({ ...creds, smtp_password: e.target.value })} placeholder="••••••••••••" className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white disabled:opacity-60" />
)} {!creds.is_locked_by_config && ( )}
{/* Live Test Email Sender (Cols 8-12) */}

Live Test Email Dispatch

Sends an immediate verification email to verify SMTP handshake or ZeptoMail CPaaS API delivery.

setTestRecipient(e.target.value)} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-sky-500 focus:outline-none" />
setTestSubject(e.target.value)} className="w-full px-3 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-900 dark:text-white focus:ring-2 focus:ring-sky-500 focus:outline-none" />