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

查看:43
本文介绍了使用 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=" + encodeURIComponent("This is my subject")
             + "&body=" + encodeURIComponent(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天全站免登陆