Reputation: 7145
I have a webpage with a text box where the user can enter his email id. I want to send mail to the mail Id Entered.
<form action="mailto:[email protected]" method="post" enctype="text/plain" >
FirstName:<input type="text" name="FirstName">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="Submit">
</form>
The problem is I want mailto:[email protected]
to be taken from the text box.Then a message should appear saying that email has been sent successfully. Can I do that using javascript?
Upvotes: 1
Views: 11928
Reputation: 128993
Browser-side JavaScript cannot send email on its own. The best it can do is try to open the user's email application for them so they can send the email. For example:
location.href = 'mailto:' + encodeURIComponent(emailAddress) +
'?subject=' + encodeURIComponent(subject) +
'&body=' + encodeURIComponent(body);
You could hook the submit
event of the form, prevent the default action, and execute that code, and the end result would be they could fill out the form, click submit, their email client opens, and they can click send.
Try it on JSFiddle.
Upvotes: 3
Reputation: 41209
First of all, emails cannot be sent using javascript. It is a client side scripting language only, and cannot send an email without first contacting a server and 'asking' it to send the email using PHP or some other server-side language.
I'm not sure what you mean by saying you want mailto:[email protected]
to be taken from the textbox. mailto:
is just a protocol that causes the browser to open up the user's default email program to send a mail to the given address. You can easily create a mailto: link that the user can click on to do this or pull the email address from an existing mailto: link, but the email still has to be sent some other way.
There are plenty of elaborate ways to tell the user a message has been sent succesfully, but the easiest and most direct is probably a simple alert box:
alert("Your email has been sent succesfully.");
Upvotes: 0