alfaro
asked on
Emails Not Arriving From Contact Form
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..
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 ;
?>
ASKER
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
What should I try now?
dc
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"); ?>
Don't forget <?php mail("me@mydomain.com", "test message", "Hello world"); ?>
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: https://www.experts-exchange.com/questions/24305209/Sending-an-Email-using-PHP.html#24096153
As well as: https://www.experts-exchange.com/questions/24351404/Website-PHP-mailer-form-not-working-Linux-server.html#24233879
Please see this: https://www.experts-exchange.com/questions/24305209/Sending-an-Email-using-PHP.html#24096153
As well as: https://www.experts-exchange.com/questions/24351404/Website-PHP-mailer-form-not-working-Linux-server.html#24233879
ASKER
Using the test.php file I get taken to my thankyou.html page but no email arrives...
ASKER
I'm checking with the website hosting co to see if the mail() function is enabled..
ASKER
I'm checking with the website hosting co to see if the mail() function is enabled..
ASKER
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...
ASKER
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?
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?
ASKER
And should I remove any of the existing code from the feedback.php file??
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.
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.
ASKER
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...
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.
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.
ASKER
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
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
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-e n.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.
--------------------
---------
Copy these files class.phpmailer.php to your web server:
class.phpmailer.php
languages/phpmailer.lang-e
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.
--------------------
ASKER
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
dc
ok, let's se if mail is returning an error
change test.php to this
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.";
?>
ASKER
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\te st.php on line 2
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\
Well that explains why mail() doesn't work. Looks like you're going to have to use the smtp option
ASKER
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
dc
Seems like on that server they've disabled email relays
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I've tried that and it still fails to send the email...
dc
dc
ASKER
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
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
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.
ASKER
I'll take a look and see what's what...
dc
dc
ASKER
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...
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.