Solved

php form to email problems

Posted on 2007-03-30
39
314 Views
Last Modified: 2013-12-20
Could someone who knows php tell me why my form isn't sending any mail?

thanks
Iso
0
Comment
Question by:isotherm
  • 20
  • 19
39 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 18825672
I am happy to help with this, but I would need to see the form and PHP script to give any specifics.
0
 

Author Comment

by:isotherm
ID: 18825713
Sorry! I meant to post this - this is the script:

       <?php
@extract($_POST);
$name = stripslashes($name);
$company_name = stripslashes($company_name);
$address = stripslashes($address);
$email = stripslashes($email);
$telephone = stripslashes($telephone);
$enquiry = stripslashes($enquiry);

$smtp_server = "mail.holmes.7host.com";
$port = 25;
$mydomain = "holmes.7host.com";
$username = "holmes@holmes.7host.com";
$password = "isotherm";
$sender = "holmes@holmes.7host.com";
$recipient = "holmes@holmes.7host.com";
$subject = "test";
$content = "test";

// Initiate connection with the SMTP server
$handle = fsockopen($smtp_server,$port);
fputs($handle, "EHLO $mydomain\r\n");

// SMTP authorization
fputs($handle, "AUTH LOGIN\r\n");
fputs($handle, base64_encode($username)."\r\n");
fputs($handle, base64_encode($password)."\r\n");

// Send out the e-mail
fputs($handle, "MAIL FROM:<$sender>\r\n");
fputs($handle, "RCPT TO:<$recipient>\r\n");
fputs($handle, "DATA\r\n");
fputs($handle, "To: $recipient\n");
fputs($handle, "Subject: $subject\n\n");
fputs($handle, "$content\r\n");
fputs($handle, ".\r\n");

// Close connection to SMTP server
fputs($handle, "QUIT\r\n");

header("location:thanks.php");
?>

This is the form:

<div id="Layerform">
  <form name="Generalenquiry" method="post" action="http://www.holmes.7host.com/sendenquirymail.php">
  <div align="left" class="hills">
    <p class=MsoNormal> <span class="bold style11"><a name="top"></a></span><span class="page_heading">General enquiry form  </span></p>
             <input name="type" type="hidden" value="Business">
    <table cellspacing="0" cellpadding="0">
      <tr>
        <td width="113" valign="top"><p class="bodytext">Name</p></td>
        <td width="313" valign="top" class="bodytext">
          <input name="name" type="text" size="50">        </td>
      </tr>
    </table>
    <table cellspacing="0" cellpadding="0">
      <tr>
        <td width="113" valign="top"><p class="bodytext">Company name<br />
          (if applicable) <br />
          </p>
          </td>
        <td width="313" valign="top" class="bodytext">
          <input name="company_name" type="text" size="50">        </td>
      </tr>
    </table>
    <table cellspacing="0" cellpadding="0">
      <tr>
        <td width="113" valign="top"><p class="bodytext">Address</p></td>
        <td width="313" valign="top" class="bodytext">
          <textarea name="address" cols="40" class="text" id="address"></textarea>        </td>
      </tr>
    </table>
    <table cellspacing="0" cellpadding="0">

      <tr>

        <td width="113" valign="top"><p class="bodytext">Telephone
          </p>          </td>

        <td width="313" valign="top" class="bodytext">
          <input name="telephone" type="text" id="telephone" size="50">        </td>
      </tr>
      <tr>
        <td colspan="2" valign="top"><p class="maintext">&nbsp;</p></td>
        </tr>
    </table>

    <table cellspacing="0" cellpadding="0">

      <tr>

        <td width="113" valign="top"><p class="bodytext">Email</p></td>

        <td width="313" valign="top"><p>

          <span class="bodytext">
          <input name="email_address" type="text" id="email_address" size="50">
          </span></p></td>
      </tr>
      <tr>
        <td colspan="2" valign="top"><p class="maintext">&nbsp;</p></td>
        </tr>
      <tr>
        <td valign="top" class="maintext">Your enquiry </td>
        <td valign="top"><span class="bodytext">
          <textarea name="enquiry" cols="40" class="text" id="enquiry"></textarea>
        </span></td>
      </tr>
    </table>

    <p class="bodytext">&nbsp;</p>

    <p>

      <input name="Submit" type="submit" class="bold" value="Submit">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

      <input name="Reset" type="reset" class="bold" value="Reset">
</p>

    <p class=bold>  </div>
</form>
    </div>
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18825798
Three notes:

