从我的联系表单发送电子邮件不起作用

时间:2012-12-24 13:33:55

标签: php html wampserver

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

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

但问题是一样的吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我认为您错过了电子邮件配置。

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

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

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