Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

html - 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 = "[email protected],[email protected],[email protected]";
$to_mail = "[email protected]";
//$cc="[email protected]";
$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" <[email protected]>'."
";
    //$headers .= 'CC: "'.$cc.'" <'.$cc.'>'."
";
    $headers .= 'MIME-Version: 1.0' . "
";
    $headers .= 'Content-type: text/html; charset=utf-8' . "";



    $subject = "New contact message";

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

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

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

    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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 = "[email protected],[email protected],[email protected]";
$to_mail = "[email protected]";
//$cc="[email protected]";
$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" <[email protected]>'."
";
    //$headers .= 'CC: "'.$cc.'" <'.$cc.'>'."
";
    $headers .= 'MIME-Version: 1.0' . "
";
    $headers .= 'Content-type: text/html; charset=utf-8' . "";

    $subject = "New contact message";

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

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

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

    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...