简体   繁体   中英

How to send to a specific email using Javascript forms?

I want to have it so I can fill out this kind of form (below) and then when I click "Send" it would send to a specific email that I have inputted. Meaning, I would be declaring the information that is filled out in the "Email" field as a variable and then somehow have the "mailto:" link send to that email address. I really hope that makes sense. I'm not entirely sure how to explain it.

It seems like it would be relatively simple, I'm just not having much luck (Javascript is not my strongest language).

<!DOCTYPE html>
<html>
<body>

<h2>Send e-mail to someone@example.com:</h2>

<form action="MAILTO:someone@example.com" method="post" enctype="text/plain">
Name:<br>
<input type="text" name="name" value="your name"><br>
E-mail:<br>
<input type="text" name="mail" value="your email"><br>
Comment:<br>
<input type="text" name="comment" value="your comment" size="50"><br><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>

</body>
</html>

Or, even a way to make a contact form of sorts, but it goes to an email that you input somewhere in the form.

The mailto can't be used with a form directly.

However, you can do something like this :

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
</head>
<body>

<h2>Send e-mail to someone@example.com:</h2>

<form action="#" id="myForm">
Name:<br>
<input type="text" name="name" value="your name"><br>
E-mail:<br>
<input type="text" name="mail" value="email@mail.com"><br>
Comment:<br>
<input type="text" name="comment" value="your comment" size="50"><br><br>
<button id="btnSend">Send mail</button>
<input type="reset" value="Reset">
</form>
<script>
$(function(){
    $(myForm).submit(function(){
    var mailto = "mailto:";

    mailto += $("input[name=mail]").val();

    mailto += "?subject=Your Email Subject (new comment)";

    mailto += "&body=message from : " + $("input[name=name]").val() + " comment : " + $("input[name=comment]").val();
    console.log(mailto);
    window.open(mailto);
    return false;
    });

});
</script>
</body>
</html>

Basicly we agregate the field value and opend a new windows/tab with the mailto as url.

Note that it will open the mail client of the user (if he/she have one) and he/she will have to click send for the mail to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM