-5

I'm building my personal website. At the "contact" page, I would like the visitor to send message to me. Once they clicked the button, an e-mail containing the typed information would be sent to my e-mail address. I searched some results implemented by PHP. But I want get some advise on just html or javascript.

My following code can active a email window. But I don't want the visitors to type the information again. I want the email would be sent just after the "submit" button is clicked.

<form method="post" action='mailto:myemail@gmail.com'>
    <label for="visitor-name">Name</label>
    <input type="text" id="visitor-name" placeholder="Please Enter Your Name">
    <label for="visitor-email">E-mail</label>
    <input type="text" id="visitor-name" placeholder="Please Enter Your E-mail">
    <label for="visitor-message">Message</label>
    <textarea id="visitor-message" placeholder="Please Leave your Message"></textarea>
    <input type="submit" value="Submit" id="contact-sub" onclick="sendmessage();">
</form>

by the way, the sendmessage() function is empty.

  • 4
    Without server side code like PHP to process the email, neither HTML nor JavaScript can send email on its own without opening the client's default mail application. – j08691 Oct 16 '14 at 19:34
  • Because of the mailto in your form, this is going to open the mail client on the user's computer (if they have one). Like mentioned above, if you want to send through code server-side the action needs to point to a program that processes the form post server-side. – Tim Oct 16 '14 at 19:36
  • What about nodejs http://stackoverflow.com/questions/4113701/sending-emails-in-node-js – Maxim Hash Oct 16 '14 at 19:36
  • 2
    @MaximHash: Nodejs is a server side application, even if uses javascript. – Daniel Conde Marin Oct 16 '14 at 19:40

3 Answers3

0

I'm sure you've read through these responses and seen already, but what you want to do is impossible through only client-side code. These server-side solutions work, because alot of these server environments have the different servers and whatnot set up to handle sending off an email.

It's not as simple as doing an AJAX request.

You may be able to find a third party service that helps you handle transactional email, like Mandrill.

These work, because they do all the work that the server-side needs. All you need to do is access their API endpoint through AJAX the way you would anything else, and bam. Mandrill will shoot off your email for you.

Now, Mandrill has a free tier, and that may be all you need. But think about how much this will be used, as it may just be more worth it to use something like Mandrill, as opposed to rolling your own sever side solution.

taveras
  • 485
  • 3
  • 12
-1

To do what you want, you need to point this to a server-side technology - mailto opens the client's mail application if they have one.

The scripts you mention point to services like GMail, which is server-side code.

Tim
  • 4,051
  • 10
  • 36
  • 60
-1

Here's an example for simple PHP contact just put a form and have it send the results of the form here as php is a server side code it can send messages without the user having to use their email client. I would recommend putting a simple verification as well.

$EmailFrom = "WHO@changeme.com";
$EmailTo = "you@whatever.com";
$Name = Trim(stripslashes($_POST['Name'])); 
$Message = Trim(stripslashes($_POST['Message'])); 
$test = $_POST['math'];
// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "Message: ";
$Body .= $Message;    
// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page 
if ($success){
  print "Thanks for contacting me";
}
?>
obi1kenobi2
  • 43
  • 1
  • 8