使用Javascript发送电子邮件 [英] Sending emails with Javascript

查看:233
本文介绍了使用Javascript发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有点混乱的解释,所以在这里忍受我...

This is a little confusing to explain, so bear with me here...

我想建立一个系统,用户可以通过我的方式发送模板的电子邮件网站,除了它实际上不是使用我的服务器发送 - 而是打开自己的本地邮件客户端与电子邮件准备去。应用程序将使用预定义的变量填写电子邮件的正文,以保存用户必须自己键入。他们可以根据需要编辑邮件,如果它不完全符合他们的目的。

I want to set up a system where a user can send templated emails via my website, except it's not actually sent using my server - it instead just opens up their own local mail client with an email ready to go. The application would fill out the body of the email with predefined variables, to save the user having to type it themselves. They can then edit the message as desired, should it not exactly suit their purposes.

有很多理由我希望通过用户的本地邮件客户端,让服务器发送电子邮件不是一个选择:它必须是100%的客户端。

There's a number of reasons I want it to go via the user's local mail client, so getting the server to send the email is not an option: it has to be 100% client-side.

我已经有一个大多数工作的解决方案运行,我会发布的细节作为答案,我想知道是否有更好的方法?

I already have a mostly-working solution running, and I'll post the details of that as an answer, I'm wondering if there's any better way?

推荐答案

我的方式现在做的基本是这样的:

The way I'm doing it now is basically like this:

HTML:

<textarea id="myText">
    Lorem ipsum...
</textarea>
<button onclick="sendMail(); return false">Send</button>

Javascript:

The Javascript:

function sendMail() {
    var link = "mailto:me@example.com"
             + "?cc=myCCaddress@example.com"
             + "&subject=" + escape("This is my subject")
             + "&body=" + escape(document.getElementById('myText').value)
    ;

    window.location.href = link;
}

令人惊讶的是,效果很好。唯一的问题是,如果身体特别长(超过2000个字符的地方),那么它只是打开一个新的电子邮件,但没有信息。我怀疑这是与超出的URL的最大长度有关。

This, surprisingly, works rather well. The only problem is that if the body is particularly long (somewhere over 2000 characters), then it just opens a new email but there's no information in it. I suspect that it'd be to do with the maximum length of the URL being exceeded.

这篇关于使用Javascript发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