0

我意识到这个问题之前已经被问过很多次,但是每个人的代码显然不同,而且我对 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 } ?>        

我在这里遗漏了一些明显的东西吗?任何帮助将不胜感激,谢谢!

4

3 回答 3

1

您已经使用了此处不需要的会话,您也可以使用标志变量而不是这种简单形式的数组,使用此更新的代码。

<?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 语句,因此在提交表单时不会显示错误。

于 2013-08-14T10:09:11.760 回答
0

注释掉以下行: if ( isset($ SESSION['code ']) && $ SESSION['code '] == strtoupper($str)){} else {$error['secu'] = 1;}

您应该可以访问邮件功能。

于 2013-08-14T14:02:32.287 回答
0

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

于 2013-08-14T10:17:47.643 回答