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(process.env.YOURSEND_API_KEY!);
app.post('/send-email', async (req, res) => {
const { to, subject, html } = req.body;
const { data, error } = await ys.send({
channel: 'email',
from: 'hello@yourdomain.com',
to,
subject,
html,
});
if (error) {
return res.status(400).json({ error });
}
res.json({ message_id: data.message_id });
});
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>"
}'