1

我想在我的网站上添加联系人,所以我在互联网上搜索了一个示例,我找到了这个示例:

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");

但问题是一样的吗?

4

2 回答 2

1

mail() 函数有时不起作用的原因有很多,请阅读这篇文章 PHP mail() doesn't work

我强烈推荐使用 PHPMailer Library,它是免费的,易于使用,并且比原生的 php mail() 函数更可靠

于 2012-12-24T14:42:34.630 回答
0

我认为您缺少电子邮件配置。

您可以在 php.ini 中进行配置,请参见此处的示例:http ://www.quackit.com/php/tutorial/php_mail_configuration.cfm

您将需要一个 SMTP 服务器,最简单的方法是使用像 mandrill (http://mandrill.com/) 这样的免费服务。注册一个帐户,您将看到 SMTP 详细信息。在您的 php.ini 中输入这些详细信息,电子邮件将通过 SMTP 服务器从 mandril 发送。

或者,您可以设置本地 SMTP 服务器,但我想这会更麻烦 :)

于 2012-12-24T13:42:24.957 回答