使用Meteor / Javascript向我自己发送包含联系表格数据的电子邮件

时间:2015-10-17 22:40:07

标签: javascript html email meteor contact-form

我想弄清楚如何使用从联系表单收集的数据向我自己发送电子邮件。我正在使用Meteor和Bootstrap构建应用程序。我愿意将数据写入Google电子表格,如果这样做容易得多,但我更愿意发送电子邮件。到目前为止,这是我的代码:

HTML:

<div class="modal fade" id="contact">
          <div class="modal-dialog">
            <div class="modal-content">
              <form class="form-horizontal" onsubmit="return check_contact();">
                <div class="modal-header" style="text-align: center">
                  <img style="max-width:140px;" src="LagoonLogoContact.png">
                </div>

                <div class="modal-body">

                  <div class="form-group">
                    <label for="contact-name" class="col-lg-1 control-label"><i class="fa fa-user" id="contact-name-icon"></i></label>

                    <div class="col-lg-11">
                      <input type="text" class="form-control" id="contact-name" placeholder="name">
                    </div>

                  </div>

                  <div class="form-group">
                    <label for="contact-email" class="col-lg-1 control-label"><i class="fa fa-envelope-o" id="contact-email-icon"></i></label>

                    <div class="col-lg-11">
                      <input type="email" class="form-control" id="contact-email" placeholder="email">
                    </div>

                  </div>

                  <div class="form-group">
                    <label for="contact-msg" class="col-lg-1 control-label"><i class="fa fa-pencil" id="contact-message-icon"></i></label>

                    <div class="col-lg-11">
                      <textarea class="form-control" rows="8" placeholder="message" id="contact-message"></textarea>
                    </div>

                  </div>

                </div>

                <div class="modal-footer">
                  <a class="btn btn-primary pull-left" data-dismiss="modal"><i class="fa fa-times-circle"></i> Close</a>
                  <button class="btn btn-primary pull-right" type="submit"><i class="fa fa-paper-plane"></i> Send</button>
                </div>
              </form>
            </div>
          </div>
        </div>

JAVASCRIPT(同一页面上的HTML下方):

 <script>

    function check_contact() {

      var name = document.getElementById('contact-name').value;
      var email = document.getElementById('contact-email').value;
      var message = document.getElementById('contact-message').value;
      var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

      if(name == "" || email == "" || message == ""){

        document.getElementById('contact-name').style.border = "1px solid black";
        document.getElementById('contact-name-icon').style.color = "black";

        document.getElementById('contact-email').style.border = "1px solid black";
        document.getElementById('contact-email-icon').style.color = "black";

        document.getElementById('contact-message').style.border = "1px solid black";
        document.getElementById('contact-message-icon').style.color = "black";

        if(name == ""){
        document.getElementById('contact-name').style.border = "1px solid #ff6464";
        document.getElementById('contact-name-icon').style.color = "#ff6464";
        }

        if(email == "" || !reg.test(email)){
        document.getElementById('contact-email').style.border = "1px solid #ff6464";
        document.getElementById('contact-email-icon').style.color = "#ff6464";
        }

        if(message == ""){
        document.getElementById('contact-message').style.border = "1px solid #ff6464";
        document.getElementById('contact-message-icon').style.color = "#ff6464";
        }

        return false;

      }
      else{
        if(reg.test(email)){

          console.log("working here");

          var nameSend = $('#contact-name').val();
          var emailSend = $('#contact-email').val();
          var messageSend = $('#contact-message').val();

          var dataSend = 'nameSend=' + name + '&emailSend=' + email + '&messageSend=' + message;
          console.log(dataSend);


          return true;
          }

        else {
          return false;
        }
      }

    }

  </script>

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

email套餐可让您轻松发送电子邮件。请注意,email.send()仅适用于服务器,因此您需要定义服务器方法并从客户端调用它。

您还需要注意不要创建垃圾邮件向量:如果客户端可以使用所有参数调用此函数,那么任何人都可以轻松编写脚本以发送大量电子邮件。通常,您不希望收件人和正文字段都来自客户端。