如何在电子邮件中发送 HTML 表单 .. 不仅仅是 MAILTO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7449767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:39:05  来源:igfitidea点击:

How do I send an HTML Form in an Email .. not just MAILTO

htmlforms

提问by Joe W

I have an HTML form for people to fill out, and I want it so when they click the submit button, it will just send the email, not bring up their email and ask them to send the message themselves.

我有一个供人们填写的 HTML 表单,我想要这样当他们单击提交按钮时,它只会发送电子邮件,而不是打开他们的电子邮件并要求他们自己发送消息。

When I use:

当我使用:

<form action="MAILTO:[email protected]"... >

All that does is open up a new window and populates the body of the email, but I want it to just send an email.

所做的只是打开一个新窗口并填充电子邮件的正文,但我希望它只发送一封电子邮件。

And is there a way to format the output of what the email will look like? Instead of just a list of the field names and the entered value.

有没有办法格式化电子邮件的输出格式?而不仅仅是字段名称和输入值的列表。

Thanks.

谢谢。

采纳答案by Joe W

I actually use ASP C# to send my emails now, with something that looks like :

我现在实际上使用 ASP C# 发送我的电子邮件,内容如下:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Form.Count > 0)
    {
        string formEmail = "";
        string fromEmail = "[email protected]";
        string defaultEmail = "[email protected]";

        string sendTo1 = "";

        int x = 0;

        for (int i = 0; i < Request.Form.Keys.Count; i++)
        {
            formEmail += "<strong>" + Request.Form.Keys[i] + "</strong>";
            formEmail += ": " + Request.Form[i] + "<br/>";
            if (Request.Form.Keys[i] == "Email")
            {
                if (Request.Form[i].ToString() != string.Empty)
                {
                    fromEmail = Request.Form[i].ToString();
                }
                formEmail += "<br/>";
            }

        }
        System.Net.Mail.MailMessage myMsg = new System.Net.Mail.MailMessage();
        SmtpClient smtpClient = new SmtpClient();

        try
        {
            myMsg.To.Add(new System.Net.Mail.MailAddress(defaultEmail));
            myMsg.IsBodyHtml = true;
            myMsg.Body = formEmail;
            myMsg.From = new System.Net.Mail.MailAddress(fromEmail);
            myMsg.Subject = "Sent using Gmail Smtp";
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "pward");

            smtpClient.Send(defaultEmail, sendTo1, "Sent using gmail smpt", formEmail);

        }
        catch (Exception ee)
        {
            debug.Text += ee.Message;
        }
    }
}

This is an example using gmail as the smtp mail sender. Some of what is in here isn't needed, but it is how I use it, as I am sure there are more effective ways in the same fashion.

这是一个使用 gmail 作为 smtp 邮件发件人的示例。这里的一些内容不是必需的,但这是我使用它的方式,因为我确信有更有效的方法。

回答by nelsonic

> 2020 Answer= The EasyWay using Google Apps Script (5 Mins)

