-5

I'm creating a website and I have to send the text of a form as an email. I'm running the script before the body so as the function is not working is blocking the whole script. The function I use is this:

function Email(){
                var command='mailto:blahblah@gmail.com?Subject=';
                command=+ document.getElementById('subject').value;
                command=+ "&body=";
                command=+ document.getElementById('context').value;

                window.location.href=command;
            }

So basically the user enters in two fields the subject and the message of the email and I want it to be send as an email but it doesn't open the outlook as it does when I use a fixed command.

My HTML code that is used is this:

<button onclick="Email();"> Send </button> 

I corrected all the things mentioned to the comments and now I get an error saying:

Firefox can't find the file at /C:/..../NaN. Check the file name for capitalisation or other typing errors. Check to see if the file was moved, renamed or deleted.

natan
  • 79
  • 9
  • 3
    I'm not exactly sure what you're asking. It seems like you mean `href` and not `ref` in your `window.location` command. Additionally, the reset will never be hit because the last part of the Email function is to redirect. Are you receiving any errors? – xDaevax Oct 08 '14 at 15:15
  • Is `window.location.ref` a typo? `window.location.href` is what it should be. Calling this command will also stop processing anything after the function. – haxtbh Oct 08 '14 at 15:18
  • 1
    Why this `'"` when declaring the variable? – emerson.marini Oct 08 '14 at 15:18

2 Answers2

0

mailto links are just supposed to invite the user to send an email using their own mail client. You can't send email directly from javascript in your app. You'll have to send the content of the form to your server, and then email it from there. There are lots of server-side libraries for sending email (I use nodemailer), or you can use a service to send the mail for you, such as mailchimp or sendgrid.

Raphael Serota
  • 2,157
  • 10
  • 17
  • I've seen this http://stackoverflow.com/a/7977629/3992589 that's why I thought it was possible :S – natan Oct 08 '14 at 15:43
  • Why is it opening my outlook if write a fixed sentence and this can't work? – natan Oct 08 '14 at 16:16
  • @natan This is how mailto links work. They merely open your default email client, in your case this is Outlook. There is no way to send email automatically through a browser only. This would be a security nightmare! – Lee Taylor Oct 08 '14 at 18:25
  • ok now i understand how it works, thanks for the explanation...but still doesn't have the command i create to open my email client like the fixed command when i press the button? why can't this work in the same way? – natan Oct 09 '14 at 09:39
0

What the guys who answered before and one more thing. You need to add .value to the end of your document.getElementById statements. Othervise it will return a node insted of a value.

here is updated html and javascript:

<form id="mailform">
  subject <input type="text" id="subject">
  context <input type="text" id="context">
 <input type="submit" value="submit">

</form>

//javascript

function Email(){
    var subject = document.getElementById('subject').value;
     var context = document.getElementById('context').value;
            var command='mailto:blahblah@gmail.com?Subject=';
            command=+ subject;
            command=+ "&body=";
            command=+context;


            window.location.href=command;
        }
document.getElementById('submit').addEventListener('click', function(e){
e.preventDefault();
Email();
});
natan
  • 79
  • 9
Beckafly
  • 411
  • 2
  • 5