使用 JavaScript 发送 html 电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15089857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending html emails with JavaScript
提问by szamfirov
I'm creating a form (using only HTML and JavaScript) that sends emails. I want to insert a simple HTML table inside the body of the email. Something like this:
我正在创建一个发送电子邮件的表单(仅使用 HTML 和 JavaScript)。我想在电子邮件正文中插入一个简单的 HTML 表格。像这样的东西:
<table border=1>
<tr><td>blabla</td></tr>
</table>
<table border=1>
<tr><td>blabla</td></tr>
</table>
After I click on the "Send" button a JavaScript function is called which sends the email via URL and the POST data appears in my email client - Outlook (which is okay for me) but there is a problem with the formatting. The data in bodyof the email is in plain text.
单击“发送”按钮后,将调用 JavaScript 函数,该函数通过 URL 发送电子邮件,POST 数据出现在我的电子邮件客户端 - Outlook(对我来说没问题)中,但格式存在问题。电子邮件正文中的数据为纯文本格式。
- Is there any other way to do this only with JavaScript?
- Is it necessary to use some kind of server-side scripts (like PHP or others) to format it properly?
- 有没有其他方法可以只使用 JavaScript 来做到这一点?
- 是否有必要使用某种服务器端脚本(如 PHP 或其他)来正确格式化它?
回答by Tapas Pal
You can use JavaScript mailto function. Try this, hope it'll help you
您可以使用 JavaScript 的 mailto 函数。试试这个,希望对你有帮助
<div id="mailBody"><table border=1> <tr><td>blabla</td></tr> </table></div>
<input type="button" onclick="sendMail();">
Under the script tag
在脚本标签下
function sendMail()
{
var mailBody=document.getElementById('mailBody').innerHTML;
window.location="mailto:[email protected]?subject=hii&body="+mailBody;
}
回答by Pandian
If you want to use only the java script to send HTML email try like below...
如果您只想使用 java 脚本发送 HTML 电子邮件,请尝试如下...
But i have NOT Recommendedthis type of Mail sending....
但我不推荐这种类型的邮件发送....
Sending HTML Email by Javascript :
通过 Javascript 发送 HTML 电子邮件:
function SendEmail()
{
var outlookApp = new ActiveXObject("Outlook.Application");
var nameSpace = outlookApp.getNameSpace("MAPI");
mailFolder = nameSpace.getDefaultFolder(6);
mailItem = mailFolder.Items.add('IPM.Note.FormA');
mailItem.Subject = "Me";
mailItem.To = "[email protected]";
mailItem.HTMLBody = "<table border=1> <tr><td>blabla</td></tr> </table>";
mailItem.display(0);
}
if you want to just send a email as plain text by javascript then try like below...
如果您只想通过 javascript 以纯文本形式发送电子邮件,请尝试如下...
Sending Plain Text by Javascript :
通过 Javascript 发送纯文本:
window.open('mailto:[email protected]?subject=subject&body=Testing Email');
Better you use a Server Side Coding to Send a email
最好使用服务器端编码来发送电子邮件
回答by Subliminal Hash
What are the <
characters are like in if you look at the source? if they are like <
or <
then you need to disable encoding on these. Also, you need to set formatting to HTML mail. Sorry I am not a PHPguy but I guess you will need server side code for that.
<
如果你看来源,人物是什么样的?如果他们喜欢<
或者<
那么你需要禁用这些编码。此外,您需要将格式设置为 HTML 邮件。对不起,我不是一个PHP人,但我想你需要服务器端代码。