在客户端使用 JavaScript 发送电子邮件

Sending an email in JavaScript on client side

提问人:Marshall 提问时间:9/23/2023 更新时间:9/23/2023 访问量:36

问:

也许有人可以告诉我为什么会发生这种情况以及我能做些什么。我正在尝试从客户端发送电子邮件。

代码如下(所有客户端)...

HTML格式:

    <button type="button" onclick="PrepEmail()">Contact Creator/Maintainer/Provider of This Data via email</button>

JS:

    <script type="text/javascript">
    function PrepEmail() {
        var DBinfo, pnt, email, version, header;
        const ws = new WebSocket('ws://localhost:3000/');

        ws.onopen = function() {
            ws.send("DBInfo");
            ws.onmessage = function(e) {
                if (e.data == -1 || e.data == -2)
                    alert ('Email address for Creator of DataBase not available.');
                else {
                    DBinfo = e.data;
                    pnt = DBinfo.indexOf("DBCreatorEmail =");
                    if (pnt == -1)
                        alert ('Email address for Creator of DataBase not available.');
                    else {
                        email = getData(DBinfo, pnt);
                        if (email == "not specified")
                            alert ('Email address for Creator of DataBase not available.');
                        else {
                            header = "mailto:";
                            header += email + "?subject=";
                            pnt = DBinfo.indexOf("DBName =");
                            if (pnt == -1)
                                header += "DataBase";
                            else {
                                header += getData(DBinfo, pnt);
                                pnt = DBinfo.indexOf("DBVersion =");
                                if (pnt != -1) {
                                    version = getData(DBinfo, pnt);
                                    header += " (" + version + ")";
                                }
                            }
                        }
                    }
                }
            }
        }
        const terminationEvent = 'onpagehide' in self ? 'pagehide' : 'unload';
        window.addEventListener(terminationEvent, (event) => {
            if (event.persisted === false) {
                // client is gone
                ws.onclose = function () { };
                ws.close();
            }
        });

        SendEmail(header);
    }

    function SendEmail (header) {
        const windowRef = window.open(header, '_blank');

        windowRef.focus();
        setTimeout(function() {
            if(!windowRef.document.hasFocus()) {
                windowRef.close();
            }
        }, 500);
    }

    function getData (DB, p) {
        var pos1 = DB.indexOf('"', p) + 1;
        var pos2 = DB.indexOf('"', pos1);
        var data = DB.substring(pos1, pos2);
        if (data == "")
            return ("not specified")
        else
            return (data);
    }

    </script>

WebSocket 调用按预期工作,返回:

DBName = “真正的数据库” DBVersion = “20230101” DBCreator = “Joe Blow”


DBCreatorEmail = “[email protected]
"

单击该按钮会显示一个包含完全空白页面的新选项卡。新选项卡的地址行包含“about:blank”。

但是,如果我将标头变量设置为全局变量并避免传递它 - 第一次单击按钮时,我会得到同样的东西:一个新选项卡,其中包含一个完全空白的页面和一个包含“about:blank”的地址行;但是,如果我第二次单击该按钮,它工作正常,并且在 Inspect 中收到以下控制台消息(消息仅在第二次单击后显示):

在“http://999.999.9.999:8080”上自动授予源“moz-nullprincipal:{c3a06e0d-3847-421d-b143-df87ff91029d}”的存储访问权限。
未捕获的 DOMException:拒绝访问跨域对象
上的属性“document”的权限 SendEmail http://999.999.9.999:8080/~devdir/Misc.html:65
setTimeout 处理程序*SendEmail http://999.999.9.999:8080/~devdir/Misc.html:64
PrepEmail http://999.999.9.999:8080/~devdir/Misc.html:56
onclick http://999.999.9.999:8080/~devdir/Misc.html:1

JavaScript HTML 电子邮件

评论

1赞 Konrad 9/23/2023
我不确定我是否看对了,但你在外面打电话,而不是在完成标头后打电话SendEmail(header)onopen
0赞 Marshall 9/23/2023
@Konrad,枪之子。我早就应该知道的。非常感谢您的评论。例程的工作方式应如此。

答: 暂无答案