最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to send mail using javascript - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Mar 3, 2012 at 4:06 station asked Mar 3, 2012 at 4:02 stationstation 7,14516 gold badges59 silver badges93 bronze badges 3
  • 2 add an id to the email input, say id="mail". then document.getElementById("mail").value. – onemach Commented Mar 3, 2012 at 4:06
  • So you want the user send email to himself/herself ?? – xdazz Commented Mar 3, 2012 at 4:09
  • You cannot send mail directly from the client-side. You can set up your form to behave as a mailto link, but there is no way to confirm whether or not the message initiated by the mailto was sent successfully. – rjz Commented Mar 3, 2012 at 4:09
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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.");
发布评论

评论列表(0)

  1. 暂无评论