revert: restore original mail.ts and admin email form
Reverted all changes to lib/mail.ts and the email section of admin-settings-form.tsx. The original Resend code was working fine; my "fixes" for the from field broke it. Restored the exact original code that was functional. Kept: auto-tagging fix (getTagsProvider), language detection, revalidateTag fix, debug logging, docker-compose fix, setup wizard. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -277,8 +277,6 @@ export function AdminSettingsForm({ config }: { config: Record<string, string> }
|
||||
if (emailProvider === 'resend') {
|
||||
const key = formData.get('RESEND_API_KEY') as string
|
||||
if (key) data.RESEND_API_KEY = key
|
||||
const from = formData.get('SMTP_FROM') as string
|
||||
if (from) data.SMTP_FROM = from
|
||||
} else {
|
||||
data.SMTP_HOST = formData.get('SMTP_HOST') as string
|
||||
data.SMTP_PORT = formData.get('SMTP_PORT') as string
|
||||
@@ -302,32 +300,6 @@ export function AdminSettingsForm({ config }: { config: Record<string, string> }
|
||||
const handleTestEmail = async () => {
|
||||
setIsTesting(true)
|
||||
try {
|
||||
// Save email config to DB first so test uses the latest values
|
||||
const emailForm = document.querySelector('form[name="email-form"]') as HTMLFormElement
|
||||
if (emailForm) {
|
||||
const formData = new FormData(emailForm)
|
||||
const saveData: Record<string, string> = { EMAIL_PROVIDER: emailProvider }
|
||||
if (emailProvider === 'resend') {
|
||||
const key = formData.get('RESEND_API_KEY') as string
|
||||
if (key) saveData.RESEND_API_KEY = key
|
||||
const from = formData.get('SMTP_FROM') as string
|
||||
if (from) saveData.SMTP_FROM = from
|
||||
} else {
|
||||
saveData.SMTP_HOST = formData.get('SMTP_HOST') as string
|
||||
saveData.SMTP_PORT = formData.get('SMTP_PORT') as string
|
||||
saveData.SMTP_USER = formData.get('SMTP_USER') as string
|
||||
saveData.SMTP_PASS = formData.get('SMTP_PASS') as string
|
||||
saveData.SMTP_FROM = formData.get('SMTP_FROM') as string
|
||||
saveData.SMTP_IGNORE_CERT = smtpIgnoreCert ? 'true' : 'false'
|
||||
saveData.SMTP_SECURE = smtpSecure ? 'true' : 'false'
|
||||
}
|
||||
const saveResult = await updateSystemConfig(saveData)
|
||||
if (saveResult.error) {
|
||||
toast.error('Failed to save settings before testing: ' + saveResult.error)
|
||||
setIsTesting(false)
|
||||
return
|
||||
}
|
||||
}
|
||||
const result: any = await testEmail(emailProvider)
|
||||
if (result.success) {
|
||||
toast.success(t('admin.smtp.testSuccess'))
|
||||
@@ -920,7 +892,7 @@ export function AdminSettingsForm({ config }: { config: Record<string, string> }
|
||||
<CardTitle>{t('admin.email.title')}</CardTitle>
|
||||
<CardDescription>{t('admin.email.description')}</CardDescription>
|
||||
</CardHeader>
|
||||
<form name="email-form" onSubmit={(e) => { e.preventDefault(); handleSaveEmail(new FormData(e.currentTarget)) }}>
|
||||
<form onSubmit={(e) => { e.preventDefault(); handleSaveEmail(new FormData(e.currentTarget)) }}>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm font-medium">{t('admin.email.provider')}</label>
|
||||
@@ -985,11 +957,6 @@ export function AdminSettingsForm({ config }: { config: Record<string, string> }
|
||||
<Input id="RESEND_API_KEY" name="RESEND_API_KEY" type="password" defaultValue={config.RESEND_API_KEY || ''} placeholder="re_..." />
|
||||
<p className="text-xs text-muted-foreground">{t('admin.resend.apiKeyHint')}</p>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label htmlFor="SMTP_FROM_RESEND" className="text-sm font-medium">Sender email</label>
|
||||
<Input id="SMTP_FROM_RESEND" name="SMTP_FROM" defaultValue={config.SMTP_FROM || ''} placeholder="noreply@yourdomain.com" />
|
||||
<p className="text-xs text-muted-foreground">Email address used as sender. Must be a domain verified in Resend.</p>
|
||||
</div>
|
||||
{config.RESEND_API_KEY && (
|
||||
<div className="flex items-center gap-2 text-xs text-green-600">
|
||||
<span className="inline-block w-2 h-2 rounded-full bg-green-500" />
|
||||
|
||||
@@ -41,12 +41,12 @@ export async function sendEmail({ to, subject, html, attachments }: MailOptions,
|
||||
// Force Resend (no fallback)
|
||||
if (provider === 'resend') {
|
||||
if (!resendKey) return { success: false, error: 'No Resend API key configured' };
|
||||
return sendViaResend(resendKey, config, { to, subject, html, attachments });
|
||||
return sendViaResend(resendKey, { to, subject, html, attachments });
|
||||
}
|
||||
|
||||
// Auto: try Resend, fall back to SMTP
|
||||
if (resendKey) {
|
||||
const result = await sendViaResend(resendKey, config, { to, subject, html, attachments });
|
||||
const result = await sendViaResend(resendKey, { to, subject, html, attachments });
|
||||
if (result.success) return result;
|
||||
|
||||
console.warn('[Mail] Resend failed, falling back to SMTP:', result.error);
|
||||
@@ -56,28 +56,14 @@ export async function sendEmail({ to, subject, html, attachments }: MailOptions,
|
||||
return sendViaSMTP(config, { to, subject, html, attachments });
|
||||
}
|
||||
|
||||
async function sendViaResend(apiKey: string, config: Record<string, string>, { to, subject, html, attachments }: MailOptions): Promise<MailResult> {
|
||||
async function sendViaResend(apiKey: string, { to, subject, html, attachments }: MailOptions): Promise<MailResult> {
|
||||
try {
|
||||
const { Resend } = await import('resend');
|
||||
const resend = new Resend(apiKey);
|
||||
|
||||
// Build a valid "from" address for Resend
|
||||
// Priority: SMTP_FROM from DB config > env var > derived from NEXTAUTH_URL > Resend default
|
||||
const smtpFrom = config.SMTP_FROM || process.env.SMTP_FROM;
|
||||
let from: string;
|
||||
if (smtpFrom) {
|
||||
from = smtpFrom.includes('<') ? smtpFrom : `Memento <${smtpFrom}>`;
|
||||
} else if (process.env.NEXTAUTH_URL) {
|
||||
const hostname = new URL(process.env.NEXTAUTH_URL).hostname;
|
||||
// Only use hostname-based from if it's not localhost (Resend rejects it)
|
||||
if (hostname !== 'localhost') {
|
||||
from = `Memento <noreply@${hostname}>`;
|
||||
} else {
|
||||
from = 'Memento <onboarding@resend.dev>';
|
||||
}
|
||||
} else {
|
||||
from = 'Memento <onboarding@resend.dev>';
|
||||
}
|
||||
const from = process.env.NEXTAUTH_URL
|
||||
? `Memento <noreply@${new URL(process.env.NEXTAUTH_URL).hostname}>`
|
||||
: 'Memento <onboarding@resend.dev>';
|
||||
|
||||
// Resend supports attachments with inline content
|
||||
const resendAttachments = attachments?.map(att => ({
|
||||
|
||||
Reference in New Issue
Block a user