Home >Web Front-end >JS Tutorial >How Can I Send Emails from My Website without Page Refresh Using JavaScript?
How to Send an Email without Page Refresh Using JavaScript
Problem:
You aim to create a website that sends emails without requiring a page refresh. You intend to use JavaScript for this purpose, but are unsure of the code to implement within the JavaScript function.
Answer:
While JavaScript itself is unable to send emails, there are alternatives to achieve this functionality:
Opening the User's Mail Client:
window.open('mailto:[email protected]');
Pre-filling Subject and Body (Optional):
window.open('mailto:[email protected]?subject=subject&body=body');
Server-Side Email Sending:
This involves making an AJAX call to your server, which then handles sending the email. Here's a sample PHP script for server-side processing:
<?php if (isset($_POST['email'])) { mail($_POST['to'], $_POST['subject'], $_POST['body']); }
Caution:
When using the server-side approach, implement security measures to prevent unauthorized email sending through your server.
The above is the detailed content of How Can I Send Emails from My Website without Page Refresh Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!