1

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.

Noob_Programmer
  • 111
  • 5
  • 14

3 Answers3

2

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 :

  • When you pass "from" in headers, php expects an existing email account of your
    server. For example : $headers = 'From: emailacc@yourserver.com';
  • So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
  • The From field in the $headers is not the From 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 :)

The SuperKat
  • 234
  • 2
  • 10
  • Hi! Your explanation makes perfect sense. I copied exactly what you said. Created an email account with my free web hosting provider. I tested out the script but an email still was not sent out. But the thing is that I could send out an email from emailacc@myserver.com to my gmail address but not from my php script. Any ideas? – Noob_Programmer Aug 21 '16 at 10:32
  • Do a little test : firstly before using form values test this (with strings): ` $email="anymail@gmail.com"; $to = "youremail@gmail.com"; $subject = "test subject"; $message="test message"; $headers = 'From: emailacc@yourserver.com'; mail($to, $subject, $message, $headers); ` do not change anything above. just copy and paste and replace only $to variable with your gmail account and **emailacc@yourserver.com**. If that works then the problem is solved in your php script. – The SuperKat Aug 21 '16 at 10:40
  • Ok I just copied and pasted exactly what you said and still no email was sent out. Updated my first post with the PHP script that I used and with the HTML form code. I really cant figure out what is going wrong here. – Noob_Programmer Aug 21 '16 at 10:57
  • OKay See, [here](http://moodbnao.com/Capture.PNG) i have gmail : **pawansharma3303@gmail.com** and server mail address : **info@botsystems.in** this script works – The SuperKat Aug 21 '16 at 11:07
  • I have the exact same thing but its not working for me i dont know why. Im using 000webhost. Could i be a problem on their side? – Noob_Programmer Aug 21 '16 at 11:53
  • 000webhost has quite a good reputation. So,, can you show up exact code that you are using , including the email addresses that you are using?? – The SuperKat Aug 21 '16 at 12:19
  • Wow the problem was actually with 000webhost. I changed hosting provider and it worked immediately! Thanks for all the help :) – Noob_Programmer Aug 21 '16 at 13:01
  • Great!! your welcome – The SuperKat Aug 21 '16 at 13:17
0

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.

$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation

if(mail("myemail@gmail.com", "Contact Form Submission", $message, $headers)){
    // success message
} else {
    // error message
}

Really hope this helps! :)

GROVER.
  • 4,071
  • 2
  • 19
  • 66
0

Try adding spaces after the "=" that might be the problem,

If that doesn't work you could try to use this

 <?php
     $emailvariable = $_POST['InputEmail']


    $to      = 'example@gmail.com';
    $subject = "Form"
    $message = $_POST['InputMessage'];
    $headers = "From: $emailvariable";

    mail($to, $subject, $message, $headers);
    ?> 

Hope this helps

  • i tried using your solution but still no email sent. I really cant seem to figure out the problem. – Noob_Programmer Aug 21 '16 at 09:47
  • Are you hosting your website with a domain etc.. Because if you aren't you are not able to use a mailserver to send mail. So with wamp or mamp you are not able to do this, if you are hosting it on a website try removing the uppercase letters from the $_POST['InputEmail'] –  Aug 21 '16 at 10:06
  • Yup im hosting it on a website with a domain name using a free webhost. Do you mean something like $_post['inputemail']? – Noob_Programmer Aug 21 '16 at 10:30
  • Yes else try to change message to some type of string like "messagegoeshere" and then do the same for $headers = "From: mail@example.com" –  Aug 21 '16 at 10:45