> 2020 答案=使用 Google Apps 脚本的简单方法(5 分钟

We had a similarchallenge to solve yesterday, and we solved it using a Google Apps Script!

昨天我们遇到了一个类似的挑战,我们使用 Google Apps 脚本解决了它!

Send Email From an HTML Form Withouta Backend (Server) via Google!

通过 Google从没有后端(服务器)的 HTML 表单发送电子邮件!

The solution takes 5 minsto implement and I've documented with step-by-step instructions: https://github.com/nelsonic/html-form-send-email-via-google-script-without-server

该解决方案需要5分钟来实现,我已经记录有一步一步的指示https://github.com/nelsonic/的HTML表单发送的电子邮件-通过-谷歌脚本-无需服务器

Brief Overview

简要概述

A. Using the sample script, deploy a Google App Script

A. 使用示例脚本,部署 Google App Script

Deploy the sample script as a Google Spreadsheet APP Script: google-script-just-email.js

将示例脚本部署为 Google 电子表格应用程序脚本: google-script-just-email.js

3-script-editor-showing-script

3-script-editor-showing-script

rememberto set the TO_ADDRESSin the script to where ever you want the emails to be sent.
and copy the APP URLso you can use it in the next step when you publish the script.

请记住TO_ADDRESS将脚本中的设置为您希望将电子邮件发送到的位置。
并复制APP URL,以便在下一步发布脚本时使用它。

B. Create your HTML Form and Set the actionto the App URL

B. 创建您的 HTML 表单并将其设置action为应用程序 URL

Using the sample htmlfile: index.htmlcreate a basic form.

使用示例html文件: index.html创建一个基本表单。

7-html-form

7-html-form

rememberto paste your APP URLinto the form actionin the HTML form.

记得将您的APP URL粘贴到actionHTML 表单中的表单中。

C. Test the HTML Form in your Browser

C. 在浏览器中测试 HTML 表单

Open the HTML Form in your Browser, Input some data & submit it!

在浏览器中打开 HTML 表单,输入一些数据并提交!

html form

html表单

Submit the form. You should see a confirmation that it was sent: form sent

提交表格。您应该会看到已发送的确认信息: 表格发送

Open the inbox for the email address you set (above)

打开您设置的电子邮件地址的收件箱(上图

email received

收到电子邮件

Done.

完毕。

Everythingabout this is customisable, you can easilystyle/themetheformwith your favourite CSSLibrary andStorethe submitted datain a Google Spreadsheetfor quickanalysis.
The complete instructionsare available on GitHub:
https://github.com/nelsonic/html-form-send-email-via-google-script-without-server

一切有关这是定制的,你可以很容易的风格/主题形式,用你喜欢的CSS图书馆 商店提交的数据谷歌电子表格进行快速分析。
完整说明,可在GitHub上
https://github.com/nelsonic/的HTML表单发送的电子邮件-通过-谷歌脚本-无需服务器

回答by mingos

You are making sense, but you seem to misunderstand the concept of sending emails.

您说得很有道理,但您似乎误解了发送电子邮件的概念。

HTML is parsed on the client side, while the e-mail needs to be sent from the server. You cannot do it in pure HTML. I would suggest writing a PHP script that will deal with the email sending for you.

HTML 在客户端解析,而电子邮件需要从服务器发送。你不能在纯 HTML 中做到这一点。我建议编写一个 PHP 脚本来处理为您发送的电子邮件。

Basically, instead of the MAILTO, your form's action will need to point to that PHP script. In the script, retrieve the values passed by the form (in PHP, they are available through the $_POSTsuperglobal) and use the email sending function (mail()).

基本上,您的表单的操作将需要指向该 PHP 脚本,而不是 MAILTO。在脚本中,检索表单传递的值(在 PHP 中,它们可通过$_POSTsuperglobal 获得)并使用电子邮件发送函数 ( mail())。

Of course, this can be done in other server-side languages as well. I'm giving a PHP solution because PHP is the language I work with.

当然,这也可以用其他服务器端语言来完成。我给出了一个 PHP 解决方案,因为 PHP 是我使用的语言。

A simple example code:

一个简单的示例代码:

form.html:

表单.html:

<form method="post" action="email.php">
    <input type="text" name="subject" /><br />
    <textarea name="message"></textarea>
</form>

email.php:

电子邮件.php:

<?php
    mail('[email protected]', $_POST['subject'], $_POST['message']);
?>
<p>Your email has been sent.</p>

Of course, the script should contain some safety measures, such as checking whether the $_POST valies are at all available, as well as additional email headers (sender's email, for instance), perhaps a way to deal with character encoding - but that's too complex for a quick example ;).

当然,脚本应该包含一些安全措施,例如检查 $_POST valies 是否完全可用,以及额外的电子邮件标题(例如发件人的电子邮件),也许是一种处理字符编码的方法 - 但那也是复杂的一个简单的例子;)。

回答by raydowe

I don't know that what you want to do is possible. From my understanding, sending an email from a web form requires a server side language to communicate with a mail server and send messages.

我不知道你想做的事情是可能的。根据我的理解,从 Web 表单发送电子邮件需要服务器端语言来与邮件服务器通信并发送消息。

Are you running PHP or ASP.NET?

您运行的是 PHP 还是 ASP.NET?

ASP.NET Example

ASP.NET 示例

PHP Example

PHP 示例

回答by Guente Anijo Guente

<FORM Action="mailto:xyz?Subject=Test_Post" METHOD="POST">
    mailto: protocol test:
    <Br>Subject: 
    <INPUT name="Subject" value="Test Subject">
    <Br>Body:&#xa0;
    <TEXTAREA name="Body">
    kfdskfdksfkds
    </TEXTAREA>
    <BR>
    <INPUT type="submit" value="Submit"> 
</FORM>