简体   繁体   English

使用 JS 从 HTML 页面发送 email

[英]Send email from HTML page using JS

I'm trying to send a mail from a html page using the below:我正在尝试使用以下内容从 html 页面发送邮件:

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


 <!-- Contact Form -->
 <div class="col-md-6 contact">
    <form role="form">
               <!-- Name -->
               <div class="row">
                   <div class="col-md-6">
                       <!-- E-Mail -->
                       <div class="form-group">
                           <input type="text" class="form-control" placeholder="Your name" id="myText">
                       </div>
                   </div>
                   <div class="col-md-6">
                       <!-- Phone Number -->
                       <div class="form-group">
                           <input type="email" class="form-control" placeholder="Email address">
                       </div>
                   </div>
               </div>
               <!-- Message Area -->
               <div class="form-group">
                   <textarea class="form-control" placeholder="Write you message here..." style="height:232px;" id="myText2"></textarea>
               </div>
               <!-- Subtmit Button -->
               <button type="submit" class="btn btn-send" 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.默认情况下,我希望将这些 go 放入 email 的主题和正文部分。

Your query string variables should start with "?", not "&".您的查询字符串变量应以“?”开头,而不是“&”。

var link = "mailto:email@live.co.uk?subject=" + encodeURIComponent(document.getElementById('myText').value)
             + "&body=" + encodeURIComponent(document.getElementById('myText2').value)
    ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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