1. This is outside the scope of your question, but should be addressed anyway: Never use extract()!! Your script is assuming that input will always come from the form you created. However, that may or may not be the case. Anyone can craft a POST request and send it directly to your script. The POST request may contain any number of fields, with potentially disastrous results. Always create your scripts in such as way that you verify the data provided is the data you expect.

2. I attempted to connect to port 25 on 'homes.7host.com', and received a connection timeout. Are you sure that your script can access that server?

3. Is there a reason that you must use this complicated handshake process rather than just using the PHP built-in mail functions?
0
 

Author Comment

by:isotherm
ID: 18825836
Hi there - ok - extract will be out; I didn't realise it was so open to abuse - it should be 'holmes.7host.com' not 'homes.7host.com' ...the script seems to run and fires up the 'thanks' page, but nothing gets sent.
I was originally using just the simple 'mail' function, but my ISP told me that due to spamming I would have to use this nasty 'handshake' script :-(

Thanks for your help....

Iso
0
 

Author Comment

by:isotherm
ID: 18825842
Incidentally the page is at:

http://www.holmes.7host.com/contact_form.php

I know the gfx aren't working - I'm just trying to get the functionality there :-)

Iso
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18825871
Sorry, I mistyped when I listed the server name here, but I am unable to connect to 'holmes.7host.com' as well.

When I tested your form, I received the following warnings:

Warning: main(nav_build2_test.php): failed to open stream: No such file or directory in D:\business\holmes.7host.com\html\thanks.php on line 382

Warning: main(): Failed opening 'nav_build2_test.php' for inclusion (include_path='.;c:\php4\pear') in D:\business\holmes.7host.com\html\thanks.php on line 382

Is there anything in the missing 'nav_build2.test.php script that may be related?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18825880
I notice that you are not testing for a valid connection after using fsockopen(). Can you add the following:

$handle = fsockopen($smtp_server,$port);
if (!$handle)
{
   die("Cannot connect to the SMTP server.");
}
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18825888
Or better yet:

$handle = fsockopen($smtp_server, $port, $error_number, $error_string);
if (!$handle)
{
   die("Cannot connect to the SMTP server. $error_number: $error_string");
}
0
 

Author Comment

by:isotherm
ID: 18825891
Hi there - that's odd that you couldn't connect - I don't understand that if the page is being served?

The warnings relate to a ssi page which contains the navigation - so shouldn't have any effect on the script.
0
 

Author Comment

by:isotherm
ID: 18825916
like this?

       <?php

$name = stripslashes($name);
$company_name = stripslashes($company_name);
$address = stripslashes($address);
$email = stripslashes($email);
$telephone = stripslashes($telephone);
$enquiry = stripslashes($enquiry);


$smtp_server = "mail.holmes.7host.com";
$port = 25;
$mydomain = "holmes.7host.com";
$username = "holmes@holmes.7host.com";
$password = "isotherm";
$sender = "holmes@holmes.7host.com";
$recipient = "holmes@holmes.7host.com";
$subject = "test";
$content = "test";

// Initiate connection with the SMTP server
$handle = fsockopen($smtp_server,$port);
fputs($handle, "EHLO $mydomain\r\n");

// SMTP authorization
fputs($handle, "AUTH LOGIN\r\n");
fputs($handle, base64_encode($username)."\r\n");
fputs($handle, base64_encode($password)."\r\n");

// Send out the e-mail
fputs($handle, "MAIL FROM:<$sender>\r\n");
fputs($handle, "RCPT TO:<$recipient>\r\n");
fputs($handle, "DATA\r\n");
fputs($handle, "To: $recipient\n");
fputs($handle, "Subject: $subject\n\n");
fputs($handle, "$content\r\n");
fputs($handle, ".\r\n");

// Close connection to SMTP server
fputs($handle, "QUIT\r\n");

$handle = fsockopen($smtp_server, $port, $error_number, $error_string);
if (!$handle)
{
   die("Cannot connect to the SMTP server. $error_number: $error_string");
}

header("location:thanks.php");
?>
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18825932
It is possible that 'holmes.7host.com' is configured to allow connections only from 'www.holmes.7host.com' (where your script resides), so that may be the answer to that portion. However, you may want to verify that, just to make sure that this is truly a PHP problem, rather than a server problem.

Making the alterations I suggested above may help to identify any connection problem, if it exists.

Additionally, if you are able, check the SMTP logs on the SMTP server. See if perhaps the email *is* being sent, but is being filtered or dropped in some way.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18825946
Sorry, I was still typing when you posted, so I missed the last one. You want to replace your current fsockopen line:

// Initiate connection with the SMTP server
$handle = fsockopen($smtp_server,$port);

with this:

// Initiate connection with the SMTP server
$handle = fsockopen($smtp_server, $port, $error_number, $error_string);
if (!$handle)
{
   die("Cannot connect to the SMTP server. $error_number: $error_string");
}

That way, we are checking the connection before trying to do any of the puts() statements.
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18825955
'fputs()', rather than 'puts()' sorry.
0
 

Author Comment

by:isotherm
ID: 18826026
I'e done that and re-tried with no  luck - although bizarrely I commented out the 'header' bit at the bottom to stop it leavibng the script page, and i got two emails through with just the title 'test'...??
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18826062
Ah, that may tell us something. Perhaps the email on the server is not send until the connection is explicitly closed. The webserver will do this automatically when the script ends (which doesn't really happen if you are redirecting using header()). Try adding an explicit close after 'fputs($handle, "QUIT\r\n");':


fputs($handle, "QUIT\r\n");
fclose($handle);
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18826066
'is not sent', rather. This must be a day for typos.
0
 

Author Comment

by:isotherm
ID: 18826105
..good thinking - but not working. I tried commenting out the header again but it doesn't seem to have the magic effect this time  :-(
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18826123
What else was changed last time at the same time that you commented out the header() line?

Additionally, have you attempted to open up a direct telnet connection to the SMTP server, just to see if you can do it?
0
 

Author Comment

by:isotherm
ID: 18826145
The only other thing I changed was the bit of code I'd put at the bottom, rather than in the 'Initiate Connection' part. I tried doing that again ( getting desperate now :-) but no go....

Re: connecting by Telnet - I'm not sure what telnet is to be honest.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:isotherm
ID: 18826182
Incidentally, this is what came through on the responses:

test
X-UIDL: 475177612
X-IMail-ThreadID: 65f403920000bb82
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18826187
Okay, no problem. If you are using a Windows computer, click the Start menu and click "Run". In the Run box, type 'cmd' to open a command window.

In the command window, type:

telnet holmes.7host.com 25

This tells your computer to open a connect to the server 'holmes.7host.com' using port 25. Telnet is just a program that initates the connect. In fact, your web browser is performing a very similar action when it connects to a webserver to download a web page. The only major difference is that the browser connects to port 80, and then uses a different set of command to request a page.

If you connect to 'holmes.7host.com', try running each of the commands listed in the fputs() statements above (First 'EHLO', followed by the Enter key, then the next command, and so forth).

If this completed successfully, you will know that the connection to the mail server is not at fault. If, however, you are unable to connect to the server, I would assume that the error lies there.
0
 

Author Comment

by:isotherm
ID: 18826231
hmmm...well I did that and it said 'connecting..' then the command window just closed. Tired it several times with the same result. Doesn't look hopeful ...... but then why did it send those two reponses? I just don't get it...

0
 
LVL 24

Expert Comment

by:glcummins
ID: 18826250
Perhaps an intermittent connect issue on the server: sometimes it is available, and the emails go through, and other times it is not.

I would again suggest that you gain access to the SMTP logs on the server, to try to deduce why it is not accepting connections.
0
 

Author Comment

by:isotherm
ID: 18826557
Hi - I have emailed the hosting co to see if they can let me have access to the log. Thanks for all your help - this is so frustrating!
Will post the outcome of any logs that are forthcoming :-)

iso
0
 

Author Comment

by:isotherm
ID: 18826633
I am also taking out space on a different server , so will see if that helps!

best,
iso
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18826646
Sounds good! I hope it works out well for you.
0
 

Author Comment

by:isotherm
ID: 18831743
Hi there,

well - it WAS the mail server! Grrr! I have signed up with another host that has no smtp issues!

This is the code now:

<?php

$name = $_POST['name'];
$company_name = $_POST['company_name'];
$address = $_POST['address'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];

$formcontent="VISITOR NAME: $name\n\nCOMPANY NAME: $company_name\n\nENQUIRY: $enquiry\n\nADDRESS: $address\n\nEMAIL: $email";
$recipient = "holmes@dircon.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");

//header("location:thanks.php");
?>

I had to comment out the last 'header' bit as it caused an error, saying

Warning: Cannot modify header information - headers already sent by (output started at /home/madhouse/public_html/sendenquirymail.php:10) in /home/madhouse/public_html/sendenquirymail.php on line 37

is there a way to fix this? Also do I have to list each part of the form in the 'mail' section - or is there a way to send all the fields in one go?

thanks
Iso

0
 

Author Comment

by:isotherm
ID: 18831748
Sorry - I meant in the 'formcontent' section...
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18832827
Web servers handle data in two parts. First is the header, which includes things like information about the type of content that will be sent, and how to send it. The second part is the data itself. As a rule, all header information must be sent first, and then the data.

