PHP联系表单提交但未收到电子邮件

时间:2013-08-14 09:05:26

标签: php html form-submit contact-form

我意识到这个问题之前已被多次询问,但每个人的代码明显不同,我对php很新,所以只是想看看有人能给我一些帮助。

我为网站创建了一个基本联系表单但由于某种原因,虽然我认为该表单已提交,但该信息未发送到我的电子邮件地址?

我的PHP代码是:

<?php
session_start();
//$to_mail = "architects@palavin.com,t.lavin@palavin.com,12yorkcourt@gmail.com";
$to_mail = "danny@enhance.ie";
//$cc="paul@enhance.ie";
$mail_sent = 0;
if(isset($_POST['submit'])){
    //echo "the form was submitted";

$error= array();

$name = trim(strip_tags($_POST['name']));
if($name == "")
    $error['name'] = 1;

$email = trim(strip_tags($_POST['email']));
if($email == "")
    $error['email'] = 1;

$phone = trim(strip_tags($_POST['phone']));

$address = trim(strip_tags($_POST['address']));

$description = trim(strip_tags($_POST['description']));

$str = trim(strip_tags($_POST['secu']));
if ( isset($_SESSION['code_']) && $_SESSION['code_'] == strtoupper($str)){} else {$error['secu'] = 1;}


if(empty($error)){
    $headers = 'From: "Euro Insulation" <no-reply@euroinsulations.ie>'."\r\n";
    //$headers .= 'CC: "'.$cc.'" <'.$cc.'>'."\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "";



    $subject = "New contact message";

    $message = "New Contact message, received from: <br /> \n ";
    $message .= "<b>Name</b> ".$name."<br /> \n";
    $message .= "<b>Email</b> ".$email."<br /> \n";

    $message .= "<b>Phone</b> ".$phone."<br /> \n";
    $message .= "<b>Address</b> ".$address."<br /> \n";

    $message .= "<b>Description</b> ".$description."<br /> \n";

    if(@mail($to_mail,$subject,$message,$headers ))
    {
        echo "mail sent";
        $mail_sent = 1;
    }
    else echo "mail not sent";
}

}

?>

我的html表单如下所示:

<table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td width="65%" valign="top"><p class="header"><br>
        Contact US <br>
                  </p>
                      <?php if($mail_sent==1){
    print "Thank you for your message.";
} else { ?>
<form class="email_sub" method="post" >

<table width="77%"  border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><label for="name" class="formtext" <?php if($error['name']==1) echo "style='color:red;'" ?> >Name:</label></td>
<td><input type="text" name="name" id="text"  <?php if($name) echo "value='".$name."'" ?>  /></td>
</tr>
<tr>
<td><label for="phone" class="formtext">Number:</label></td>
<td><input type="text" name="phone" id="phone"/><tr>
<br />

<tr>
<td><label for="email" class="textarea" <?php if($error['email']==1) echo "style='color:red;'" ?>>Email:</label></td>
<td><input type="text" name="email" id="email"  <?php if($email) echo "value='".$email."'" ?>  /></td>
</tr>
<tr>
<td><br /></td>
</tr>

<tr><td><label for="address" class="textarea">Address/Location of project:</label></td>
<td><textarea rows="3" cols="20" name="address" id="address" style="width: 400px;"><?php if($address!="") echo $address ?></textarea></td>
</tr>
<tr>
<td><br /></td>
</tr>
<br />
<tr>
<td><label for="description" class="fixedwidth">Enquiry</label></td>
<td><textarea rows="3" cols="20" name="description" id="description" style="width: 400px;"><?php if($description!="") echo $description; ?></textarea></td>
<tr>
<td><br /></td>
</tr>

<!-- form -->
<tr>
<td><label>&nbsp;</label></td>
<td><input type="submit" value="Submit" name="submit" /></td>
</tr>
</table>
</form>
<?php } ?>        

我错过了一些明显的东西吗?任何帮助将非常感谢!

3 个答案:

答案 0 :(得分:1)

你已经使用过这里不需要的会话,你也可以用这个简单的形式使用flag变量代替数组,使用这个更新的代码。

<?php
//$to_mail = "architects@palavin.com,t.lavin@palavin.com,12yorkcourt@gmail.com";
$to_mail = "danny@enhance.ie";
//$cc="paul@enhance.ie";
$mail_sent = 0;
if(isset($_POST['submit'])){
    //echo "the form was submitted";

$name = trim(strip_tags($_POST['name']));
if($name == "")
    $error = true;

$email = trim(strip_tags($_POST['email']));
if($email == "")
    $error = true;

$phone = trim(strip_tags($_POST['phone']));
$address = trim(strip_tags($_POST['address']));
$description = trim(strip_tags($_POST['description']));


if($error != true){
    $headers = 'From: "Euro Insulation" <no-reply@euroinsulations.ie>'."\r\n";
    //$headers .= 'CC: "'.$cc.'" <'.$cc.'>'."\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=utf-8' . "";

    $subject = "New contact message";

    $message = "New Contact message, received from: <br /> \n ";
    $message .= "<b>Name</b> ".$name."<br /> \n";
    $message .= "<b>Email</b> ".$email."<br /> \n";

    $message .= "<b>Phone</b> ".$phone."<br /> \n";
    $message .= "<b>Address</b> ".$address."<br /> \n";

    $message .= "<b>Description</b> ".$description."<br /> \n";

    if(@mail($to_mail,$subject,$message,$headers))
    {
        echo "mail sent";
        $mail_sent = 1;
    }
    else echo "mail not sent";
} else {
    echo 'validation error';
}
}
?> 

您还错过了表单验证测试的else语句,因此在您提交表单时不会显示错误。

答案 1 :(得分:0)

从邮件功能中删除at符号,看看你得到了什么错误。 @mail可以防止显示错误。

答案 2 :(得分:0)

注释掉以下行:if(isset($ SESSION [&#39; code &#39;])&amp;&amp; $ SESSION [&#39; code < / em>&#39;] == strtoupper($ str)){} else {$ error [&#39; secu&#39;] = 1;}

您应该能够访问邮件功能。