-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
89 lines (75 loc) · 2.63 KB
/
server.js
File metadata and controls
89 lines (75 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import express from 'express';
import cors from 'cors';
import { Resend } from 'resend';
const app = express();
const PORT = 3001;
// Initialize Resend with your API key
const resend = new Resend('re_XiNEHU2C_PqTmd21U8xTrfsRWqfMe2Z1R');
// Middleware
app.use(cors());
app.use(express.json({ limit: '10mb' })); // Increase limit for large HTML emails
// Health check endpoint
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', message: 'Email API server is running' });
});
// Send test email endpoint
app.post('/api/send-test-email', async (req, res) => {
try {
const { to, subject, from, html } = req.body;
// Validate required fields
if (!to || !subject || !html) {
return res.status(400).json({
error: 'Missing required fields',
required: ['to', 'subject', 'html'],
});
}
// Validate email format
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(to)) {
return res.status(400).json({
error: 'Invalid email address format',
});
}
console.log('📧 Sending email via Resend...');
console.log('To:', to);
console.log('Subject:', subject);
console.log('From:', from || 'onboarding@resend.dev');
// Send email using Resend
// Note: For testing, we must use onboarding@resend.dev
// Custom domains need to be verified in Resend dashboard
const data = await resend.emails.send({
from: 'onboarding@resend.dev', // Must use verified domain
to: to, // This sends to the email address entered by the user
subject: subject,
html: html,
reply_to: from, // Set reply-to to the original from address
});
console.log('✅ Email sent successfully!');
console.log('Response:', data);
console.log('📬 Email should arrive at:', to);
// Check if email was sent to the intended recipient
if (data.id) {
console.log('✅ Resend Email ID:', data.id);
console.log('🔍 Track this email in Resend dashboard: https://resend.com/emails/' + data.id);
}
res.json({
success: true,
message: 'Email sent successfully',
data: data,
});
} catch (error) {
console.error('❌ Error sending email:', error);
res.status(500).json({
error: 'Failed to send email',
message: error.message,
details: error.response?.data || error,
});
}
});
// Start server
app.listen(PORT, () => {
console.log('🚀 Email API Server Started!');
console.log(`📍 Server running on http://localhost:${PORT}`);
console.log(`📧 Resend API configured and ready`);
console.log(`\n✅ Ready to send emails from your email builder!`);
});