简体   繁体   English

如何通过HTML和JavaScript发送电子邮件?

[英]How do I send email through HTML and JavaScript?

I have a contact form on my website and I wanted to make a send button. 我的网站上有联系表格,我想发一个发送按钮。 I would not want an e-mail program on the computer to start, I just want the text to be sent to my e-mail right away by just pressing the button. 我不希望计算机上的电子邮件程序启动,我只想按下按钮立即将文本发送到我的电子邮件。 I have searched for weeks now on the internet and I am giving up. 我现在已经在互联网上搜索了几周而且我放弃了。

<form method="post" name="contact" action="#">
    <label for="author">Name:</label>
    <input type="text" id="author" name="author" class="required input_field" />
    <div class="cleaner h10"></div>

    <label for="email">Email:</label>
    <input type="text" class="validate-email required input_field" name="email" id="email" />
    <div class="cleaner h10"></div>

    <label for="subject">Subject:</label>
    <input type="text" class="validate-subject required input_field" name="subject" id="subject"/>                             
    <div class="cleaner h10"></div>

    <label for="text">Message:</label>
    <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
    <div class="cleaner h10"></div>             

    <input type="submit" value="Send" id="submit" name="submit" class="submit_btn float_l" />
    <input type="reset" value="Reset" id="reset" name="reset" class="submit_btn float_r" />
</form>

Maybe if you don't want to use php , you may try just use an external API to provide you the email to be sent. 也许如果您不想使用php ,您可以尝试使用外部API为您提供要发送的电子邮件。

Mandrill can do that. Mandrill可以做到这一点。 It's free up to 12k emails per month. 每月最多可免费发送12k封电子邮件。

In you page the code would be like this: 在您的页面中,代码将如下所示:

$.ajax({
  type: “POST”,
  url: “https://mandrillapp.com/api/1.0/messages/send.json”,
  data: {
    ‘key’: ‘YOUR API KEY HERE’,
    ‘message’: {
      ‘from_email’: ‘YOUR@EMAIL.HERE’,
      ‘to’: [
          {
            ‘email’: ‘RECIPIENT_NO_1@EMAIL.HERE’,
            ‘name’: ‘RECIPIENT NAME (OPTIONAL)’,
            ‘type’: ‘to’
          },
          {
            ‘email’: ‘RECIPIENT_NO_2@EMAIL.HERE’,
            ‘name’: ‘ANOTHER RECIPIENT NAME (OPTIONAL)’,
            ‘type’: ‘to’
          }
        ],
      ‘autotext’: ‘true’,
      ‘subject’: ‘YOUR SUBJECT HERE!’,
      ‘html’: ‘YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!’
    }
  }
 }).done(function(response) {
   console.log(response); // if you're into that sorta thing
 });

Here how: 这里如何:

https://medium.com/design-startups/b53319616782 https://medium.com/design-startups/b53319616782

http://www.codecademy.com/tracks/mandrill (CodeCademy can teach how to use the API) http://www.codecademy.com/tracks/mandrill(CodeCademy可以教授如何使用API​​)

This maybe what are you searching for: http://www.sanwebe.com/2011/12/making-simple-jquery-ajax-contact-form . 这也许你在寻找什么: http//www.sanwebe.com/2011/12/making-simple-jquery-ajax-contact-form You can only do what do you want by using PHP and HTML + AJAX. 您只能使用PHP和HTML + AJAX执行您想要的操作。 Create the form in HTML and than send the request + data using Jquery POST request like this: 用HTML创建表单,然后使用Jquery POST请求发送请求+数据,如下所示:

        $.post('sendmail.php', post_data, function(response){  ... }

The process of sending a mail happens server-side, HTML/JavaScript is client-side. 发送邮件的过程发生在服务器端,HTML / JavaScript是客户端的。 As far as I can see you're not using a web-server nor are you hosting the website somewhere. 据我所知,你没有使用网络服务器,也没有在某个地方托管网站。

There is a FakeSendMail option with the new install from XAMPP which you could use to emulate the mail() function from PHP. 使用XAMPP的新安装有一个FakeSendMail选项,您可以使用它来模拟PHP中的mail()函数。 XAMPP is one of the most known localhost web servers , which you could use to finish your project, in case you don't really need that mail to be actually sent. XAMPP是最着名的localhost Web服务器之一,您可以使用它来完成项目,以防您真的不需要实际发送邮件。 If you do, I recommend using a webhosting. 如果您这样做,我建议使用虚拟主机。

But first you need to understand the difference between Client-Side and Server-Side. 但首先,您需要了解客户端和服务器端之间的区别。 As far as Client Side is concerned, all it does is render your static data. 就客户端而言,它所做的就是呈现静态数据。 (HTML/CSS/JS). (HTML / CSS / JS)。 But, as for Server Side, there is a lot more use to it, as you can work with a database, fetch and insert data from or to it, and eventually render data which will be processed by the Browser (client side) 但是,对于服务器端,它有更多的用途,因为您可以使用数据库,从中获取或插入数据,并最终呈现将由浏览器(客户端)处理的数据

I don't think you 'searched' the net for weeks either :) After one google search I got the following: https://medium.com/design-startups/b53319616782 我不认为你在网上搜索了几个星期:)在谷歌搜索之后我得到了以下内容: https//medium.com/design-startups/b53319616782

Thechnically it is not possible to send emails with javascript alone. 技术上不可能单独发送带有javascript的电子邮件。 You always need a backend service. 您总是需要后端服务。 But as described in the link above, there are mostly free (depends on volume) services available (as mentioned in the article above: https://mandrill.com/ ). 但如上面的链接所述,主要有免费(取决于数量)服务(如上文所述: https//mandrill.com/ )。

here is the code example of the link above: 这是上面链接的代码示例:

$.ajax({
  type: 'POST',
  url: 'https://mandrillapp.com/api/1.0/messages/send.json',
  data: {
    'key’: 'YOUR API KEY HERE’,
    'message': {
      'from_email': 'YOUR@EMAIL.HERE',
      'to': [
          {
            'email': 'RECIPIENT_NO_1@EMAIL.HERE',
            'name': 'RECIPIENT NAME (OPTIONAL)',
            'type': 'to'
          },
          {
            'email': 'RECIPIENT_NO_2@EMAIL.HERE',
            'name': 'ANOTHER RECIPIENT NAME (OPTIONAL)',
            'type': 'to'
          }
        ],
      'autotext': 'true',
      'subject': 'YOUR SUBJECT HERE!',
      'html': 'YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!'
    }
  }
}).done(function(response) {
    console.log(response); // if you're into that sorta thing
});

Hope this helps. 希望这可以帮助。

Best regards, Chris 最好的问候,克里斯

I am being lazy and copied shamelessly from W3Schools (Please don't go and say how crappy W3schools is, I know but this serves the purpose!)> 我是懒惰的,并且从W3Schools无耻地复制(请不要去说W3schools是多么糟糕,我知道,但这是有用的!)>

<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
  ?>
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  From: <input type="text" name="from"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  <input type="submit" name="submit" value="Submit Feedback">
  </form>
  <?php 
} else {    // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["from"])) {
    $from = $_POST["from"]; // sender
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    // message lines should not exceed 70 characters (PHP rule), so wrap it
    $message = wordwrap($message, 70);
    // send mail
    mail("webmaster@example.com",$subject,$message,"From: $from\n");
    echo "Thank you for sending us feedback";
  }
}
?>

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

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