However, when writing a PHP script it can be difficult to tell where one part ends and the other part begins. For example, try to run the following script:

<?php
print "Hello, world.";

header("Location: index.php");
?>

You will receive the same error: "Cannot modify header information." The reason for this is that you sent data ("Hello, world"), and then tried to send header information. Web browsers will balk at this, so PHP produces a warning.

Now in your script, there are no obvious 'print' statements, or anything that would be sending data before the header() line. So, we need to find out what is sending the data. For now, comment out the header line and run the script again. See what output is displayed on the screen. We will need to fix the part of the script that is sending output to the screen before we can use the header() line.
0
 

Author Comment

by:isotherm
ID: 18833137
Hi - ok I have uncommented the header again;

Warning: Cannot modify header information - headers already sent by (output started at /home/madhouse/public_html/sendenquirymail.php:10) in /home/madhouse/public_html/sendenquirymail.php on line 36

..That's what I get.  If I comment it out, I get no error message.....
0
 

Author Comment

by:isotherm
ID: 18833148
But surely the script has to send the mail before proceeding to the 'thanks' page?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18835950
Hm... No output if the 'header' line is commented out, but the warning is still received if you use the 'header' line.

Try this: check the top of your script, before the opening '<?php'. Make sure there are no blank lines or spaces, so that the '<' is the very first character in the script. Does that make a difference?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18835955
>> Also do I have to list each part of the form in the 'mail' section - or is there a way to send all the fields in one go?

I'm not sure what you mean here. Can you provide more details?
0
 

Author Comment

by:isotherm
ID: 18836490
Hi - well - I've taken out any spaces in the script -

<html>

<head>

<title>Submitting form...</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="maintext.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$name = $_POST['name'];
$company_name = $_POST['company_name'];
$address = $_POST['address'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$formcontent="VISITOR NAME: $name\n\nCOMPANY NAME: $company_name\n\nENQUIRY: $enquiry\n\nADDRESS: $address\n\nEMAIL: $email";
$recipient = "holmes@dircon.co.uk";
$subject = "Gneral Enquiry Contact Form";
$mailheader = "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Failure!");
header("location:thanks.php");
?>

   
</body>

</html>

..but gives this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/madhouse/public_html/sendenquirymail.php:12) in /home/madhouse/public_html/sendenquirymail.php on line 26

..line 26 is the "header("location:thanks.php"); "line.

By sending all the fields in one go - I mean can I assign all $_post fields to a variable, rather than having to name each one - like:
$formcontent="VISITOR NAME: $name\n\nCOMPANY NAME: $company_name\n\nENQUIRY: $enquiry\n\nADDRESS: $address\n\nEMAIL: $email";

..does that make sense?
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18836580
Okay, I didn't know that you were adding all of the HTML stuff at the top. That is causing the error. This page is not supposed to display any output, so remove all of the HTML. Everything before the '<?php' should go.

There are a couple of different ways to use the values in $_POST, but you are probably using the best one. The other options will not label the data correctly, or be formatted as well.

Instead of:

$name = $_POST['name'];
$company_name = $_POST['company_name'];
$address = $_POST['address'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$formcontent="VISITOR NAME: $name\n\nCOMPANY NAME: $company_name\n\nENQUIRY: $enquiry\n\nADDRESS:
You can do something like this, however:

foreach ($_POST as $field=>$value)
{
   $formcontent .= "$field: $value<br />";
}

Then use the $formcontent variable in your mail() line, and the data will be sent.
0
 

Author Comment

by:isotherm
ID: 18836893
Fantastic - only slight bug is that it comes through as:

type: Business<br />name: dfgdfg<br />company_name: dfgdfg<br />address: dfgdfgdfg<br />telephone: dgdfgdf<br />email: fgdgdfgdf<br />enquiry: gdfgdfgdfg<br />Submit: Submit<br />

Presumably this need to be sent as html email to get the formatting and breaks - is that possible?

thanks for all your help,

Iso
0
 
LVL 24

Accepted Solution

by:
glcummins earned 250 total points
ID: 18836931
Yes, you could sent the email as HTML, or modify the loop slightly:

foreach ($_POST as $field=>$value)
{
   $formcontent .= "$field: $value\n";
}

The '\n' will cause line breaks in plaintext email. Sorry I missed that before.
0
 

Author Comment

by:isotherm
ID: 18836988
Brilliant - thanks so much for all your help - I am getting better honestly!

All the best,
Iso
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18837038
Yep, you'll get there. Just remember, www.php.net is your friend! :) All the best.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Read about why website design really matters in today's demanding market.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now