Javascript将数据库条目发送到电子邮件

时间:2016-06-05 12:43:13

标签: javascript database cordova email send

在Phonegap-App中,我有一个数据库,希望用户在App会话结束时将其条目发送到用户指定的电子邮件地址。

如何使用Javascript获取用户设备上的数据库条目,并让用户键入数据发送到的电子邮件地址?

请帮忙吗?

这是数据库Javascript代码:

<script>
        var FILENAME = 'database.db',
            $ = function (id) {
                return document.getElementById(id);
            },
            failCB = function (msg) {
                return function () {
                    alert('[FAIL] ' + msg);
                }
            },
            file = {
                writer: { available: false },
                reader: { available: false }
            },
            dbEntries = [];
        document.addEventListener('deviceready', function () {
            var fail = failCB('requestFileSystem');
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
        }, false);
        function gotFS(fs) {
            var fail = failCB('getFile');
            fs.root.getFile(FILENAME, {create: true, exclusive: false},
                            gotFileEntry, fail);
        }
        function gotFileEntry(fileEntry) {
            var fail = failCB('createWriter');
            file.entry = fileEntry;
            fileEntry.createWriter(gotFileWriter, fail);
            readText();
        }
        function gotFileWriter(fileWriter) {
            file.writer.available = true;
            file.writer.object = fileWriter;
        }
        function saveText(e) {
            var name = $('name').value,
                desc = $('desc').value,
                fail;
            dbEntries.push('<dt>' + name + '</dt><dd>' + desc + '</dd>')
            $('name').value = '';
            $('desc').value = '';
            $('definitions').innerHTML = dbEntries.join('');
            if (file.writer.available) {
                file.writer.available = false;
                file.writer.object.onwriteend = function (evt) {
                    file.writer.available = true;
                    file.writer.object.seek(0);
                }
                file.writer.object.write(dbEntries.join("\n"));
            }
            return false;
        }
        function readText() {
            if (file.entry) {
                file.entry.file(function (dbFile) {
                    var reader = new FileReader();
                    reader.onloadend = function (evt) {
                        var textArray = evt.target.result.split("\n");
                        dbEntries = textArray.concat(dbEntries);
                        $('definitions').innerHTML = dbEntries.join('');
                    }
                    reader.readAsText(dbFile);
                }, failCB("FileReader"));
            }
            return false;
        }
    </script>

2 个答案:

答案 0 :(得分:0)

使用插件https://github.com/katzer/cordova-plugin-email-composer

它会直接打开手机设备中电子邮件的默认应用程序。

js语法如下:

cordova.plugins.email.open({
    to:          Array, // email addresses for TO field
    cc:          Array, // email addresses for CC field
    bcc:         Array, // email addresses for BCC field
    attachments: Array, // file paths or base64 data streams
    subject:    String, // subject of the email
    body:       String, // email body (for HTML, set isHtml to true)
    isHtml:    Boolean, // indicats if the body is HTML or plain text
}, callback, scope);

答案 1 :(得分:0)

好吧,我认为使用Javascript电子邮件无法正常工作,因为我希望它能够正常运行。

有什么方法可以用php来获取数据吗?

不喜欢用function readText()显示数据,而是将其交给服务器端.php,就像使用此表单一样:

<?php
$empfaenger = $_POST["email"]; 
$absendername = $_POST["name"];
$absendermail = $_POST["email"];
$betreff = "$absendername have send an email.";
$answer1 = $_POST["antwort1"];
$answert2 = $_POST["antwort2"];
$text = "send to: $absendermail

Namen: $absendername

Station 1: $answer1 
Station 2: $answer2";
mail($empfaenger, $betreff, $text, "From: $absendername <$absendername>");
?>
<head>
</head>
<body>
<h3>Thanks</h3>
</body>