Home > Web Front-end > JS Tutorial > body text

js send email

高洛峰
Release: 2016-12-17 12:42:19
Original
1816 people have browsed it

There is a need to send emails in the project. I found several methods from the Internet, listed below:

 1.

<a href="mailto:test@163.com;test1@163.com?CC=test@163.com&BCC=test@163.com&Subject=Hello&Body=你好">给我发邮件</a>
Copy after login

2.

<button type="button" id="email">发送邮件</button>
Copy after login
<script type="text/javascript">
    $(document).on("click", "#email", function () {
         document.location = "Mailto:me@home.com?subject=Feedback; 
//经过测试这个方法不成功
    });

</script>
Copy after login

3.

<form name="frmEmailOutLook" action="" method="post" enctype="text/plain"> 
  <input type="hidden" name="message" value=""> 
</form> 
<form name="frmEmail" action="" method="post"> 
姓名:<input type="text" name="name" value="a"><BR> 
电话:<input type="text" name="phone" value="b"><BR> 
网址:<input type="text" name="website" value="c"><BR> 
主题:<input type="text" name="subject" value="d"><BR> 
内容:<textarea name="message" >ee 
ddd</textarea><BR> 
<input type="button" name="send" value="send" onClick="toOutLook()"> 
</form>
Copy after login
<script language="javascript">... 
function toOutLook()...{ 
   var objFrm = document.frmEmail; 
    var objFrmOutLook = document.frmEmailOutLook; 
    var msg = ""; 
    msg += "姓名: " + objFrm.name.value + "  "; 
    msg += "电话: " + objFrm.phone.value + "  "; 
    msg += "网址: " + objFrm.website.value + "  "; 
    msg += "主题: " + objFrm.subject.value + "  "; 
    msg += "内容: " + objFrm.message.value + "  "; 
    objFrmOutLook.message.value = msg; 
    objFrmOutLook.action = "mailto:sundysea@hotmail.com?subject=" + objFrm.subject.value; 
    objFrmOutLook.submit(); 
} 
</script>
Copy after login

However, there are several problems with this method: 1. Email The body is the value of type=hideen, and what is displayed is message=....., message is the name of type=hideen, if name="message" is replaced with

name="Content", then the body The displayed content becomes Content=………….

2. If the content is written in Chinese characters, it will be garbled when displayed in Outlook

Finally, I improved this code and sent the email without a form.

<div class="commonemail_Wrap">
       <!-- <form  name="frmEmailOutLook" action="" method="post" enctype="text/plain">
            <input type="hidden" name="message" value=""/>
        </form>-->
        <!--<form name="frmEmail" action="" method="post">
            <div><label>收件人</label><input type="text" name="website"  /></div>
            <div><label>主题</label><input type="text"  name="subject"/></div>
            <div><label class="unique">内容</label><textarea  rows="4"   name="message"></textarea></div>
            <input type="button" name="send" value="发送邮件" id="SendEmai"/>
        </form>-->
        <div>
            <div><label>收件人</label><input type="text" name="website" id="test1" /></div>
            <div><label>主题</label><input type="text"  id="test2" name="subject"/></div>
            <div><label class="unique">内容</label><textarea  rows="4"   id="test3" name="message"></textarea></div>
            <!--<input type="button" name="send" value="发送邮件" id="SendEmail"/>-->
            <a href="" id="SendEmail">发送邮件</a>
        </div>
    </div>
Copy after login
<script type="text/javascript">
        $(document).on("click", "#SendEmail", function () {

            var website = document.getElementById("test1").value;
            var subject = document.getElementById("test2").value;
            var body = document.getElementById("test3").value;
            var url = "mailto:" + website + "?subject=" + subject + "&body=" + body;
            document.getElementById("SendEmai").setAttribute("href", url);
         //return fasle;
        });
</script>
Copy after login

However, there are some differences between the two different methods of the third method: when you use form to send an email, the fixed template in your outlook will not appear, but if you use a to send an email, that template will appear



For more articles related to js sending emails, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template