Reputation: 109
I'm trying to create a registration form. This form includes an email text box. How do I make it so that the code sends an email to the email that the user inputted?
Here's the code:
<form name="regForm" method="post" onsubmit="return validateSignupForm()">
<div class="form-group mb-0">
<label for="formGroupEmail"></label>
<input type="email" class="form-control" id="formGroupEmail" placeholder="Email address"
name="signupEmail">
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Sign up" class="btn btn-danger">
</div>
</form>
Thanks
Upvotes: 1
Views: 884
Reputation: 1621
html doesn't support sending emails, to do this you need a backend language.
I red you are using node js: so it is really simple. On the "send" button click you have to send a post request to the server (with the email content, title, and recipient). Then you have to make the server read the request and send the email using nodemailer (i think this is the best service).
to install nodemailer just write in the bash
npm install nodemailer
if you want to read a tutorial about nodemailer i would suggest you this, that is really well done.
Here is a simple example of what you are trying to do: client.html
<form action="/sendEmail" method="post">
<input type="text" name="content">
<input type="text" name="email">
<input type="submit">
</form>
and this is an example of how to send an email using nodemailer: but be careful, you have to implement this in a server (i would suggest you express, look at this)
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourpassword'
}
});
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
note also that there are a few email api that can allow you to send an email from js without any server (for example emailjs or Sendgrid)
const nodemailer = require('nodemailer');
const express = require('express');
const app = express();
const port = 80;
var account_data = {email: "[email protected]", password: "yourpassword", service: "gmail"}
app.use(express.json());
app.post('/sendEmail', (req,res) => {
var transporter = nodemailer.createTransport({
service: account_data["service"],
auth: {
user: account_data["email"],
pass: account_data["password"]
}
});
var mailOptions = {
from: account_data["email"],
to: req.body.email,
subject: 'some subject',
text: req.body.content
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.send('Email sent');
})
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
})
to run this remember that you have to install both express and nodemailer,
look here for a tutorial about express
As @KevinPotgieter noticed nodemailer doesn't work for Google Cloud Platform, which hosting are you using?
Upvotes: 2
Reputation: 798
Not entirely needed to use a backend, You can use a service like SendGrid to send the mail. You'll need to use their API after you have created an account. I think the first 1000 emails a month are free
Example:
const sendGridMail = require('@sendgrid/mail');
sendGridMail.setApiKey(options.send_grid_api_key);
const mail = {
from: from_email,
to: recipient,
subject: email_subject,
html: html_text
};
sendGridMail.send(mail, false),
Upvotes: 1
Reputation: 1
Unfortunately html doesn't support sending mail. To do this, you need to use a backend (PHP, Java, Node js).
Upvotes: 0