# FormToEmail script doesn't send emails



## mjones1 (May 25, 2008)

We recently put together a simple website, and we created a simple web form. We are trying to get this FormToEmail.php script to work, but it doesn't seem to want to actually SEND the email when we go through the process. It gives us the "thank you for submission" screen with no errors.

We have PHP5 configured, as well as fastCGI and perl (why not, lol). We have set handler mappings for PHP and PHP via fastCGI, and they are enabled. We have out HTML form as follows:


> <form name="form1" method="post" action="FormToEmail.php">
> <p>First Name<br>
> <input type="text" name="name" id="name">
> <br>
> ...


And our FormToEmail.PHP script as as follows:


> <?php
> 
> $my_email = "[email protected]";
> 
> ...


When all is said and done, we get no email sent to us, but no error messages/pages/codes from the server. Are we completely missing something here?

Any direction is much appreciated.


----------



## Laxer (Dec 28, 2010)

change this line:


```
mail($my_email,$subject,$message,$headers);
```
to this ->

```
if(mail($my_email,$subject,$message,$headers)) echo "<h1>mail sent</h1>";
```
If that returns "mail sent"

try replacing PHP_EOL with "\n" or "\r\n"

If still nothing i will take a deeper look into it.


----------



## mjones1 (May 25, 2008)

I changed that line. It didn't give me any "mail sent" message, but it did what it normally does, which is:


> Thank you sdf
> Your message has been sent
> 
> Click here to continue
> ...


Then it returns me to the webpage.


----------



## DDAoN (Mar 19, 2011)

