Send emails with Express
Add email sending to your Express.js backend with YourSend.
1. Install
npm install yoursend express2. Set up Express route
server.ts
import express from 'express';
import { YourSend } from 'yoursend';
const app = express();
app.use(express.json());
const ys = new YourSend({ apiKey: process.env.YOURSEND_API_KEY! });
app.post('/send-email', async (req, res) => {
const { to, subject, html } = req.body;
try {
const message = await ys.send({ channel: 'email', to, subject, html });
res.json({ message_id: message.message_id });
} catch (err) {
res.status(400).json({ error: err instanceof Error ? err.message : 'Send failed' });
}
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});3. Test it
curl -X POST http://localhost:3000/send-email \
-H "Content-Type: application/json" \
-d '{
"to": "user@example.com",
"subject": "Hello from Express",
"html": "<p>Sent from Express!</p>"
}'