Solved

Emails Not Arriving From Contact Form

Posted on 2009-05-09
27
411 Views
Last Modified: 2012-05-06
I have a simple contact form that works fine on one website but when I use the same form and .php file on a seperate site the email seems to have been sent but it never arrives. I assume everything has gone ok because the "thankyou.html page comes up after send but whether the email has been sent I can't tell..

 I've checked the email and it is working normally outside of the contact form. PHP is also enabled on the domain with the hosting company.

The code is shown below..


The Contact Form. "contactus.html"
 

<form action="feedback.php" method="post">

<table border="0" cellpadding="8" cellspacing="8" summary="feedback form">

<tr><td><label for="tswname">Name</label>

:</td><td><input type="text" maxlength="40" name="name" id="tswname" size="25" /></td></tr>

<tr><td><label for="tswemail">Email address</label>:</td><td><input type="text" maxlength="50" id="tswemail" name="email" size="25" /></td></tr>

  <td valign="top">How you found us:</td>

  

 

 

  <td valign="top"><select name="findus" id="findus">

  

  <option name="Google Ads" >select</option>

  <option name="Google Ads" >Google Ads</option>

  <option name="Google - Local Business" >Google - Local Business Section</option>

  <option name="Google - Normal Listings" >Google - Normal Listings</option>

  <option name="Returning Customer" >Returning Customer</option>

  <option name="My Abbots" >My Abbots</option>

  <option name="My Watford" >My Watford</option>

  <option name="My Bushey" >My Bushey</option>

  <option name="Nascot Wood Newsletter" >Nascot Wood Newsletter</option>

  <option name="Yahoo.co.uk" >Yahoo.co.uk</option>

  <option name="Recommnendation" >Recommendation</option>

  <option name="Other" >Other</option>
 

    </select></td>

</tr>
 

<tr>

<td colspan="2">

<label for="tswcomments">Comments</label> 

(and phone number if you want us to call you)

<br />

<textarea rows="15" cols="45" name="comments" id="tswcomments" ></textarea></td>

</tr>

<tr>

<td align="center" colspan="2">

<input type="submit" value="Send Feedback" /><br />
 

</td>

</tr>

</table>

</form>
 

The feedback.php file...
 

<?php
 

// ------------- CONFIGURABLE SECTION ------------------------
 

// $mailto - set to the email address you want the form

// sent to, eg

//$mailto		= "youremailaddress@example.com" ;
 

$mailto = 'sales@MYDOMAIN.co.uk' ;
 

// $subject - set to the Subject line of the email, eg

//$subject	= "Feedback Form" ;
 

$subject = "Message From Website" ;
 

// the pages to be displayed, eg

//$formurl		= "http://www.example.com/feedback.html" ;

//$errorurl		= "http://www.example.com/error.html" ;

//$thankyouurl	= "http://www.example.com/thankyou.html" ;
 

$formurl = "http://www.MYDOMAIN.co.uk/contactus.html" ;

$errorurl = "http://www.MYDOMAIN.co.uk/error.html" ;

$thankyouurl = "http://www.MYDOMAIN.co.uk/thankyou.html" ;
 

$email_is_required = 1;

$name_is_required = 1;

$findus_is_required = 0;

$uself = 0;

$use_envsender = 0;

$use_webmaster_email_for_from = 0;

$use_utf8 = 1;
 

// -------------------- END OF CONFIGURABLE SECTION ---------------
 

$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;

$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;

if (!isset( $use_envsender )) { $use_envsender = 0 ; }

$envsender = "-f$mailto" ;

$name = $_POST['name'] ;

$email = $_POST['email'] ;

$findus = $_POST['findus'] ;

$comments = $_POST['comments'] ;

$http_referrer = getenv( "HTTP_REFERER" );
 

if (!isset($_POST['email'])) {

	header( "Location: $formurl" );

	exit ;

}

