Reputation: 29
Hi I am facing a problem with my contact form I didn't receive any mails and I don't know why ..
Here is my php code is there any thing wrong
<?php
$name=$_POST ['name'];
$mobileno=$_POST ['mobileno'];
$email=$_POST ['email'];
$message=$_POST ['message'];
$to = 'any mail.com';
$subject = ' Message from your Webite';
$msg = " Your name : $name\n";
$msg .= " Your Mobile No. : $mobileno\n";
$msg .= " Your email: $email\n" ;
$msg .= " Your Message: $message";
mail ($to, $subject, $msg, 'From:' . $email);
echo ' Thank You <br/>';
echo ' Your name ' . $name . '<br>';
echo ' Your email ' . $email . '<br>';
echo ' Your Mobile No. ' . $mobileno . '<br>';
echo ' Your Message ' . $message . '<br>';
?>
<br /><br />
Here is the result from $msg: <br /><br />
<?php
echo $msg;
?>
My HTML Code I have edited the code several times but nothing happened
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="form1" method="post" action="justsend.php">
<p>
<label for="mobileno">your mobile</label>
<input type="text" name="mobileno" id="mobileno">
<label for="name"><br>
your name</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="email">your email</label>
<input type="text" name="email" id="email">
</p>
<p>
<label for="message">your message</label>
<textarea name="message" id="message" cols="45" rows="5"></textarea>
</p>
<p>
<label for="send">send msg</label>
<input type="submit" name="send" id="send" value="Submit">
</p>
</form>
</body>
</html>
Upvotes: 2
Views: 431
Reputation: 1064
Hi I have just modified your code just a little bit..and I have recieved the email..please check with below code..please give your email in the textbox to recieve the email
<?php
if($_POST['email'] != '')
{
$name=$_POST['name'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$message=$_POST['message'];
$to = $email;
$subject = 'Message from your Webite';
$msg = " Your name : $name\n";
$msg .= " Your Mobile No. : $mobileno\n";
$msg .= " Your email: $email\n" ;
$msg .= " Your Message: $message";
mail ($to, $subject, $msg, 'From:'.$email);
echo ' Thank You <br/>';
echo ' Your name ' . $name.'<br>';
echo ' Your email ' . $email .'<br>';
echo ' Your Mobile No. ' . $mobileno . '<br>';
echo ' Your Message ' . $message . '<br>';
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form name="form1" method="post" action="justsend.php">
<p>
<label for="mobileno">your mobile</label>
<input type="text" name="mobileno" id="mobileno">
<label for="name"><br>
your name</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="email">your email</label>
<input type="text" name="email" id="email">
</p>
<p>
<label for="message">your message</label>
<textarea name="message" id="message" cols="45" rows="5"></textarea>
</p>
<p>
<label for="send">send msg</label>
<input type="submit" name="send" id="send" value="Submit">
</p>
</form>
</body>
</html>
Upvotes: 0
Reputation: 5240
First of all, if this is on a localhost, it wont work, cause mailfunction is disabled locally, you have to run it online
Further: I dont see really where you are going wrong, but I took over my own mail function i got on my own website and converted it to yours
if(isset($_POST['send'])){ // checks if the data from the submit button with the name send is here
$name=mysqli_real_escape_string($con,$_POST['name']);
$mobileno=mysqli_real_escape_string($con,$_POST['mobileno']);
$email=mysqli_real_escape_string($con,$_POST['email']);
$message=mysqli_real_escape_string($con,$_POST['message']);
// you had spaces between your post variables which wont work. Also if you use mysqli, its better to play safe and make them safe, in case you do also database work. The variable $con is your connection information.
// echo out if your variables working. For example:
// echo $mobileno;
// this way you know for sure if the items are set.
$message = wordwrap($message, 70); // in case its a long message, this allows 70 characters on one line
$to = 'any mail.com'; // your email
$subject = ' Message from your Webite';
$msg = " Your name : ".$name."\n";
$msg .= " Your Mobile No. : ".$mobileno."\n";
$msg .= " Your email: ".$email."\n" ;
$msg .= " Your Message:".$message ;
$headers = "MIME-Version: 1.0" . "\r\n"; // this has to do with css and html in your msg
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // this has to do with css and html in your msg
$headers .= "From: [email protected]"; // change this to whatever you want it to come from
if (mail ($to, $subject, $msg, $headers)) {
echo ' Thank You <br/>';
echo ' Your name ' . $name . '<br>';
echo ' Your email ' . $email . '<br>';
echo ' Your Mobile No. ' . $mobileno . '<br>';
echo ' Your Message ' . $message . '<br>';
// to make sure if your email is send it will output your message as given above, else an error
}else{
echo ' Something went wrong ... oopsie daisy';
} // closure of the if mail check
} // closure of the complete if(isset) check
dont forget to include your connection info as well for the mysqli function to sanitize your variables in case you re going to add it as well to a database. Also it's just a good practise to do it anyway. I defined $con as the connection info, yours might be different. You have to change it then to your connection variable.
if you use mysql_
functions, use mysql_real_escape_string($_POST['YourPostVariable']);
without the connection info. See here. http://php.net/manual/en/function.mysql-real-escape-string.php
--edit--
updated it, because your message wouldn't output your variables, just the $name instead of the name within the $name variable.
Hope this helps, good luck debugging :)
Upvotes: 0
Reputation: 1094
$_POST ['name'];
should be $_POST['name'];
remove all the spaces in your post values.
so:
$name=$_POST ['name'];
$mobileno=$_POST ['mobileno'];
$email=$_POST ['email'];
$message=$_POST ['message'];
becomes:
$name=$_POST['name'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$message=$_POST['message'];
and like EternalHour said, it might be a good idea to validate if the form is filled in somehow; try this just after you declared $name
and $mobileno
etc;
if(!empty($name) && !empty($mobileno) && !empty($email) && !empty($message)){
// do stuff
}
the reason you would use !empty()
instead of isset()
is because isset would still return true if the form is submitted, since the values are set.. they are just empty.
edit:
further in your HTML change action="get"
to action="post"
in order to get it working.
Upvotes: 1
Reputation: 8621
You need to fix your $_POST
variables, but I would recommend this instead so you will know if your POST succeeded.
<?php
if (isset($_POST)) {
$name=$_POST['name'];
$mobileno=$_POST['mobileno'];
$email=$_POST['email'];
$message=$_POST['message'];
$to = 'any mail.com';
$subject = ' Message from your Webite';
$msg = " Your name : $name\n";
$msg .= " Your Mobile No. : $mobileno\n";
$msg .= " Your email: $email\n" ;
$msg .= " Your Message: $message";
mail ($to, $subject, $msg, 'From:' . $email);
echo ' Thank You <br/>';
echo ' Your name ' . $name . '<br>';
echo ' Your email ' . $email . '<br>';
echo ' Your Mobile No. ' . $mobileno . '<br>';
echo ' Your Message ' . $message . '<br>';
?>
<br /><br />
Here is the result from $msg: <br /><br />
<?php
echo $msg;
} else {
echo "submit failed!";
exit;
}
?>
This way you know if there is no message your form submitted properly.
Upvotes: 0