Home > Back-end >  Send email from HTML page using JS
Send email from HTML page using JS

Time:12-17

I'm trying to send a mail from a html page using the below:

<script>
    function sendMail() {
    var link = "mailto:[email protected] &subject="   encodeURIComponent(document.getElementById('myText').value)
               "&body="   encodeURIComponent(document.getElementById('myText2').value)
    ;
    
    window.location.href = link;
}
  </script>


 <!-- Contact Form -->
 <div >
    <form role="form">
               <!-- Name -->
               <div >
                   <div >
                       <!-- E-Mail -->
                       <div >
                           <input type="text"  placeholder="Your name" id="myText">
                       </div>
                   </div>
                   <div >
                       <!-- Phone Number -->
                       <div >
                           <input type="email"  placeholder="Email address">
                       </div>
                   </div>
               </div>
               <!-- Message Area -->
               <div >
                   <textarea  placeholder="Write you message here..." style="height:232px;" id="myText2"></textarea>
               </div>
               <!-- Subtmit Button -->
               <button type="submit"  onclick="sendMail(); return false">
                   Send message
               </button>
           </form>
 </div>

When I press button to click all of the data ends up in the default mail apps "To:" Text input area. Can anyone tell me how to split out the subject and body correctly?? I'd like these to go into the subject and body sections of the email by default.

CodePudding user response:

Your query string variables should start with "?", not "&".

var link = "mailto:[email protected]?subject="   encodeURIComponent(document.getElementById('myText').value)
               "&body="   encodeURIComponent(document.getElementById('myText2').value)
    ;
  • Related