如何创建可以使用 html 发送电子邮件的电子邮件表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8239782/
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 11:38:03  来源:igfitidea点击:

How to create an email form that can send email using html

html

提问by dfdfd

I know that are a lot of examples using the mailto: post action to send emails using just html form.

我知道有很多使用 mailto: post 操作仅使用 html 表单发送电子邮件的示例。

But using that will actually, popup the send email dialog box e.g. outlook dialog box and it is actually using our own smtp server to send the email.

但是使用它实际上会弹出发送电子邮件对话框,例如 Outlook 对话框,它实际上是使用我们自己的 smtp 服务器来发送电子邮件。

Is there anyway to create html form that will simply send email upon submission?

有没有办法创建html表单,在提交时简单地发送电子邮件?

E.g. if using other javascript api can achieve the effect? say for e.g. node.js?

比如如果使用其他javascript api可以达到效果吗?说例如node.js?

Can anyone provide some code samples?

谁能提供一些代码示例?

回答by Yisela

As the others said, you can't. You can find good examples of HTML-php forms on the web, here's a very useful link that combines HTML with javascript for validation and php for sending the email.

正如其他人所说,你不能。您可以在网络上找到 HTML-php 表单的好例子,这里有一个非常有用的链接,它结合了 HTML 和用于验证的 javascript 以及用于发送电子邮件的 php。

Please check the full article (includes zip example) in the source: http://www.html-form-guide.com/contact-form/php-email-contact-form.html

请查看源中的全文(包括 zip 示例):http: //www.html-form-guide.com/contact-form/php-email-contact-form.html

HTML:

HTML:

<form method="post" name="contact_form"
action="contact-form-handler.php">
    Your Name:
    <input type="text" name="name">
    Email Address:
    <input type="text" name="email">
    Message:
    <textarea name="message"></textarea>
    <input type="submit" value="Submit">
</form>

JS:

JS:

<script language="JavaScript">
var frmvalidator  = new Validator("contactform");
frmvalidator.addValidation("name","req","Please provide your name");
frmvalidator.addValidation("email","req","Please provide your email");
frmvalidator.addValidation("email","email",
  "Please enter a valid email address");
</script>

PHP:

PHP:

<?php
$errors = '';
$myemail = '[email protected]';//<-----Put Your email address here.
if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>

回答by Antony Raphel

you can use Simple Contact Form in HTML with PHP mailer. It's easy to implement in you website. You can try the demo from following link: Simple Contact/Feedback Form in HTML-PHP mailer

您可以使用带有 PHP 邮件程序的 HTML 格式的简单联系表单。它很容易在您的网站中实施。您可以从以下链接尝试演示:HTML-PHP 邮件程序中的简单联系/反馈表

Otherwise you can watch the demo video in following link: Youtube: Simple Contact/Feedback Form in HTML-PHP mailer

否则,您可以在以下链接中观看演示视频:Youtube:HTML-PHP 邮件程序中的简单联系/反馈表

When you are running in localhost, you may get following error:

当您在 localhost 中运行时,您可能会收到以下错误:

Warning: mail(): Failed to connect to mailserver at "smtp.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in S:\wamp\www\sample\mailTo.php on line 10

警告:mail(): 无法在 "smtp.gmail.com" 的 25 端口连接到邮件服务器,请验证 php.ini 中的 "SMTP" 和 "smtp_port" 设置或在 S:\wamp\www\sample 中使用 ini_set() \mailTo.php 第 10 行

You can check in this link for more detailed information: Simple Contact/Feedback Form in HTML with php (HTML-PHP mailer)And this is the screenshot of HTML form: Simple Form

您可以在此链接中查看更多详细信息:使用 php 的 HTML 格式的简单联系/反馈表单(HTML-PHP 邮件程序)这是 HTML 表单的屏幕截图: 简单的形式

And this is the main PHP coding:

这是主要的 PHP 编码:

<?php
if($_POST["submit"]) {
    $recipient="[email protected]"; //Enter your mail address
    $subject="Contact from Website"; //Subject 
    $sender=$_POST["name"];
    $senderEmail=$_POST["email"];
    $message=$_POST["comments"];
    $mailBody="Name: $sender\nEmail Address: $senderEmail\n\nMessage: $message";
    mail($recipient, $subject, $mailBody);
    sleep(1);
    header("Location:http://blog.antonyraphel.in/sample/"); // Set here redirect page or destination page
}
?>

回答by Christophe Debove

You can't, the only things you can do with html is open your default email application. You must use a server code to send an email, php, asp .net....

你不能,你唯一能用 html 做的事情就是打开你的默认电子邮件应用程序。您必须使用服务器代码来发送电子邮件、php、asp .net....

回答by robert

You can't send email using javascript or html. You need server side scripts in php or other technologies to send email.

您不能使用 javascript 或 html 发送电子邮件。您需要使用 php 或其他技术的服务器端脚本来发送电子邮件。

回答by Sid Malani

Html by itself will not send email. You will need something that connects to a SMTP server to send an email. Hence Outlook pops up with mailto: else your form goes to the server which has a script that sends email.

Html 本身不会发送电子邮件。您将需要连接到 SMTP 服务器的东西才能发送电子邮件。因此 Outlook 会弹出 mailto:否则您的表单将转到具有发送电子邮件的脚本的服务器。

回答by LOLWTFasdasd asdad

As many answers in this thread already suggest it is not possible to send a mail from a static HTML page without using PHP or JS. I just wanted to add that there a some great solutions which will take your HTTP Post request generated by your form and create a mail from it. Those solutions are especially useful in case you do not want to add JS or PHP to your website.

由于该线程中的许多答案已经表明,不使用 PHP 或 JS 就不可能从静态 HTML 页面发送邮件。我只是想补充一点,有一些很棒的解决方案,它们将采用表单生成的 HTTP Post 请求并从中创建邮件。如果您不想将 JS 或 PHP 添加到您的网站,这些解决方案特别有用。

Those servers basically can be configured with a mail-server which is responsible for then sending the email. The receiver, subject, body etc. is received by the server from your HTTP post and then stuffed into the mail you want to send. So technically speaking it is still not possible to send mails from your HTML form but the outcome is the same.

这些服务器基本上可以配置一个负责发送电子邮件的邮件服务器。接收者、主题、正文等由服务器从您的 HTTP 帖子接收,然后塞入您要发送的邮件中。因此,从技术上讲,仍然无法从 HTML 表单发送邮件,但结果是相同的。

Some of these solutions can be bought as SaaS solution or you can host them by yourself. I'll just name a few but I'm sure there are plenty in case anyone is interested in the technology or the service itself.

其中一些解决方案可以作为 SaaS 解决方案购买,也可以自己托管。我只举几个例子,但我相信有很多,以防有人对技术或服务本身感兴趣。

回答by Mehdi Bounya

Short answer, you can't.

简短的回答,你不能。

HTML is used for the page's structure and can't send e-mails, you will need a server side language (such as PHP) to send e-mails, you can also use a third party service and let them handle the e-mail sending for you.

HTML用于页面的结构,不能发送电子邮件,您需要一种服务器端语言(例如PHP)来发送电子邮件,您也可以使用第三方服务并让他们处理电子邮件送给你。