【问题标题】:How to send to an email in HTML? [duplicate]如何以 HTML 格式发送到电子邮件? [复制]
【发布时间】:2021-09-05 23:21:31
【问题描述】:

我想创建一个表单,在单击提交按钮时将表单的输入发送到所需的电子邮件。理想情况下,我想使用 HTML。

这是我目前得到的:

<form class="row g-3 needs-validation" novalidate 
    action=”mailto:myemail@gmail.com”
    method=”POST”
    enctype=”multipart/form-data”
    name=”EmailForm”>
        <div class="col-md-4" style="margin-top: 2.4%">
            <div class="valid-feedback">
            </div>
        </div>
        <div class="col-md-4">
            <label for="floatingInput" class="form-label">First name</label>
            <input type="text" class="form-control" id="floatingInput" placeholder="Konnie"></div><br>
        <div class="col-md-4">
            <label for="floatingInput" class="form-label">Last name</label>
            <input type="text" class="form-control" id="floatingInput" placeholder="Smith" required>
        </div><br>
        <div class="form-floating mb-3">
                <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com" required>
                <label for="floatingInput">Email address</label>
        </div>
  <div class="col-12"> <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
      <label class="form-check-label" for="invalidCheck">
        Agree to terms and conditions
      </label> 
      <div class="invalid-feedback">
        You must agree before submitting.
      </div>
    </div>
  </div>
  <br>
  <div class="col-12">
    <button class="btn btn-primary" type="submit">Submit form</button>
  </div>
        </form>

当我尝试提交表单时,我收到此错误:

Not Found

The requested URL was not found on this server.

【问题讨论】:

  • 你试过method="get"吗?
  • 您是否使用任何后端语言?喜欢 PHP 吗?
  • 当然,如果用户没有注册的邮件客户端(例如没有响应mailto协议的程序),它当然不会工作。
  • 您不能自动发送带有纯 html 表单的电子邮件 - 这只会打开用户的 mailto 处理程序
  • 另外,你试过this question的表格吗?如果是,请尝试根据您的需要逐步修改它,看看错误发生在哪里

标签: html form-submit


【解决方案1】:

按照以下示例发送电子邮件:

创建一个文件 index.html 并放入以下内容:

<form method="post" action="subscriberform.php">
 <textarea name="message"></textarea>
 <input type="submit">
</form>

创建一个 PHP 文件 subscribeform.php 并输入以下内容:

<?php

if($_POST["message"]) {

 mail("your@email.address", "Here is the subject line", $_POST["insert your message here"]. "From: an@email.address");

}
?>

现在将这些文件上传到您的主机上并尝试一下。 如果未发送电子邮件,请联系您的托管服务提供商了解您的服务器配置。

【讨论】:

    【解决方案2】:

    不幸的是,在执行诸如发送电子邮件之类的操作时,不可能只使用 HTML。一种可能的方法是创建这样的输入:

    <input id="receiver" />
    <input id="text" />
    <button onclick="sendMail()">Send</button>
    

    还有一个发送邮件的按钮。然后在你的 HTML 中添加一个隐藏的链接标签:

    <a id="sender" style="display: none;">Sender</a>
    

    现在将此脚本添加到您的 HTML:

    function sendMail() {
      document.getElementById("sender").href = "mailto:" + document.getElementById("receiver").value + "&body=" + encodeURIComponent(document.getElementById("text").value;
      document.getElementById("sender").click();
    }
    

    这将打开用户的邮件程序,用户只需在他的邮件程序中点击“发送”。其他所有内容都是预先填写的。

    【讨论】:

    • Afaik,没有 javascript 也是可能的,例如使用来自this answer 的代码 sn-p
    【解决方案3】:

    <form class="row g-3 needs-validation" novalidate action="mailto:myemail@gmail.com” method=”post”
        enctype=”multipart/form-data” name=”EmailForm”>
        <div class=" col-md-4" style="margin-top: 2.4%">
        <div class="valid-feedback">
        </div>
        </div>
        <div class="col-md-4">
            <label for="floatingInput" class="form-label">First name</label>
            <input type="text" class="form-control" id="floatingInput" placeholder="Konnie">
        </div><br>
        <div class="col-md-4">
            <label for="floatingInput" class="form-label">Last name</label>
            <input type="text" class="form-control" id="floatingInput" placeholder="Smith" required>
        </div><br>
        <div class="form-floating mb-3">
            <input type="email" class="form-control" id="floatingInput" placeholder="name@example.com" required>
            <label for="floatingInput">Email address</label>
        </div>
        <div class="col-12">
            <div class="form-check">
                <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
                <label class="form-check-label" for="invalidCheck">
                    Agree to terms and conditions
                </label>
                <div class="invalid-feedback">
                    You must agree before submitting.
                </div>
            </div>
        </div>
        <br>
        <div class="col-12">
            <button class="btn btn-primary" type="submit">Submit form</button>
        </div>
    </form>

    【讨论】:

    • 您能解释一下为什么这应该有效,而 OP 中的示例无效吗?我猜这是使用的引号类型
    • 在动作属性引用之前是 this ”,我将引用替换为 this “所以代码正常工作
    猜你喜欢
    • 2012-01-27
    • 1970-01-01
    • 2015-08-04
    • 2010-11-26
    • 2014-06-22
    相关资源
    最近更新 更多