繁体   English   中英

如何在js中使用smtp邮寄表单

[英]How to mailto in form with smtp in js

我被困在这里。 我想要一个按钮(在这种情况下为“发送电子邮件”按钮)来触发mailto而不打开电子邮件客户端,我希望它自动发送(在JS中,smtp)。 我不知道我问得是否太多,甚至是否有可能。这是我的表格:

<form id="form">
  <div class="row">
    <div class="col-md-6">
      <div class="input-group">
        <span class="input-group-addon" id="basic-addon1"><i class="fa fa-user-secret fa-lg"></i></span>
        <input id="username" type="text" class="form-control blender-pro-book form-text" placeholder="Username" aria-describedby="basic-addon1">
        <span class="input-group-addon" id="basic-addon1"><i class="fa fa-user-secret fa-lg"></i></span>
        <input id="message" type="text" class="form-control blender-pro-book form-text" placeholder="Message" aria-describedby="basic-addon1">
      </div>
      <p class="error-holder"><span id="error" class="error blender-pro-book"><i class="fa fa-exclamation-triangle"></i>Try again please!</span></p>
    </div>
  </div>
  <br>
  <div class="row">
    <div class="col-md-6 col-md-offset-3 col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-3">
      <button id="start" type="button" class="btn btn-default btn-lg btn-block btn-custom blender-pro-book">Send e-mail <i class="fa fa-hand-o-right blue-text-color"></i></button>
    </div>
  </div>
</form>

我尝试输入几个代码,但是它们都导致打开电子邮件客户端。 然后,我发现了SmtpJS.com 我已经将脚本代码放在索引文件中,但是我不知道将代码放在哪里:

Email.send("from@you.com",
"to@them.com",
"This is a subject",
"this is the body",
"mx1.hostinger.nl", */ That's my hosting SMTP /* 
"username",
"password");

我只希望此按钮发送电子邮件:

<button id="start" type="button" class="btn btn-default btn-lg btn-block btn-custom blender-pro-book">Send e-mail <i class="fa fa-hand-o-right blue-text-color"></i></button>

您能告诉我在表格中的什么位置吗?

非常感谢!

为什么要使用一些JavaScript电子邮件服务? 只需使用PHP。
PHP是一种服务器端语言。 使用$To = $_POST['to'];类的命令$To = $_POST['to']; $From = $_POST['from']; ,您可以获取从表单元素发送的数据。 我建议您阅读Mail函数上的PHP手册,以了解如何使用PHP发送电子邮件。 PHP Mail函数实际上非常简单。 如果您不太了解PHP,请访问W3Schools.com的PHP教程。

<form method="POST" action="mymail.php">
<input name="to" type="text" placeholder="To:" value="" />
<input name="from" type="text" placeholder="From:" value="" />
<input name="cc" type="text" placeholder="CC:" value="" />
<!-- Blah Blah Blah, your code goes here, I'm not very good at this site -->
<input type="submit" value="Submit" />
</form>

在上面的代码中,它使用带有attribute方法和action的form元素。 方法告诉客户端它将在POST或更安全的GET版本中运行。 可以在这样的URL中看到GET: https//mywebsite.co/index.php?input = Hi
与GET不同,POST无法在URL中看到,因此很难进行干预。 换句话说,POST更安全使用。 动作表示数据将被发送到的文件。 服务器将处理数据,并将其解释为代码(如果提供)。 INPUT标记必须具有“名称”,以便服务器可以接收它们。 “类型”表示输入将是常规的“文本”,“密码”还是“提交”。 提交是一个按钮,它将告诉表单在单击时发送数据。 占位符是TEXT和PASSWORD的输入属性,当值为null时将显示指定的输入。 值是属性,它基本上包含您希望服务器接收的部分。 除了按钮,提交。 SUBMIT按钮的值只是该按钮将显示的文本。 您不从按钮本身收集数据。

@Ty Q.的答案是最好的方法,但是在回顾了SmtpJS之后 ,这就是您的使用方式:

 <html> <head> </head> <body> <form id="form"> <!--form goes here--> <input type="submit" value="Submit" /> </form> <script src="http://smtpjs.com/smtp.js"></script> <script> function sendMail(e){ event.preventDefault(); console.log('This will send the mail through SmtpJS'); Email.send("from@you.com", "to@them.com", "This is a subject", "this is the body", "smtp.yourisp.com", "username", "password"); } function init(){ document.getElementById('form').onsubmit = sendMail; } window.onload = init; </script> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM