I've been grappling with an issue for over a week while trying to develop a web application that sends welcome emails to new subscribers. Despite my API code working perfectly, I cannot seem to get any output on the console indicating success or failure of the email delivery. No matter what I try, the terminal remains unresponsive.
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const axios = require('axios');
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// setting up a server to send welcome emails to users who subscribe
app.post('/send-welcome-email', async (req, res) => {
const { email } = req.body;
const emailData = {
From: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e497838596858fa48081858f8d8aca818091ca8591">[email protected]</a>',
To: email,
Subject: 'Welcome to DevLink',
HtmlBody: '<html><head></head><body><p>Welcome to DevLink, thanks for jooining our platform!</p></body></html>'
};
try {
const response = await axios.post('https://api.postmarkapp.com/email', emailData, {
headers: {
'Authorization': `MY_API_TOKEN`,
'Accept': 'application/json',
'content-type': 'application/json'
}
});
res.status(200).send('Email sent');
} catch (error) {
console.error(error);
res.status(500).send('Something went wrong');
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
background-color: #ffffff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
margin-top: 50px;
}
h1 {
font-size: 24px;
margin-bottom: 10px;
}
form {
display: flex;
flex-direction: column;
}
label {
font-size: 16px;
margin-bottom: 5px;
}
input[type="email"] {
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color: #007bff;
color: #ffffff;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>DevLink Subscription</title>
</head>
<body>
<div class="container">
<h1>Signup for our daily insider</h1>
<form id="subscription-form">
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email" required>
<button type="submit">Subscribe</button>
</form>
</div>
<script src="script.js"></script>
</body>
</html>