2011-11-23 159 views
36

我知道很多使用mailto:post操作的例子都是使用html表单发送电子邮件。如何创建一个可以使用html发送电子邮件的电子邮件表格

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

有无论如何创建HTML表单,只需提交发送电子邮件?

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

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

回答

3

Html本身不会发送电子邮件。您需要连接到SMTP服务器才能发送电子邮件。因此,Outlook弹出与mailto:否则你的表单到服务器有一个脚本发送电子邮件。

4

你不能,只有你可以用HTML做的事情是打开你的默认电子邮件应用程序。您必须使用服务器代码发送电子邮件,php,asp .net ....

3

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

38

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

请检查完整的文章(包括拉链例子)在源: 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 = '[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'); 
} 
?> 
+0

如果你阅读提供的链路上的评论,也没有送出了结果的电子邮件上面的代码。 – Tempo

2

你不能,HTML不能发送邮件,你需要一个服务器端语言(例如:PHP,ASP ..)。

5

您可以使用HTML邮箱中的简单联系人表单。在你的网站上实现很容易。你可以尝试从以下链接演示: Simple Contact/Feedback Form in HTML-PHP mailer

否则,您可以观看下面的链接演示视频:Youtube: Simple Contact/Feedback Form in HTML-PHP mailer

当你在本地主机上运行,​​你可能会得到以下错误:

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

您可以在此链接中查看更详细的信息:Simple Contact/Feedback Form in HTML with php (HTML-PHP mailer)这是HTML表格的屏幕截图: Simple Form

这是主要的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 
} 
?>