Send mail in js
In JavaScript, you can send an email using the mailto
protocol, which is a URI scheme that allows you to create a link that, when clicked, will open the user's default email client with a new email message.
Here is an example of how you can send an email using JavaScript:
<a href="mailto:[email protected]?subject=Hello&body=This%20is%20a%20test%20email">Send Email</a>
This will create a link that, when clicked, will open the user's default email client with a new email message addressed to [email protected]
with the subject "Hello" and the body "This is a test email".
You can also use JavaScript to send an email programmatically using the window.location
object:
window.location.href = "mailto:[email protected]?subject=Hello&body=This%20is%20a%20test%20email";
This will achieve the same result as the previous example, but will not create a link.
You can also use libraries like emailjs
or nodemailer
to send emails programmatically.
Here is an example of how you can use emailjs
to send an email:
const emailjs = require('emailjs');
const server = emailjs.server('smtp.gmail.com', 587, {
user: '[email protected]',
password: 'your_password'
});
const message = {
from: '[email protected]',
to: '[email protected]',
subject: 'Hello',
body: 'This is a test email'
};
server.send(message, function(err, message) {
if (err) {
console.log(err);
} else {
console.log('Email sent successfully!');
}
});
This will send an email to [email protected]
with the subject "Hello" and the body "This is a test email" using the emailjs
library.
Please note that sending emails programmatically requires a valid email account and password, and may be subject to certain restrictions and limitations.