if (($email_is_required && (empty($email) || !ereg("@", $email))) || ($name_is_required && empty($name))) {

	header( "Location: $errorurl" );

	exit ;

}

if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) || ereg( "[\r\n]", $findus ) ) {

	header( "Location: $errorurl" );

	exit ;

}

if (empty($email)) {

	$email = $mailto ;
 
 
 

}

$fromemail = (!isset( $use_webmaster_email_for_from ) || ($use_webmaster_email_for_from == 0)) ? $email : $mailto ;
 

if (get_magic_quotes_gpc()) {

	$comments = stripslashes( $comments );

}
 

$messageproper =

	"This message was sent from:\n" .

	"$http_referrer\n" .

	"------------------------------------------------------------\n" .

	"Name of sender: $name\n" .

	"Email of sender: $email\n" .

	"------------------------- How you found us -------------------------\n\n" .

	"I found you via: $findus\n" .

	"------------------------- COMMENTS -------------------------\n\n" .

	$comments .

	"\n\n------------------------------------------------------------\n" ;
 

$headers =

	"From: \"$name\" <$fromemail>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.13.0" .

	$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;
 

if ($use_envsender) {

	mail($mailto, $subject, $messageproper, $headers, $envsender );

}

else {

	mail($mailto, $subject, $messageproper, $headers );

}

header( "Location: $thankyouurl" );

exit ;
 

?>

Open in new window

0
Comment
Question by:alfaro
27 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24344536
first test to see if mail is working on this server with a test.php and

mail("me@mydomain.com", "test message", "Hello world");

this will tell us if the issue is with mail on that server or to look into your code.
0
 

Author Comment

by:alfaro
ID: 24344772
I'm not sure if I've done this right but creating a test.php file with your code and changed the contactus.html accordingly gives me the code displayed as shown above..

What should I try now?

dc
0
 
LVL 3

Expert Comment

by:webvogel
ID: 24345954
You have to check, if you have email, after you changed me@mydomain.com to your mail address and put the url to the test.php in your browser.
Don't forget <?php mail("me@mydomain.com", "test message", "Hello world"); ?>
0
 
LVL 18

Expert Comment

by:Hube02
ID: 24347994
The PHP function mail() may actually be disabled, some hosts do this. However, the tests given above may actually still fail depending on some server settings. You can do some testing to find out if this is the case.

Please see this: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24305209.html#24096153

As well as: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24351404.html#24233879
0
 

Author Comment

by:alfaro
ID: 24352696
Using the test.php file I get taken to my thankyou.html page but no email arrives...

0
 

Author Comment

by:alfaro
ID: 24352742
I'm checking with the website hosting co to see if the mail() function is enabled..
0
 

Author Comment

by:alfaro
ID: 24352751
I'm checking with the website hosting co to see if the mail() function is enabled..
0
 

Author Comment

by:alfaro
ID: 24352848
Regarding the test.php form I meant to say that I get taken to the test.php file after submitting the form but no email arrives...
0
 

Author Comment

by:alfaro
ID: 24354263
My hosting company have sent me the following...


we would highly recommend not using PHPMail function as they is no method for us to track what the problem is, I would suggest using a SMTP method including SMTP authentication, this way we can almost gaurentee it will always work, as your connection is authenticated.

Try using something similar to below in your PHP Script,

$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.your-domain"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = you@your-domain; // SMTP username
$mail->Password = "POP3-Password"; // SMTP password
$mail->Port = 25;

Should I add this code to my existing feedback.php file and if so, where?
0
 

Author Comment

by:alfaro
ID: 24354275
And should I remove any of the existing code from the feedback.php file??
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24354513
Here's the site for the documentation and an example of the code. Replace teir example variables with your host, username and password.

http://phpmailer.codeworxtech.com/index.php?pg=examplebsmtp

Becauwse your message is text only you could put it in the AltBody and leave MsgHTML undefined.
0
 

Author Comment

