/** * Resend e-posta gönderimi — bağımlılık yok (Node 18+ built-in fetch). * * Env: * RESEND_API_KEY — Resend API anahtarı (re_...). Yoksa e-posta kapalıdır. * EMAIL_FROM — Gönderen adres. Üretimde Resend'de DNS (SPF/DKIM) ile * doğrulanmış kendi domaininiz olmalı. Doğrulanana kadar * Resend'in paylaşımlı test adresi kullanılır. * * Kaynak/Endpoint: POST https://api.resend.com/emails (Bearer auth) */ const RESEND_ENDPOINT = 'https://api.resend.com/emails'; export function emailConfigured() { return Boolean(process.env.RESEND_API_KEY); } export function emailFrom() { return process.env.EMAIL_FROM || 'İhaleist '; } export async function sendEmail({ to, subject, html, text }) { const key = process.env.RESEND_API_KEY; if (!key) throw new Error('E-posta yapılandırılmamış (RESEND_API_KEY yok)'); if (!to || !subject || (!html && !text)) throw new Error('Eksik alan: to, subject ve html/text gerekli'); const res = await fetch(RESEND_ENDPOINT, { method: 'POST', headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ from: emailFrom(), to: Array.isArray(to) ? to : [to], subject, html, text }), }); const data = await res.json().catch(() => ({})); if (!res.ok) { throw new Error(data?.message || `Resend hatası: HTTP ${res.status}`); } return { id: data.id }; } // İhale bildirimi için basit, markasız HTML gövdesi export function tenderDigestHtml({ name, tenders }) { const rows = (tenders || []) .slice(0, 15) .map( (t) => ` ${escapeHtml(t.baslik || '')} ${escapeHtml(t.sehir || '')} ${escapeHtml(t.tarih || '')} ` ) .join(''); return `

İhaleist

Merhaba ${escapeHtml(name || '')}, takip kriterlerinize uyan ihaleler:

${rows || ''}
İhale İl Tarih
Yeni ihale yok.

Bu e-posta İhaleist tarafından gönderildi. Bildirim tercihlerinizi uygulama ayarlarından değiştirebilirsiniz.

`; } function escapeHtml(s) { return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); }