The statement he gave you to put it should've returned "mail sent" had the server done what it's supposed to have done. One thing I notice is that your script is set-up for a local e-mail server pre-configured via the server control panel (my PHP is a bit rusty, so correct me if I'm mistaken, Laxer), but are using a gMail account. Give this a try:

```
<?php
 $continue = '/';
 $from = '[email protected]';
 $to = '[email protected]';
 $subject = 'FormToEmail Comments';
 $message = stripslashes(build_message($_REQUEST) . PHP_EOL.PHP_EOL."-- ".PHP_EOL."");
 $host = 'ssl://smtp.gmail.com';
 $port = '465';
 $username = '[email protected]';
 $password = 'password';
 $headers = array ('From' => 'From: ' . $_REQUEST['email']);
 $smtp = Mail::factory('smtp',
 array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
 $mail = $smtp->send($to, $subject, $message, $headers);
 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>Dreamweaver Tutorial - Contact Form</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 </head>

 <body bgcolor="#ffffff" text="#000000">
  <div>
   <center>
    ';
 if (PEAR::isError($mail))
 {
  echo '<p>' . $mail->getMessage() . '</p>');
 }
 else
 {
  echo '<b>Thank you ' . stripslashes($_REQUEST['name']) . '</b>
    <br>Your message has been sent
    <p><a href="' . $continue . '">Click here to continue</a></p>
    <p><b>Dreamweaver Spot</b> by <a href="http://dreamweaverspot.com">dreamweaverspot.com</a></p>';
 }
 echo '
   </center>
  </div>
 </body>
</html>';
?>
```
I already configured it for you particular set-up, assuming you can sending it from yourself to yourself. ^^' All you should have to do is put in the password for your account.

Should it worked right, you should give the message that it was sent, as you had written it, and your gMail should send itself the e-mail. In the event it doesn't work, it should instead display an error message providing a little more information to go off of. Mind you, the error message would be in relation to the code I'm providing, not to your previous set-up.

Also, this is meant to replace your FormToEmail.php file, not correct it. Please remember to back-up your previous work before applying this. There is no garuntee it will work, but I sure hope it does. I want to help you. ^^


----------



## mjones1 (May 25, 2008)

That makes a little more sense, your explanation of the smtp server info. I tried your code into a new FormToEmail.php file, replaced the password variable line with that particular gmail account's password, but no luck. It displayed a new page, blank white, with nothing on it, and on the address bar on top, had www.mysite.com/FormToEmail.php left in the URL with no redirect.

Any more ideas?  I really do appreciate all of the awesome help...


----------



## DDAoN (Mar 19, 2011)

I'm not sure what exactly what I might've did wrong, maybe its the version of PHP, I'm using 4. 

Ummm, I tweaked the code to include the redirect, if the submission is successful. In addition, I did a few little tweaks to the connection lines, hopefully it'll work for you this time. It tested fine on my private server with my gMail account. I do need to ask, have you enabled POP/IMAP for your account? Initially my account didn't work, but when I enabled the settings, as if using a third-party e-mail software, it worked. Here's the code:

```
<?php
 $continue = "/";
 $from = "[email protected]";
 $to = "[email protected]";
 $subject = "FormToEmail Comments";
 $message = stripslashes(build_message($_REQUEST) . PHP_EOL.PHP_EOL."-- ".PHP_EOL."");
 $host = "smtp.gmail.com";
 $port = "465";
 $username = "[email protected]";
 $password = "password";
 $headers = array ('From' => "From: " . $_REQUEST['email']);
 $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
 $mail = $smtp->send($to, $subject, $message, $headers);
 if (PEAR::isError($mail))
 {
  $redirect = echo '';
  $page = echo '<p>' . $mail->getMessage() . '</p>');
 }
 else
 {
  $redirect = echo '
  <meta http-equiv="Refresh" content="15;URL=' . $continue . '">';
  $page = echo '<b>Thank you ' . stripslashes($_REQUEST['name']) . '</b>
    <br>Your message has been sent
    <p><a href="' . $continue . '">Click here to continue (the page will automatically redirect in 15 seconds)</a></p>
    <p><b>Dreamweaver Spot</b> by <a href="http://dreamweaverspot.com">dreamweaverspot.com</a></p>';
 }
 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>Dreamweaver Tutorial - Contact Form</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' . $redirect . '
 </head>

 <body bgcolor="#ffffff" text="#000000">
  <div>
   <center>
    ' . $page . '
   </center>
  </div>
 </body>
</html>';
?>
```
If this new code doesn't work, I'm sorry, but I don't know what would be wrong.


----------



## mjones1 (May 25, 2008)

Yes, I have pop/imap support enabled...
" $password = "password";" Is the ONLY line that needs to be changed, correct?

Gave it another whirl, seems to still be a no go. Are there any server side configs I may be missing (windows server 2008 R2)?

It did the same thing as last time: went to a blank page, titled http://mysite.com/FormToEmail.php and sat there and a blank page. no confirmation, no error, no redirect. Does that sound like it isn't parsing PHP properly server-side? I am also testing this from a separate machine other than the server, which leads me to think my hypothesis is true, but I have no idea what I might have done wrong when installing PHP, setting handler mappings, etc... or how to undo it all to start it freshly...

/blarg


----------



## Laxer (Dec 28, 2010)

Please check your "junk" mail on gmail.

Unless you are using a secure mail server most messages will get dumped into trash =/ (even with the correct headers)


----------



## DDAoN (Mar 19, 2011)

It is strange. :S

I doubt the messages are being sent, meaning they wouldn't even be in your junk mail, but there's no reported errors. If the server found an error it should call out the line in the code of the error, and it's not processing the echo commands, let alone submitting the mail function. 

I'm so sorry, mjones1, but I don't know what I might've screwed up. Your form with my script runs just fine on my private server, but I also use a *nix server. ^^'

I'll break open one of my SMTP ebooks and see if I can come up with something else. Laxer, if you wouldn't mind, could you review the problem and the provided codes?


----------



## Laxer (Dec 28, 2010)

> I changed that line. It didn't give me any "mail sent" message, but it did what it normally does, which is:


Sorry I thought you got a good return.

I will adjust the script for you and post it up in ~10mins


----------



## Laxer (Dec 28, 2010)

try creating a page with just this


```
<? if(mail("[email protected]","test","test")) echo "sent"; ?>
```
Let me know if you get any errors.

If not check your junk mail at gmail, i have had form2email spit it out there before.

IF still nothing create a blank page with:


```
<? phpinfo(); ?>
```
then provide me a link.

*Last thought, who are you hosted through?*


----------



## mjones1 (May 25, 2008)

I thought I had mentioned this before, I host it myself. I have a static IP routing as a DMZ host to a server machine, running Windows Server 2008 R2. I have installed PHP5, Perl, IIS7, and tons of other junk that I can't currently remember. We are hosting our first few sites with no problem, we are just having a heck of a time getting this PHP script to work with our server, and the more we try, the more I think this is a server-side issue. I _think_ I've set the handlers properly, I can provide an s/s if necessary for that of their configs.

Ok, so I have created three files in a default website directory for testing purposes under IIS7.
First, I made the aforementioned echo test:

```
<? if(mail("[email protected]","test","test")) echo "sent"; ?>
```
http://98.190.158.244/test.PHP
Next, I made the standard phpinfo.PHP file (which I tested locally, and it worked fine LOCALLY, server-side executed, but does not work now)
http://98.190.158.244/phpinfo.php
And finally, to make sure I wasn't going crazy and screwing up in IIS7, I made a regular old text file with "test". This worked fine, leading me to believe I screwed PHP parsing up somewhere on the server:
http://98.190.158.244/text.txt

So, I'm kinda' realizing where the problem is, but still pretty lost on how to fix it. Does that make sense?

Again, thanks for all the great support.


----------



## Laxer (Dec 28, 2010)

The php script itself isn't even running..... (you can view it in the source)

I didn't know that you owned the server....

I would go back and look through some of the information to make sure you installed it correctly.

I haven't installed php from scratch in 3 years so i am not familiar with the process.

PHP: Installation on Windows systems - Manual


----------



## mjones1 (May 25, 2008)

I went through and installed it twice, to make sure I was doing it correctly.

Here, when I try to access the file locally on the server (phpinfo()), which consists of "<?php phpinfo(); ?>", I get the following:
[/quote]No input file specified.[/quote]

Does that give any direction? If not, I can perhaps take this issue over to the Server 2008 section... blarg, this is driving me nuts.


----------



## Laxer (Dec 28, 2010)

A few things can cause that error....

make sure your path to the file is correct. (if it is not it spits out that error)

If you are positive it is correct then your .htaccess file is to blame.

Go to the root of your site and open .htaccess in a text editor.

Add these lines onto the bottom of it:


```
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.+\.php$ /bogusfile
```
If still nothing create a normal file (html/txt) in that folder and try to access it... do you get a 404?


----------



## mjones1 (May 25, 2008)

I don't seem to have an .htaccess file anywhere... 

It should be under inetpub/wwwroot/mysite/, right?

EDIT: I can definitely _access_ files in that directory... and entire site is being hosted in it (minus this stupid php script...)

Second EDIT: I'm doing a little reading on .htaccess files (as to not be that douchebag that comes and asks for help without putting in any effort).

I'm just a little confused on how to create one with the settings you specified, and is it simple called ".htaccess"? Because I can't make a file with that extension and no name (or do I call it "whateverIwant.htaccess" or "config.htaccess"?)


----------



## mjones1 (May 25, 2008)

I made my own config.htaccess at the root of the site, and added the aforementioned code. Same message on a new page: 


> No input file specified.


----------



## Laxer (Dec 28, 2010)

Please check serverside.

The file should have been created when the server was created.

All this should do is 404 php files.

The true problem probably lies in the php.ini file....


----------



## mjones1 (May 25, 2008)

The default location should be... inetpub/wwwroot/defaultsite ? SHould it create a new one every time we make a new site?

EDIT:

Also, I get "You must type a file name" when I try to create ".htaccess" as a file.


----------



## Laxer (Dec 28, 2010)

default location should be: inetpub/wwwroot/

I would suggest creating subfolders for sites like what you did.

.htaccess should already exist and not have to be created. (in my experiences)

It appears it sometimes does not get created... this may be your case....

If so please follow this -> How To Create & Edit The .htaccess File For Your Site


----------



## mjones1 (May 25, 2008)

Yeah, I had checked that site out and already generated code for mine, but when I try to create the file, like I specified above, I get a message telling me I cannot create a file with that name.


----------



## Laxer (Dec 28, 2010)

You truly have me baffled.

Perhaps you have shorttags disabled?

try


```
<?php phpinfo(); ?>
```
If its not that it has to be something related to php(5).ini or .htaccess....

the question is what


----------



## mjones1 (May 25, 2008)

Don't give up! I have new info. 

I believe I started to uninstall PHP, and remove the fastCGI service. I uninstalled PHP, then I removed the fastcgi service, and restarted the server.

Then, I had a recommendation to use the Microsoft Web Platform Installer. I loaded it, and it said I already had PHP installed on the server... funny, I had removed it and rebooted.... So, for one last stupid thoughtless action, I copied the .htaccess file (that I finally got configured, but didn't do anything in the mysite.com root dir) and placed it into the wwwroot directory... and now, localhost/info.php gives me:


> HTTP Error 500.21 - Internal Server Error
> Handler "PHP Extension" has a bad module "FastCgiModule" in its module list


So, that sounds like CGI is simply uninstalled (which it should be). So, I re-ran the CGI role service installer, and now, accessing localhost/info.php gives me:


> HTTP Error 500.0 - Internal Server Error
> <handler> scriptProcessor could not be found in <fastCGI> application configuration


So, it sounds like my handlers just aren't set up now?

I'm dying from reading for tonight. I desperately require sleep. I'm going to try to tackle this in the AM.

/hugs for the halp


----------



## Laxer (Dec 28, 2010)

At least you got a break through.

CGI errors are a little easier to work with then just a random non parsing error.

Try this if you have a chance: How to install Apache 2 with PHP 5 as a CGI


----------



## mjones1 (May 25, 2008)

You lost me at install Apache... I'm using IIS7... Why would I need Apache?


----------



## mjones1 (May 25, 2008)

Ok, so I've cleared the entirety of PHP and CGI, to start from scratch. Tell me if this sounds about right:

I stopped and uninstalled the CGI role service from IIS, removed all of the *.php mapping handlers/modules (including the fastCGI extension), then I deleted the whole damn PHP directory.

I went to PHP For Windows: Binaries and sources Releases and selected the non-thread safe package (.zip distributable), extracted it all to C:\PHP (newly created, after deleting).

I then went into the PHP directory, renamed php.ini-production (which I believe I should be using, since they removed the php.ini-recommended file from this version) to php.ini.

I edited php.ini in the following respects:


> Open the php.ini file. Uncomment and modify the settings as follows:
> 
> Set fastcgi.impersonate = 1. FastCGI under IIS supports the ability to impersonate security tokens of the calling client. This allows IIS to define the security context that the request runs under.
> Set cgi.fix_pathinfo=1. cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. Previously, PHP behavior was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not define PATH_INFO. For more information about PATH_INFO, see the cgi specifications. Setting this value to 1 will cause PHP CGI to fix its paths to conform to the specifications.
> ...


And now, this is where I get stuck: I should be able to go into an elevated cmd prompt and do:

```
C:\PHP>php –info
```
But alas, I can't. It tells me "Access is denied". Hm. There's nothing limiting my PHP directory, hell, I added "Everyone" as an object parameter with full privileges!

This might be where I was coughing before, but I've followed many a guides to get to this point, so... what the heck?

For reference, I was using this guide: Using FastCGI to Host PHP Applications on IIS 7 : Host Applications on the Microsoft Web Platform : Running PHP Applications on IIS : The Official Microsoft IIS Site

I'm so close to advertising LMI/TV credentials and a reward for someone to hop in and configure this crap, I'm so close to losing it!


----------

