...

View Full Version : email form not sending - no errors



jarv
10-18-2012, 08:47 PM
<?php
if(isset($_POST['query'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail@address.com";
$email_subject = "Query Form from me";


function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['Link']) ||
!isset($_POST['FirstName']) ||
!isset($_POST['MyEmail']) ||
!isset($_POST['MyQuery'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$Link = $_POST['Link'];
$FirstMame = $_POST['FirstName']; // required
$MyEmail = $_POST['MyEmail']; // required
$MyQuery = $_POST['MyQuery']; // required

echo $Link.'<br />';
echo $FirstMame.'<br />';
echo $MyEmail.'<br />';
echo $MyQuery.'<br />';


$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "Link: ".clean_string($Link)."\n";
$email_message .= "First Name: ".clean_string($FirstName)."\n";
$email_message .= "Email: ".clean_string($MyEmail)."\n";
$email_message .= "Query: ".clean_string($MyQuery)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<? }?>

my form submits to this page but it's blank?!

please help

Fou-Lu
10-18-2012, 10:56 PM
That would be a good thing. This script generates output only on failure, so no output means either everything was successful, or you have a parse error.

Make sure you check the results of a mail call. If it checks out successful, PHP did its job; the problem will now be the sendmail on the host machine. Make sure you take off the @ as well, or to a trace back of the error message from error_get_last().

jarv
10-18-2012, 11:35 PM
hmm, its not showing errors?!

tangoforce
10-18-2012, 11:45 PM
mail() won't show any errors because you've supressed them using the @ in front.

If your page is showing up as blank then it may well be a parser error - check your error logs.

As usual jarv, my previous advice still stands. You need to seriously improve your debugging skills especially if you intend to take on paid projects.

Your call to mail() will return a boolean result (thats true or false). You can easily get it by doing this:

$Result = mail($email_to, $email_subject, $email_message, $headers);

var_dump($Result);

Thats called debugging jarv. You print things out to the screen and see what they contain. In this case, you're looking for the var_dump() to report a boolean with a true or 1 contained in it.

Try that, if it gives you a false / 0 result then mail() is failing for some reason.

Fou-Lu
10-19-2012, 12:04 AM
yep, in which case you can use the error_get_last() function or the $php_errormsg to retrieve the last error triggered. If there is none, and the mail pulls up as successful, the issue is beyond PHP's level and you'll need to look into the sendmail logs.

tangoforce
10-19-2012, 12:50 AM
you can use the error_get_last() function

I've just learned something new today :thumbsup: :)

Fou-Lu
10-19-2012, 01:16 AM
ikr! I just learned the other day that SQL actually has a standard that says you're supposed to use ' and not " in queries O.o
Never knew that before!

jarv
10-24-2012, 09:52 PM
still stuck on this, can't get it to display any errors?!

Fou-Lu
10-24-2012, 10:02 PM
still stuck on this, can't get it to display any errors?!

Which sounds to me that you have none.
You can even put a simple echo 'done.'; at the bottom of the script. If that shows, and assuming you've followed the advice here by checking the results of the mail() call check out as successful, you're diving into the sendmail logs to find out why your OS didn't send the mail.

jarv
11-06-2012, 12:56 AM
my emailer still doesn't work?!



<?php
error_reporting(E_ALL);
ini_set( 'display_errors','1');

echo 'hello';
if(isset($_POST['query'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = 'me@gmail.com';
$email_subject = 'Query Form ';

function died($error) {
// your error code can go here
echo 'We are very sorry, but there were error(s) found with the form you submitted.';
echo 'These errors appear below.<br /><br />';
echo $error.'<br /><br />';
echo 'Please go back and fix these errors.<br /><br />';
die();
}

if(!isset($_POST['Link']) ||
!isset($_POST['FirstName']) ||
!isset($_POST['MyEmail']) ||
!isset($_POST['MyQuery'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$Link = $_POST['Link'];
$FirstName = $_POST['FirstName']; // required
$MyEmail = $_POST['MyEmail']; // required
$MyQuery = $_POST['MyQuery']; // required

echo $Link;
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$MyEmail)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$FirstName)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($MyQuery) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= 'Link: '.clean_string($Link).'\n';
$email_message .= 'First Name: '.clean_string($FirstName).'\n';
$email_message .= 'Email: '.clean_string($MyEmail).'\n';
$email_message .= 'Query: '.clean_string($MyQuery).'\n';
echo $email_message;
end;

// create email headers
$headers = 'From: '.$MyEmail.'\r\n'.
'Reply-To: '.$MyEmail.'\r\n' .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
echo 'done.';
?>

tangoforce
11-06-2012, 01:34 AM
jarv did you read my previous advice?



Your call to mail() will return a boolean result (thats true or false). You can easily get it by doing this:

$Result = mail($email_to, $email_subject, $email_message, $headers);

var_dump($Result);


Well? Did you try it?

Here is another way:


if (mail($email_to, $email_subject, $email_message, $headers))
{
echo 'done.';
}
else
{
echo 'error';
}


If it now prints 'done' and you still do not get the email then it is an smtp / sendmail issue or a spam filter somewhere.

Please jarv, read and try things that people say to you.

jarv
11-06-2012, 10:21 AM
Notice: Undefined variable: email_to in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

Notice: Undefined variable: email_subject in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

Notice: Undefined variable: email_message in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

Notice: Undefined variable: headers in D:\aandheurope.com\wwwroot\send_form_email.php on line 556

Warning: mail() [function.mail.php]: SMTP server response: 503 5.5.2 Need Rcpt command. in D:\aandheurope.com\wwwroot\send_form_email.php on line 556
error

tangoforce
11-06-2012, 01:49 PM
Jarv,

Those messages say it all!

You're using variables called:
$email_to, $email_subject, $email_message and they're not containing any values. Your call to mail()then fails and has been outputting error messages that you were hiding using @ symbol. That is why you should not use @ - it hides problems!

This line of code:


if(isset($_POST['query'])) {


Does not contain your call to mail() yet this is where you are setting those variables - inside this block of code. Your call to mail() is outside it at the end so even if your variables are not set, you are still trying to send an email.

Therefore, move your call to mail() inside the { braces } at the end for your if(isset($_POST['query'])) { line of code.

jarv
11-06-2012, 09:49 PM
Thanks!!!! it works... next problem, just not getting the email?! oh well

Fou-Lu
11-06-2012, 10:35 PM
Thanks!!!! it works... next problem, just not getting the email?! oh well

Like I said, if the result of the mail() is successful, PHP has done its job. You'll be hitting the sendmail logs to find out if it sent them or not.
If its not successful, then use the error_get_last() to determine if its a variable issue, or if it has any more information to share. If the variables all check out, then you need to verify sendmail is available, or that smtp is configured if its a windows machine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum