After mail send message using javascript
You're looking to send a message after sending an email using JavaScript!
To achieve this, you'll need to use a combination of JavaScript and a server-side technology like PHP or Node.js to send the email. Here's a basic example using PHP:
HTML
<form id="email-form">
<input type="email" id="email" name="email" placeholder="Enter your email">
<input type="submit" value="Send Email">
</form>
JavaScript
const form = document.getElementById('email-form');
form.addEventListener('submit', (e) => {
e.preventDefault();
const email = document.getElementById('email').value;
// Send email using PHP
fetch('send-email.php', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `email=${email}`
})
.then((response) => response.text())
.then((message) => {
// Display the message after sending the email
alert(message);
})
.catch((error) => console.error(error));
});
PHP (send-email.php)
<?php
// Configuration
$to = '[email protected]';
$subject = 'Test Email';
// Get the email from the form
$email = $_POST['email'];
// Send the email using PHPMailer or your preferred email library
$mail = new PHPMailer();
$mail->setFrom($email);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = 'This is a test email';
$mail->send();
// Display a success message
echo 'Email sent successfully!';
?>
In this example, when the form is submitted, the JavaScript code sends a POST request to the send-email.php
script, which sends the email using PHPMailer or your preferred email library. After the email is sent, the PHP script displays a success message, which is then caught by the JavaScript code and displayed as an alert.
Note that this is a basic example and you should consider implementing proper error handling and security measures when sending emails in a production environment.