$HTTP_USER_AGENT issue with mail() function

Hi guys,

I’m working on a web form which generates email to client. I’ve used this script a dozen times before on a dozen different websites, but on this one, it’s just not working, and I’m tearing out my hair trying to figure out what’s gone wrong.

The form is a pretty basic web form:

<div id="form">
<form action="../includes/post/concertbookingform.php" method="post" id="enquiry">
<input type="hidden" name="Booking_Form" value="Concert 2012" />

<fieldset>
<legend>Contact Details</legend>
<label for="School">Name of School:</label>
	<input type="text" name="School" id="School" size="40" placeholder="Please enter the school's name here."/><br>

<label for="Contact">Contact Name:</label>
	<input type="text" name="Contact" id="Contact" size="40" placeholder="Please enter the contact name here."/><br>

<label for="Address" class="clear">Address:</label>
	<textarea name="Address" id="Address" rows="5" cols="40" placeholder="Please enter the school's address here."></textarea><br>

<label for="Postcode">Postcode:</label>
	<input type="text" name="Postcode" id="Postcode" size="40" placeholder="Please enter the school's postcode here."/><br>

<label for="Phone">Telephone:</label>
	<input type="tel" name="Phone" id="Phone" size="40" placeholder="Please enter the school's telephone number here."/><br>

<label for="Email1">Email Address:</label>
	<input type="email" name="Email1" id="Email1" size="40" placeholder="Please enter your email address here."/><br>

<input class="check" name="TsandCs" type="checkbox"><p class="radio">I have read and understood the terms and conditions.</p>

</fieldset>

<fieldset>
<legend>Concerts</legend>
<h1>Concert 1</h1>
<h2>31st April 2012 - 10pm</h2>
        <input type="hidden" name="concert1" value="31st April 2012 - 10pm" />
<label class="child" for="child_tickets_a">Child Tickets:</label>
	<input class="child" type="text" name="child_tickets_a" id="child_tickets_a" size="40" placeholder="Child tickets."/>

<label class="adult" for="adult_tickets_a">Adult Tickets:</label>
	<input class="adult" type="text" name="adult_tickets_a" id="adult_tickets_a" size="40" placeholder="Adult tickets."/>
</fieldset>

<fieldset>
<legend>Prove You're Human!</legend>
<p>To make sure you're not a computer programme filling out this form, please answer the following question:<br>What colours are the lights on standard UK traffic lights?</p>
<input class="radio" type="radio" name="human" value="non" /><p class="radio">Blue, White, Purple</p>
<input class="radio" type="radio" name="human" value="oui" /><p class="radio">Red, Amber, Green</p>
<input class="radio" type="radio" name="human" value="nein" /><p class="radio">Turquoise, Magenta, Beige</p>
<p class="warning">You MUST answer the question above correctly, otherwise your booking form will not be submitted.</p>
</fieldset>

<input class="go" type="submit" name="Submit" value="Send Enquiry" />
<input class="go" type="reset" name="Submit2" value="Reset Form" />
</p>

</form>

This is the concertbookingform.php it calls to generate the email:


<?php

ini_set('display_errors', true);
error_reporting(-1);

// Configuration Settings
$SendFrom = "Booking Form <booking@clientswebsite.org.uk>";
$SendTo = "boxoffice@clientswebsite.org.uk";
$SubjectLine = "Booking Form ";
$ThanksURL = "http://www.clientswebsite.org.uk/received.php";  //confirmation page
$ErrorURL = "http://www.clientswebsite.org.uk/notreceived.php";  //confirmation page
$Divider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($REMOTE_ADDR) . "\
$Divider\
";
foreach ($_POST as $Field=>$Value)
   $MsgBody .= "$Field: $Value\
";
$MsgBody .= $Divider . "\
" . $HTTP_USER_AGENT . "\
";
$MsgBody = htmlspecialchars($MsgBody);  //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

if ($_POST['human'] == 'oui' && $_POST['TsandCs'] == 'on')
{
	if (mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom)){
		header("Location: $ThanksURL");
	}

	else { echo 'something went wrong';
	};
}

else
{
	header("Location: $ErrorURL");
};

?>

This code works perfectly on one server, but not on the other. I thought, initially, it was something to do with spam filters, but the client’s tech team assured me nothing was even attempting to get through.

So. One of my colleagues suggested using the optional additional parameters of the mail() function so that

mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom, $addParam);

where $addParam = “-f booking@clientswebsite.org.uk”;

That made no difference.

I forced the displaying of errors and I got:

Notice: Undefined variable: HTTP_USER_AGENT in /home1/client/public_html/includes/post/concertbookingform.php on line 18
something went wrong

I looked up the PHP manual, and it was suggesting $_SERVER[‘HTTP_USER_AGENT’], so I tried that. Still didn’t work.

Tried toggling register_globals - no luck.

Tried simply removing the $HTTP_USER_AGENT . "
" from the code altogether. Still didn’t work.

Tried re-installing CGI-BIN Email stuff. Still didn’t work.

