This question already has an answer here:
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail@gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
Have a shot at this.
I changed you're
$header
variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.I also made it so that you pass the mail function through the parameters of the
if
statement, rather than setting a new variable.Really hope this helps! :)
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
server. For example :
$headers = 'From: emailacc@yourserver.com';
From
inheader
to the email address that you've just created.From
field in the$headers
is not theFrom
as you think.<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"];
// you put the email address from the input form here$headers = 'From: emailacc@yourserver.com';
// here is the email address specified from which u want to send the email.(i.e. your server email address)mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
Hope this helps