【问题标题】:Send form input by email without server通过电子邮件发送表单输入,无需服务器
【发布时间】:2014-04-07 20:27:21
【问题描述】:

对于本地托管的 HTML 页面,我需要通过电子邮件发送一些用户输入(姓名、电子邮件、cmets)。

这是在触摸屏显示器上完成的,访问者可以在其中留下他们的信息和 cmets。

所以我在考虑使用虚拟键盘,例如here

<form action="post" id="emails" onsubmit="return false;">
<input type="text" id="keyboard" placeholder="Enter name..."></input>
<input type="text"  id="keyboard2" placeholder="Enter email..."></input>
<input type='submit' id='send' value='Submit' />
</form>

我用它来发送电子邮件link

如果详细信息正确,以下代码可以正常工作,并且我会在收件箱中收到电子邮件。

$('#send').click(function() {
    $.ajax({ 
        type: 'POST',
        url: 'https://mandrillapp.com/api/1.0/messages/send.json',
        data: {
            'key': "xxxxxxxx",
            'message': {
            'from_email': "xxxxxx@xxxxxx.com",
            'to': [
            {
            'email': "xxxxxxxx@xxxxxxxxxx.com",
            'name': 'xxxxxx',
            'type': 'to'
            }
            ],
            'autotext': 'true',
            'subject': 'TEST! TEST!',
            'html': 'test'
            }
        }
    }).done(function(response) {
        console.log(response);
        alert("You send an email!"); // if you're into that sorta thing
    });
});

所以这行得通,因为它发送带有文本的电子邮件:'html': 'test'

我显然希望得到“姓名+电子邮件”而不是“测试”。

(此外,每月可能发送不到 50 封电子邮件,所以现在只需要一封电子邮件就可以满足我的需求,没有数据库和保存所有这些数据。我只想收到电子邮件。)

那么,有可能吗?我应该怎么做?

我现在采取的路线可行吗,还是您会建议完全不同的方式?

【问题讨论】:

    标签: javascript jquery html email


    【解决方案1】:
    // do everything inside after page load.
    $(function() {
        $('#send').click(function() {
            // get values of inputs...
            var name = $('#keyboard').val(),
                email = $('#keyboard2').val();
    
            $.ajax({ 
                type: 'POST',
                url: 'https://mandrillapp.com/api/1.0/messages/send.json',
                data: {
                    'key': "xxxxxxxx",
                    'message': {
                    'from_email': "xxxxxx@xxxxxx.com",
                    'to': [
                    {
                    'email': "xxxxxxxx@xxxxxxxxxx.com",
                    'name': 'xxxxxx',
                    'type': 'to'
                    }
                    ],
                    'autotext': 'true',
                    'subject': 'TEST! TEST!',
                    'html': 'Name: ' + name + '\nEmail: ' + email // and use it!
                    }
                }
            }).done(function(response) {
                console.log(response);
                alert("You send an email!"); // if you're into that sorta thing
            });
        });
    });
    

    【讨论】:

    • 谢谢,我已经更改了我的代码以匹配此,但是当我在填写随机名称 + 电子邮件后按下发送按钮时,我收到一封仅包含以下内容的电子邮件:名称:电子邮件:跨度>
    【解决方案2】:

    我现在可以使用以下代码:

    $(function() {
    
    $('#send').click(function() {
        var name = $('#keyboard').val();
        var email = $('#keyboard2').val();
        $.ajax({ 
            type: 'POST',
            url: 'https://mandrillapp.com/api/1.0/messages/send.json',
            data: {
                'key': "xxxxxxxx",
                'message': {
                'from_email': "xxxxxx@xxxxxx.com",
                'to': [
                {
                'email': "xxxxxxxx@xxxxxxxxxx.com",
                'name': 'xxxxxx',
                'type': 'to'
                }
                ],
                'autotext': 'true',
                'subject': 'TEST! TEST!',
                'html': 'Name: ' + name + '\nEmail: ' + email // and use it!
                }
            }
        }).done(function(response) {
            console.log(response);
            alert("You send an email!"); // if you're into that sorta thing
        });
      });
    });
    

    在其他类似的问题上没有显示值后。

    【讨论】:

    • 为了让它工作,我必须放置 ' var name = $('#keyboard').val(); var email = $('#keyboard2').val(); ' 在 click 函数内部而不是在函数上方的行。
    猜你喜欢
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2013-10-10
    相关资源
    最近更新 更多