我想在我的网站上添加联系人,所以我在互联网上搜索了一个例子我找到了这个例子:
http://www.html-form-guide.com/contact-form/php-email-contact-form.html
我遵循了这个例子,但它不起作用,我的邮箱里没有电子邮件,但它在提交后将我重定向到感谢页面。
我已将contact-form-handler.php更改为
<?php
$errors = '';
$myemail = 'myEmail@yahoo.fr';//<-----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: $myemailn";
$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');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact form handler</title>
</head>
<body>
<?php
echo nl2br($errors);
?>
</body>
</html>
My contact.html file:
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
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>
和其他页面contact-form-tank-you.html
<div id="body">
<div class="content">
<h1>Thank you!</h1>
Thank you for submitting the form. We will contact you soon!
</div>
</div>
我常用的程序有:http,wampserver,php 5.3.10
我已经打开了我的php.ini,这是我发现的:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.yahoo.fr
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = myMail@yahoo.fr
我已经记录到logs/appache_error,我发现了这个错误:
[Thu Dec 27 10:11:51 2012] [error] [client 127.0.0.1] PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. dm3sm56414314wib.9 in C:\wamp\www\MyWebsitname\en\contact-form-handler.php on line 32, referer: http://localhost/MyWebsitname/en/contact.html
所以我在网上搜索了一下,我发现我应该加上
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
但问题是一样的?
mail()函数有时不工作有很多原因,请阅读这篇文章PHP mail()不能't work
我强烈推荐使用PHPMailer库,它是免费的,易于使用,它比本地php mail()函数更可靠
我想你错过了电子邮件配置。
你可以在php.ini中配置这个,见下面的例子:http://www.quackit.com/php/tutorial/php_mail_configuration.cfm
您将需要一个SMTP服务器,最简单的方法是使用免费服务,如mandrill (http://mandrill.com/)。注册一个帐户,您将看到SMTP详细信息。在php.ini中输入这些详细信息,电子邮件将通过SMTP服务器从mandrill发送。
或者你可以设置一个本地SMTP服务器,但我想这会更麻烦:)