I’ve even tried bringing across the files from the sites I know for a fact it does work on. And it doesn’t work.

The frustrating thing is, if I copy the form, and the php into one of my other sites, it works perfectly, but I honestly just don’t know what to try next.

Has anyone else ever come across this? Any idea what might be wrong/missing on my server?

Thanks,

PenguinsRock

Some random server information below in case that helps…

cPanel Version 11.30.5 (build 6)
Apache version 2.2.22
PHP version 5.2.17
Operating system linux
Path to sendmail /usr/sbin/sendmail

The frustrating thing is, if I copy the form, and the php into one of my other sites, it works perfectly, but I honestly just don’t know what to try next.

Could you able to compare the code of that with the one that did not work? Or you could just post the code that did not work here for us to see.

Hi Tom8,

Thanks for your reply. The code posted above is the code that doesn’t work.

The code is practically identical to the code that does work on all my other sites - indeed this code DOES work when I copy it over to another site; the only variations between the various versions of this code between all my sites are the email addresses from/to, the URL redirects for successful/unsuccessful and the fields being passed. So the problem really isn’t the code itself… Or at least, that’s what I suspect.

I suspect that not only because it’s adapted from my other sites where it works perfectly, but even on this server that I’m having the issues with, when I change the email addresses in the variables $SendFrom and $SendTo, to email which are not related to the site (i.e. myemail@me.com as opposed to whatever@clientswebsite.org.uk), the code works just fine.

So I think it’s something to do with the way the php is interacting with the server/email processing, but I can’t work out what that’s got to do with the $HTTP_USER_AGENT error I’ve been getting… or, more importantly, how to go about fixing it.

Sorry, I misinterpreted your post. Even though I made the text bigger somehow I read that as copy the form, and the php FROM one of my other sites. Thanks for clearing it up. I tested the code and it works fine on my server too.

BTW, the variable for HTTP_USER_AGENT should be $_SERVER[‘HTTP_USER_AGENT’] as PHP manual noted.

Please give this a try: change LF (
) to CRLF (\r
)
and see if it works.

As Tom8 said, some servers need to have the full \r
on the headers

I also notice that you have no line limit on the message, always worth putting that in your standard library script even if you think you’ll not need it

My go to library mail script is below, always works for me.


   $message = wordwrap($message, 70);
   $sendto  = 'blah' ; // obviously the email
   $subject = 'blah' ; // obviously the subject
   $headers = 'MIME-Version: 1.0' . "\\r\
"; 
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\
"; 
   $headers .= 'From: email@mywebsite.com' . "\\r\
"; 
   $headers .= 'Bcc: ' . $any_extra_bcc_addresses . "\\r\
"; 
   $headers .= 'Reply-To: ' . $the_reply_address . "\\r\
"; 
   $headers .= 'X-Priority: 1' ."\\r\
"; 
   $headers .= 'X-MSMail-Priority: High' . "\\r\
"; 
   $headers .= 'X-Mailer: PHP' ;
   if (mail($sendto, $subject, $message, $headers)){
    echo '<p>Mail Sent.</p>';
    print '<pre>Headers: ' . $headers . '</pre>'; // Nice test to see what headers look like
   }else{
    echo '<p>Problem with Email:</p>';
   } 


Hope that helps

Hi,

I’ve tried everything you have suggested, and the emails still aren’t being generated, but like I said before, I suspect that the actual php code is not the problem here…

I’m now almost convinced it’s something to do with my CGI-BIN configuration, or apache configuration, or php.ini configuration, or some other configuration on the server… but I wouldn’t know where to start with that.

It’s the fact that the code works perfectly if I change the email addresses in the $SendTo, $SendFrom variables to email addresses other than those belonging to the client’s website that’s baffling me the most. Has anyone ever come across that?

Is the site hosted on the same server as their SMTP server is on, if so perhaps theyve an internal routing problem on the mail.

Just a thought

Sorry I missed your comment that if you changed the email address all worked perfectly, thats got to be either an internal outgoing route problem OR a incomming firewall block?

remembered …

I have one site where my client rents the doamin here in france (its a .fr) he has a redirect to a uk server (cause he hosts all his sites there), but the emails are still handled by the french company selling him the domain.

I have the same problem there, if I send a email from a php script to the same domain as the website it never arrives, because the SMTP server believes that the site is local whereas it’s not.

just a thought

It’s fixed! The server’s cPanel had MX Entry set to handle email routing through the Local Mail Exchanger. As our email is hosted on a different server to the website, I change it to handle email routing through the Remote Mail Exchanger, and suddenly it’s all working! As ever, it was one of these situations where it’s the simplest little thing that was staring me in the face which I just couldn’t see!

Though, it’s worth saying that changing $HTTP_USER_AGENT to $_SERVER[‘HTTP_USER_AGENT’] was a necessary change for the code to work once I’d sorted the email routing.

Thanks, as ever, for all your help.

PenguinsRock :slight_smile:

Glad to have helped