如何创建一个可以使用html发送电子邮件的电子邮件表单 [英] How to create an email form that can send email using html

查看:160
本文介绍了如何创建一个可以使用html发送电子邮件的电子邮件表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很多使用mailto:post action的例子都是使用html表单发送电子邮件。



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



是否有创建HTML表单的方式,只需在提交时发送电子邮件?



例如如果使用其他的JavaScript API可以达到效果?对于例如node.js?



任何人都可以提供一些代码示例吗?

解决方案

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

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


HTML:

 < form method = postname =contact_form
action =contact-form-handler.php>
您的姓名:
< input type =textname =name>
电子邮件地址:
< input type =textname =email>
消息:
< textarea name =message>< / textarea>
< input type =submitvalue =提交>
< / form>

JS:

 < script language =JavaScript> 
var frmvalidator = new Validator(contactform);
frmvalidator.addValidation(name,req,请提供您的姓名);
frmvalidator.addValidation(email,req,请提供您的电子邮件);
frmvalidator.addValidation(email,email,
请输入一个有效的电子邮件地址);
< / script>

PHP:

 <?php 
$ errors ='';
$ myemail ='yourname@website.com'; //< -----将您的电子邮件地址放在这里。
if(empty($ _ POST ['name'])||
empty($ _ POST ['email'])||
empty($ _ POST ['message']))
{
$ errors。=\\\
错误:所有字段都是必需的;
}
$ 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 - ] +)*(\。[az] {2,3})$ / i,
$ email_address))
{
$ errors。=\\\
错误:无效的电子邮件地址;


if(empty($ errors))
{
$ to = $ myemail;
$ email_subject =联系表单提交:$ name;
$ email_body =您收到了一条新消息。
以下是详细信息:\ n名称:$ name \\\

Email:$ email_address \\\
Message \\\
$ message;
$ headers =From:$ myemail\\\
;
$ headers。=回复:$ email_address;
邮件($ to,$ email_subject,$ email_body,$ headers);
//重定向到'thank you'页面
header('Location:contact-form-thank-you.html');
}
?>


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

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.

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

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

Can anyone provide some code samples?

解决方案

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.

Please check the full article (includes zip example) in the source: http://www.html-form-guide.com/contact-form/php-email-contact-form.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:

<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
$errors = '';
$myemail = 'yourname@website.com';//<-----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');
}
?>

这篇关于如何创建一个可以使用html发送电子邮件的电子邮件表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