Email Subject Line Personalizer

Email Subject Line Personalizer

Generate tailored subject lines for different audience segments using AI.

Campaign Details

Generated Subject Lines

Your AI-generated subject lines will appear here.

AI is writing your subject lines...

`; const systemPrompt = "You are an expert email marketer and copywriter specializing in personalization. Your task is to generate 5 to 7 compelling, personalized email subject lines based on the user's base subject, target audience, and goal. Ensure the subject lines are concise and designed to maximize open rates. Respond only with a valid JSON object matching the provided schema."; const userPrompt = `Base Subject: "${inputsData.baseSubject}", Target Audience: "${inputsData.audience}", Goal: "${inputsData.goal}"`; const jsonSchema = { type: "OBJECT", properties: { subject_lines: { type: "ARRAY", items: { type: "OBJECT", properties: { subject: { type: "STRING" } }, required: ["subject"] } } }, required: ["subject_lines"] }; const apiKey = ""; const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`; const payload = { contents: [{ parts: [{ text: userPrompt }] }], systemInstruction: { parts: [{ text: systemPrompt }] }, generationConfig: { responseMimeType: "application/json", responseSchema: jsonSchema } }; try { const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) throw new Error(`API Error: ${response.statusText}`); const result = await response.json(); const aiResult = JSON.parse(result.candidates?.[0]?.content?.parts?.[0]?.text); if (aiResult.subject_lines && aiResult.subject_lines.length > 0) { generatedSubjects = aiResult.subject_lines.map(item => item.subject); renderSubjects(); } else { throw new Error("No subject lines were generated."); } } catch (error) { console.error("Content generation error:", error); resultsContainer.innerHTML = `

An error occurred: ${error.message}

`; showNotification("Error generating content."); } finally { generateBtn.disabled = false; generateBtn.textContent = 'Generate Subjects'; } } function renderSubjects() { resultsContainer.innerHTML = generatedSubjects.map((subject, index) => `

${subject}

`).join(''); } // --- UTILITY FUNCTIONS --- function showNotification(message) { notification.textContent = message; notification.classList.remove('opacity-0'); setTimeout(() => notification.classList.add('opacity-0'), 2000); } function copyToClipboard(text, message) { navigator.clipboard.writeText(text).then(() => showNotification(message)); } // --- PDF GENERATION --- async function generatePdfReport() { if (generatedSubjects.length === 0) { showNotification("No subjects to export."); return; } downloadPdfBtn.disabled = true; downloadPdfBtn.textContent = '...'; const subjectCards = generatedSubjects.map((subject, index) => `

${subject}

`).join(''); const reportHtml = `

Email Marketing A/B Test Ideas

For Audience: ${inputsData.audience}

Campaign Details

Base Subject: ${inputsData.baseSubject}
Goal: ${inputsData.goal}

Generated Subject Lines
${subjectCards}
`; const pdfTemplate = document.getElementById('pdf-template'); pdfTemplate.innerHTML = reportHtml; pdfTemplate.classList.remove('invisible'); try { const { jsPDF } = window.jspdf; const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-page'), { scale: 2 }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(), pdfHeight = (canvas.height * pdfWidth) / canvas.width; pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight); pdf.save(`Email_Subjects_${inputsData.audience.replace(/\s+/g, '_').substring(0,20)}.pdf`); } catch (e) { console.error('PDF Generation Error:', e); } finally { downloadPdfBtn.disabled = false; downloadPdfBtn.textContent = 'Download PDF'; pdfTemplate.classList.add('invisible'); pdfTemplate.innerHTML = ''; } } // --- EVENT LISTENERS --- generateBtn.addEventListener('click', generateSubjects); copyAllBtn.addEventListener('click', () => { if (generatedSubjects.length > 0) { const allText = generatedSubjects.join('\n'); copyToClipboard(allText, 'All subjects copied!'); } }); resultsContainer.addEventListener('click', (e) => { if (e.target.classList.contains('copy-subject-btn')) { const text = generatedSubjects[e.target.dataset.index]; copyToClipboard(text, 'Subject copied!'); } }); downloadPdfBtn.addEventListener('click', generatePdfReport); });
Scroll to Top