專案裡有一個發送郵件的需求,我從網路上找了幾種方法,列舉如下:
1.
<a href="mailto:test@163.com;test1@163.com?CC=test@163.com&BCC=test@163.com&Subject=Hello&Body=你好">给我发邮件</a>
2.
<button type="button" id="email">发送邮件</button>
<script type="text/javascript"> $(document).on("click", "#email", function () { document.location = "Mailto:me@home.com?subject=Feedback; //经过测试这个方法不成功 }); </script>
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>
<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>
<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>
<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>
rrreeerrreee
rrreeerrreee3.
rrreeerrreee的body是type=hideen的value值,並且顯示的是message=.....,message是type=hideen的name,如果將name="message"換成
最後我改良了這個代碼,不用form進行發送郵件。
rrreeerrreee
不過第三種方法的兩個不同的方式,還有些地方不同:用form發送郵件不會出現自己outlook中的固定模板,但用a發送郵件的話會出現那種模板