0

So I have a webpage with a form, and I'm trying to set up a php page where, when the submit button is clicked, it sends all the form information to my email without the user having to use Outlook or any sort of emailing software. I thought I had it right, but instead of running the PHP, it just shows a page of all the php code written out, and nothing arrives at my email. Here's the form:

<form name="infoForm" method="post" action="email.php">
<p class="form">
    Are you a registered voter in the state of Washington?<br>
    <input type="checkbox" id="yesBox"/> Yes<br><br>

    First Name: <input type="text" id="firstName"><br>
    Last Name:<input type="text" id="lastName"><br><br>

    Email: <input type="text" id="email" size="30"><br><br>
    Street Address: <input type="text" id="street1" size="30"><br>
                    <input type="text" id="street2" size="30"><br>
    City: <input type="text" id="city" size="30"><br>
    State: <input type="text" id="state" size="30"><br>
    Zip Code: <input type="text" id="zip" size="30"><br><br>
    Initials: <input type="text" id="initials" size="3"><br><br>

    <input type="submit" name="send" class="inputButton" id="send" value="Submit" disabled=true/> <input type="reset" name="resetFields" class="inputButton" value="Reset"/>
</p>
</form>

And my php code:

<?php
    if(isset($_POST['send'])){
        $to_address="tonybenwhite@live.com";
        $subject="WSA Day of Action Entry";
        $firstname=$_POST['firstname'];
        $lastname=$_POST['lastname'];
        $email=$_POST['email'];
        $street1=$_POST['street1'];
        $street2=$_POST['street2'];
        $city=$_POST['city'];
        $state=$_POST['state'];
        $zip=$_POST['zip'];
        $initials=$_POST['initials'];
        $message="Name: " .$firstname." ".$lastname."\n";
        $message .="Email: " .$email."\n";
        $message .="Street: " .$street1."\n";
        $message .="Street: " .$street2."\n";
        $message .="City: " .$city."\n";
        $message .="State: " .$state."\n";
        $message .="Zip Code: " .$zip."\n";
        $message .="Initials: " .$initials."\n";
        $headers = 'From: '.$email."\r\n".
        'Replay-To: '.$email."\r\n".
        'X-Mailer: PHP/' .phpversion();
        mail($to_address, $subject, $message, $headers);
        }
?><!DOCTYPE HTML>

<html>
<body>
Thank you!
</body>
</html>
Tony White
  • 197
  • 5
  • 18
  • forever alone :) add PHP parser to your web server. It seems that files are not parsed – Robert Mar 05 '14 at 11:06
  • "just shows a page of all the php code written out" - is php supported by your host!? You should never see php code, if your file is named `*.php` and the server is correctly configured – Philipp Mar 05 '14 at 11:06
  • you are running this script on your server or localhost (on windows or linux)? – Mudaser Ali Mar 05 '14 at 11:06
  • @MudaserAli So far it's running on my localhost on windows. Will it only work if it's on an internet server? I'm sorry for the lack of knowledge, I'm very new to web development! – Tony White Mar 05 '14 at 11:10
  • Local server? Just open the file with your browser or using a webserver like apache? You have to use an webserver which handle php code to test your local code. If you don't have one, you could try xampp – Philipp Mar 05 '14 at 11:11
  • Thats the issue please check it on server; or if you want to send email using localhost please use SMTP : http://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer – Mudaser Ali Mar 05 '14 at 11:13
  • @Philipp yap, just dragging the .html file into my browser so I can edit it and reload for faster testing. If I have to put it on a web server in order to make the PHP work, that's fine, I'll go do that and see how it goes. With the $headers solution that everyone is offering, it SEEMS to work right, but nothing is being emailed. – Tony White Mar 05 '14 at 11:14

3 Answers3

3

You must use this content type as text/html

if(isset($_POST['send'])){
        $to_address="tonybenwhite@live.com";
        $subject="WSA Day of Action Entry";
        $firstname=$_POST['firstname'];
        $lastname=$_POST['lastname'];
        $email=$_POST['email'];
        $street1=$_POST['street1'];
        $street2=$_POST['street2'];
        $city=$_POST['city'];
        $state=$_POST['state'];
        $zip=$_POST['zip'];
        $initials=$_POST['initials'];
        $message="Name: " .$firstname." ".$lastname."\n";
        $message .="Email: " .$email."\n";
        $message .="Street: " .$street1."\n";
        $message .="Street: " .$street2."\n";
        $message .="City: " .$city."\n";
        $message .="State: " .$state."\n";
        $message .="Zip Code: " .$zip."\n";
        $message .="Initials: " .$initials."\n";


$headers=" From : ". $email."\r\n"; 
$headers.= 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


        mail($to_address, $subject, $message, $headers); 
}
Ananth
  • 1,520
  • 3
  • 15
  • 28
  • Oho, that solved the problem where the php code was simply displaying, rather than running, but now I have a new problem: nothing is arriving at my email when I submit the form... – Tony White Mar 05 '14 at 11:07
0
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";   
$to_address1="Your mail id";
mail($to_address1, $subject, $message, $headers);  

Add this

user3004356
  • 870
  • 4
  • 16
  • 49
0

Sending mail is a bit tricky with PHP. I had a lot of problems until I switched to using this plugin: http://phpmailer.worxware.com/

If you can't make it work you should definitely give it a try.

Severin
  • 8,508
  • 14
  • 68
  • 117