Spaces:
Runtime error
Runtime error
Download src/component/BulkEmail.js from kirangawande0176/DevSync: direct link, hf CLI and curl.
- Browser
- Download file 12.4 kB
-
https://huggingface.co/spaces/kirangawande0176/DevSync/resolve/main/src/component/BulkEmail.js
- Command line
-
hf download hf://spaces/kirangawande0176/DevSync/src/component/BulkEmail.js
-
curl -L -o BulkEmail.js https://huggingface.co/spaces/kirangawande0176/DevSync/resolve/main/src/component/BulkEmail.js
12.4 kB
| // // pages/bulk-email.js | |
| // import { useState } from 'react'; | |
| // import Papa from 'papaparse'; | |
| // export default function BulkEmail() { | |
| // const [file, setFile] = useState(null); | |
| // const [subject, setSubject] = useState(''); | |
| // const [body, setBody] = useState(''); | |
| // const [recipients, setRecipients] = useState([]); | |
| // const [loading, setLoading] = useState(false); | |
| // const [log, setLog] = useState(''); | |
| // const handleFileChange = (e) => { | |
| // const f = e.target.files?.[0]; | |
| // setFile(f); | |
| // setLog(''); | |
| // if (!f) return; | |
| // Papa.parse(f, { | |
| // header: true, | |
| // skipEmptyLines: true, | |
| // complete: (results) => { | |
| // // Expecting columns: email, name | |
| // const rows = results.data | |
| // .map((row) => ({ | |
| // email: String(row.email || '').trim(), | |
| // name: row.name ? String(row.name).trim() : '', | |
| // })) | |
| // .filter((r) => r.email); | |
| // setRecipients(rows); | |
| // setLog(`Parsed ${rows.length} recipients from file.`); | |
| // }, | |
| // error: (err) => { | |
| // console.error(err); | |
| // setLog('Failed to parse CSV file.'); | |
| // }, | |
| // }); | |
| // }; | |
| // const handleSubmit = async (e) => { | |
| // e.preventDefault(); | |
| // if (!recipients.length) { | |
| // setLog('No recipients loaded from file.'); | |
| // return; | |
| // } | |
| // if (!subject || !body) { | |
| // setLog('Please enter subject and body.'); | |
| // return; | |
| // } | |
| // setLoading(true); | |
| // setLog('Sending emails...'); | |
| // // Optional: simple personalization using {{name}} placeholder | |
| // // This example sends one email to all in BCC, so only global body is used. | |
| // // For per-recipient personalization, you would loop and send in chunks. [web:11] | |
| // try { | |
| // const res = await fetch('/api/send-bulk-email', { | |
| // method: 'POST', | |
| // headers: { 'Content-Type': 'application/json' }, | |
| // body: JSON.stringify({ | |
| // recipients, | |
| // subject, | |
| // html: body, | |
| // }), | |
| // }); | |
| // const data = await res.json(); | |
| // if (!res.ok) { | |
| // throw new Error(data.error || 'Failed to send emails'); | |
| // } | |
| // setLog(`Emails submitted successfully. Status: ${data.data?.id || 'OK'}`); | |
| // } catch (err) { | |
| // console.error(err); | |
| // setLog(`Error: ${err.message}`); | |
| // } finally { | |
| // setLoading(false); | |
| // } | |
| // }; | |
| // return ( | |
| // <div style={{ maxWidth: 600, margin: '40px auto', fontFamily: 'system-ui' }}> | |
| // <h1>Bulk Email Sender (CSV)</h1> | |
| // <p style={{ fontSize: 14, color: '#555' }}> | |
| // 1. Export your Excel file as CSV with columns: <strong>email,name</strong>.<br /> | |
| // 2. Upload file, set subject and HTML body, then send. | |
| // </p> | |
| // <form onSubmit={handleSubmit}> | |
| // <div style={{ marginBottom: 16 }}> | |
| // <label style={{ display: 'block', marginBottom: 4 }}>CSV file (from Excel)</label> | |
| // <input type="file" accept=".csv" onChange={handleFileChange} /> | |
| // </div> | |
| // <div style={{ marginBottom: 16 }}> | |
| // <label style={{ display: 'block', marginBottom: 4 }}>Subject</label> | |
| // <input | |
| // type="text" | |
| // value={subject} | |
| // onChange={(e) => setSubject(e.target.value)} | |
| // style={{ width: '100%', padding: 8 }} | |
| // placeholder="Your email subject" | |
| // /> | |
| // </div> | |
| // <div style={{ marginBottom: 16 }}> | |
| // <label style={{ display: 'block', marginBottom: 4 }}>HTML Body</label> | |
| // <textarea | |
| // value={body} | |
| // onChange={(e) => setBody(e.target.value)} | |
| // style={{ width: '100%', minHeight: 150, padding: 8 }} | |
| // placeholder="<p>Hello,</p><p>This is a bulk email.</p>" | |
| // /> | |
| // </div> | |
| // <div style={{ marginBottom: 16, fontSize: 14 }}> | |
| // <strong>Recipients loaded:</strong> {recipients.length} | |
| // </div> | |
| // <button | |
| // type="submit" | |
| // disabled={loading} | |
| // style={{ | |
| // padding: '10px 20px', | |
| // backgroundColor: loading ? '#888' : '#0070f3', | |
| // color: '#fff', | |
| // border: 'none', | |
| // borderRadius: 4, | |
| // cursor: loading ? 'default' : 'pointer', | |
| // }} | |
| // > | |
| // {loading ? 'Sending...' : 'Send Bulk Email'} | |
| // </button> | |
| // </form> | |
| // {log && ( | |
| // <pre | |
| // style={{ | |
| // marginTop: 20, | |
| // padding: 10, | |
| // backgroundColor: '#f5f5f5', | |
| // borderRadius: 4, | |
| // fontSize: 13, | |
| // whiteSpace: 'pre-wrap', | |
| // }} | |
| // > | |
| // {log} | |
| // </pre> | |
| // )} | |
| // </div> | |
| // ); | |
| // } | |
| // pages/bulk-email.js | |
| import { useState } from 'react'; | |
| import Papa from 'papaparse'; | |
| export default function BulkEmail() { | |
| const [file, setFile] = useState(null); | |
| const [subject, setSubject] = useState(''); | |
| const [body, setBody] = useState(''); | |
| const [recipients, setRecipients] = useState([]); | |
| const [manualInput, setManualInput] = useState(''); // manual emails textarea | |
| const [loading, setLoading] = useState(false); | |
| const [log, setLog] = useState(''); | |
| const parseManualRecipients = () => { | |
| if (!manualInput.trim()) return []; | |
| // Split by comma, semicolon, or newline | |
| const raw = manualInput | |
| .split(/[\n,;]+/) | |
| .map((item) => item.trim()) | |
| .filter(Boolean); | |
| const unique = Array.from(new Set(raw)); | |
| return unique.map((email) => ({ | |
| email, | |
| name: '', | |
| })); | |
| }; | |
| const handleFileChange = (e) => { | |
| const f = e.target.files?.[0] || null; | |
| setFile(f); | |
| setLog(''); | |
| if (!f) { | |
| // if user clears file input, keep manual entries only | |
| const manualRecipients = parseManualRecipients(); | |
| setRecipients(manualRecipients); | |
| return; | |
| } | |
| Papa.parse(f, { | |
| header: true, | |
| skipEmptyLines: true, | |
| complete: (results) => { | |
| const rows = | |
| results.data | |
| ?.map((row) => ({ | |
| email: String(row.email || '').trim(), | |
| name: row.name ? String(row.name).trim() : '', | |
| })) | |
| .filter((r) => r.email) || []; | |
| const manualRecipients = parseManualRecipients(); | |
| // merge CSV + manual, de-dupe by email | |
| const all = [...rows, ...manualRecipients]; | |
| const dedupMap = new Map(); | |
| all.forEach((r) => { | |
| if (!dedupMap.has(r.email)) { | |
| dedupMap.set(r.email, r); | |
| } | |
| }); | |
| const merged = Array.from(dedupMap.values()); | |
| setRecipients(merged); | |
| setLog( | |
| `Parsed ${rows.length} recipients from file. Manual: ${manualRecipients.length}. Total unique: ${merged.length}.` | |
| ); | |
| }, | |
| error: (err) => { | |
| console.error(err); | |
| setLog('Failed to parse CSV file.'); | |
| }, | |
| }); | |
| }; | |
| const handleManualBlur = () => { | |
| // Update recipients whenever manual input loses focus | |
| const manualRecipients = parseManualRecipients(); | |
| // keep any file recipients if file is present | |
| if (!file) { | |
| setRecipients(manualRecipients); | |
| setLog(`Manual recipients: ${manualRecipients.length}.`); | |
| return; | |
| } | |
| // if file recipients already loaded, merge with them | |
| setRecipients((prev) => { | |
| const all = [...prev, ...manualRecipients]; | |
| const dedupMap = new Map(); | |
| all.forEach((r) => { | |
| if (!dedupMap.has(r.email)) { | |
| dedupMap.set(r.email, r); | |
| } | |
| }); | |
| const merged = Array.from(dedupMap.values()); | |
| setLog( | |
| `Recipients updated. Manual: ${manualRecipients.length}, Total unique: ${merged.length}.` | |
| ); | |
| return merged; | |
| }); | |
| }; | |
| const handleSubmit = async (e) => { | |
| e.preventDefault(); | |
| const manualRecipients = parseManualRecipients(); | |
| let finalRecipients = recipients; | |
| // if no file used but manual has data, use manual | |
| if (!finalRecipients.length && manualRecipients.length) { | |
| finalRecipients = manualRecipients; | |
| setRecipients(manualRecipients); | |
| } | |
| if (!finalRecipients.length) { | |
| setLog('No recipients found. Please upload CSV or enter emails manually.'); | |
| return; | |
| } | |
| if (!subject.trim() || !body.trim()) { | |
| setLog('Please enter subject and body.'); | |
| return; | |
| } | |
| setLoading(true); | |
| setLog('Sending emails...'); | |
| try { | |
| const res = await fetch('/api/send-bulk-email', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| recipients: finalRecipients, | |
| subject, | |
| html: body, | |
| }), | |
| }); | |
| const data = await res.json(); | |
| if (!res.ok) { | |
| throw new Error(data.error || 'Failed to send emails'); | |
| } | |
| setLog( | |
| `Emails submitted successfully. Status: ${data.data?.id || 'OK'}` | |
| ); | |
| } catch (err) { | |
| console.error(err); | |
| setLog(`Error: ${err.message}`); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }; | |
| return ( | |
| <div style={{ maxWidth: 600, margin: '40px auto', fontFamily: 'system-ui' }}> | |
| <h1>Bulk Email Sender</h1> | |
| <p style={{ fontSize: 14, color: '#555' }}> | |
| You can either upload a CSV file (<strong>email,name</strong>) or enter | |
| email addresses manually (comma / semicolon / new line separated). | |
| </p> | |
| <form onSubmit={handleSubmit}> | |
| {/* CSV upload */} | |
| <div style={{ marginBottom: 16 }}> | |
| <label style={{ display: 'block', marginBottom: 4 }}> | |
| CSV file (from Excel) | |
| </label> | |
| <input type="file" accept=".csv" onChange={handleFileChange} /> | |
| </div> | |
| {/* Manual emails input */} | |
| <div style={{ marginBottom: 16 }}> | |
| <label style={{ display: 'block', marginBottom: 4 }}> | |
| Manual email addresses | |
| </label> | |
| <textarea | |
| value={manualInput} | |
| onChange={(e) => setManualInput(e.target.value)} | |
| onBlur={handleManualBlur} | |
| style={{ width: '100%', minHeight: 100, padding: 8 }} | |
| placeholder={ | |
| 'one@example.com\nsecond@example.com\nthird@example.com' | |
| } | |
| /> | |
| <small style={{ fontSize: 12, color: '#666' }}> | |
| Separate emails by comma, semicolon, or new line. | |
| </small> | |
| </div> | |
| {/* Subject */} | |
| <div style={{ marginBottom: 16 }}> | |
| <label style={{ display: 'block', marginBottom: 4 }}>Subject</label> | |
| <input | |
| type="text" | |
| value={subject} | |
| onChange={(e) => setSubject(e.target.value)} | |
| style={{ width: '100%', padding: 8 }} | |
| placeholder="Your email subject" | |
| /> | |
| </div> | |
| {/* Body */} | |
| <div style={{ marginBottom: 16 }}> | |
| <label style={{ display: 'block', marginBottom: 4 }}>HTML Body</label> | |
| <textarea | |
| value={body} | |
| onChange={(e) => setBody(e.target.value)} | |
| style={{ width: '100%', minHeight: 150, padding: 8 }} | |
| placeholder="<p>Hello,</p><p>This is a bulk email.</p>" | |
| /> | |
| </div> | |
| {/* Recipients count */} | |
| <div style={{ marginBottom: 16, fontSize: 14 }}> | |
| <strong>Recipients loaded:</strong> {recipients.length} | |
| </div> | |
| {/* Submit */} | |
| <button | |
| type="submit" | |
| disabled={loading} | |
| style={{ | |
| padding: '10px 20px', | |
| backgroundColor: loading ? '#888' : '#0070f3', | |
| color: '#fff', | |
| border: 'none', | |
| borderRadius: 4, | |
| cursor: loading ? 'default' : 'pointer', | |
| }} | |
| > | |
| {loading ? 'Sending...' : 'Send Bulk Email'} | |
| </button> | |
| </form> | |
| {log && ( | |
| <pre | |
| style={{ | |
| marginTop: 20, | |
| padding: 10, | |
| backgroundColor: '#f5f5f5', | |
| borderRadius: 4, | |
| fontSize: 13, | |
| whiteSpace: 'pre-wrap', | |
| }} | |
| > | |
| {log} | |
| </pre> | |
| )} | |
| </div> | |
| ); | |
| } | |