by:alfaro
ID: 24354774
I'm afraid that script is way beyond me. I downloaded it and it includes a bout a dozen files and folders. It seems a lot of effort for a simple contact form...
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24354822
Since your hosting company suggested it I'll assume it's already installed. So all you should have to do is include the class file.

Who's your hosting company? Do the have an example on their site?

At first it may look likes there's a lot to it, but when you look line by line it's pretty easy. and if the script does all the error checking and recovery it's well worth it.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:alfaro
ID: 24355020
It might be a bit more complicated than that. The file I downloaded has about 6 folders and probably about 100 individual files...

The hosting company have told me "We are not PHP Developers so its not really something we can help with,"

Which kind of brings things to a bit of a dead end with them..

dc
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24355643
actually you only need these three files, most of the download is for docs and examples. I downloaded PHP4 version, not sure if you need PHP 4 or 5

---------
Copy these files class.phpmailer.php to your web server:

class.phpmailer.php
languages/phpmailer.lang-en.php

If you are using the SMTP mailer then copy class.smtp.php as well.

If you do not have control of the include directory and you are running PHP 4.0 or higher you can use this function to set it to the path of your files:

ini_set("include_path", ".:/path/to/phpmailer/dir");

Or this:

$mail->PluginDir = "/path/to/phpmailer/dir";


The total size of the libraries and language files is approximately 100 Kb.
--------------------
0
 

Author Comment

by:alfaro
ID: 24355735
I can't see how connect any of this with my existing contactus.html or what to change in the php files. I think this sort of stuff is way beyond me..

dc
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24355802
ok, let's se if mail is returning an error

change test.php to this

<?php 

$ok=mail("me@mydomain.com", "test message", "Hello world"); 

if ($ok==true)

  echo "Email was sent.";

else

  echo "There was an error sending the email.";

?>

Open in new window

0
 

Author Comment

by:alfaro
ID: 24355878
This is what it gives me (I've changed the email address back here although I used the real on in the test..

There was an error sending the email.PHP Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable to relay for sales@MYDOMAIN.co.uk in C:\domains\MYDOMAIN.co.uk\wwwroot\test.php on line 2

0
 
LVL 19

Expert Comment

by:Michael701
ID: 24356229
Well that explains why mail() doesn't work. Looks like you're going to have to use the smtp option
0
 

Author Comment

by:alfaro
ID: 24356264
It's very odd though. On another of our domains with the same hosting company and using exactly the same contactus.html and feedback.php form but with the email and domain name changed the emails come through with no trouble...

dc
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24356447
Seems like on that server they've disabled email relays
0
 
LVL 19

Accepted Solution

by:
Michael701 earned 500 total points
ID: 24356468
Try using one of you're other domain names and see if the same error occurs. It could be that the dns email isn't setup the same and that the host server doesn't know that it's a valid domain to relay messages
0
 

Author Comment

by:alfaro
ID: 24357770
I've tried that and it still fails to send the email...

dc
0
 

Author Comment

by:alfaro
ID: 24362768
My hosting company say that either the email doesn't exist (which it does as do all the other ones I've tried..) or the smtp authentication username or password is wrong. I've checked the username and changed the password to no effect. I don't think they really know very much...

I've found a workaround which involves redireting the contact page to a copy of the page on our other domain which then uses a seperate feedback.php form on that domain and then returns the user to thankyou page on the first domain. Longwinded but in practice it all works smoothly and will probably have to do for now..

dc
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24364918
Well it's definitely something with the was the machine is configured. Or it could be something with the DNS MX setup. Some mail gets rejected without proper reverse DNS entries. Maybe compare the two domains.
0
 

Author Comment

by:alfaro
ID: 24364935
I'll take a look and see what's what...

dc
0
 

Author Closing Comment

by:alfaro
ID: 31579770
Although I fixed the problem by redirecting the user of the contact form to another domain I feel that Michael701 enabled me to determine that the problem resided with the hosting company's server for this particular domain...
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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

18 Experts available now in Live!

Get 1:1 Help Now