1

I'm trying to send the data filled in a form to my email: form.html:

    <form role="form" name="contact_form">
  <div class="col-xs-4">
    <div class="form-group">
      <input type="text" class="form-control" id="input_name" name="Name" placeholder="Name">
    </div>
    <div class="form-group">
      <input type="email" class="form-control" id="input_email" name="Email" placeholder="Email">
    </div>
    <div class="form-group">
      <input type="tel" class="form-control" id="input_phone" name="Phone" placeholder="Phone">
    </div>
    <div class="form-group">
      <input type="tel" class="form-control" id="input_location" name="Location" placeholder="Location">
    </div>
  </div>
  <div class="col-xs-6">
    <div class="form-group">
      <textarea class="form-control"  id="contact_comment" name="Comment" placeholder="



Details or comments?" rows="10"></textarea>
    </div>
  </div>
<div class="col-xs-2">
    <div class="form-group">
      <button id="submit_button"type="submit" class="btn btn-primary" onclick="submit_comment()">Submit</button>
    </div>
  </div>
</form>

script.js:

<script>
function sendMail(str) {
    var link = "mailto:slim.hmidi1@gmail.com" + "&subject=" + escape("This is my subject") + "&body=" + escape(str);
    location.href = link;
}
function submit_comment() {
var name = document.forms["contact_form"]["Name"].value;
var Email = document.forms["contact_form"]["Email"].value;
var Phone = document.forms["contact_form"]["Phone"].value;
var Location = document.forms["contact_form"]["Location"].value;
var Comment = document.forms["contact_form"]["Location"].value;
if((name !="") && (Email !="") && (Phone !="") && (Location !="") && (Comment !="")) {
   sendMail("HELLO");
}
}
</script>

I found these

  1. how-to-send-an-email-from-javascript
  2. sending-emails-with-javascript

But it doesn't work for mge.I'm using wordpress to create my own website.How can I fix that?

Community
  • 1
  • 1
user7883113
  • 43
  • 1
  • 1
  • 9
  • 2
    You clearly haven't read the first SO topic that you have posted in your question. The voted answer there (that one with the big green check icon) _clearly_ answers how to solve this problem. How did you miss that? – KarelG May 11 '17 at 14:48
  • 1
    I think that I'm not blind and it didn't fix my problem.Thank you for your time and your comment ;) – user7883113 May 11 '17 at 15:12
  • What 'solution' do you want? You can use mailto protocol and have their local client take over (if they have one) or you can send the emails from your server (read: __not__ from the client side). – rlemon May 11 '17 at 15:15
  • _now_.. if you updated your question to outline what you're seeing you might get better results. The answer to your actual question is "you can't" and is outlined in the dupes listed. the problem you're most likely having is that you don't cancel the form submission and the page reloads before the email client is loaded.. but that isn't being asked. – rlemon May 11 '17 at 15:18
  • You may want to take a look at [EmailJS](http://emailjs.com?src=so), which allows sending email using pre-built templates directly from Javascript [disclaimer - I'm one of the creators] – Sasha May 13 '17 at 09:55
  • @user7883113 Did you find a solution for your question? – nourza Jun 09 '20 at 06:39

1 Answers1

-1

You can't. Mails are send through a back-end server. Javascript/jQuery is client-side. You can, however, let people send an e-mail through their own mail application.

Otherwise you have to send a ajax request to a back-end script.

TRiG
  • 10,148
  • 7
  • 57
  • 107
SmartGuyz
  • 69
  • 1
  • 10
  • 1
    Why does it have to be Ajax? Would a normal form submission not work? – TRiG May 11 '17 at 15:22
  • We can't send mail directly from JS, but we can use some of free libraries. One of them is added below. https://developers.google.com/apps- script/articles/sending_emails – Rohit Parte Apr 13 '19 at 11:55