简体   繁体   中英

PHP contact form submitting but not receiving email

I realise this question has been asked numerous times before but everyone's code is obviously different and I am quite new to php so just looking to see if someone can give me some help.

I have created a basic contact form for a site but for some reason the information is not being sent to my email address although I believe that the form is submitted?

my PHP code is:

<?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";
}

}

?>

my html form looks like this:

<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 } ?>        

Am i missing something obvious here?? Any help will really be appreciated thanks!

You have used sessions which is not required here, you can also use flag variable instead of arrays in this simple form, use this updated code.

<?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';
}
}
?> 

You have also missed out the else statement for your form validation test so no errors getting displayed when you submit form.

Remove the at sign from mail function and see what errors your get. @mail suppresses errors from being displayed.

Comment out the following line: if ( isset($ SESSION['code ']) && $ SESSION['code '] == strtoupper($str)){} else {$error['secu'] = 1;}

You should be able to reach the mail function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM