我想在我的网站上添加联系人,所以我在互联网上搜索了一个示例,我找到了这个示例:
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: $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');
}
?>
<!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>
我的contact.html文件:
<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>
我使用的程序是:htmp ,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");
但问题是一样的吗?