Code won't work?

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Fri Apr 28, 2006 3:26 pm    Post subject: Code won't work? Reply with quote

Can anyone see why this very simple bit of code doesn't work?
Code:
<?
$code=$_SERVER["REDIRECT_STATUS_CODE"]; //Get's HTML error code

mail("freddo_frog@westnet.com.au", "Error $code From Kev's Place", "User encountered a $code error", "From: System Generated\n"); // Send mail?
?>

I have gone over it a dozen times and it just will not work! :( I have an almost identical piece of code in another script and it works perfectly - I'm sure it's very simple but I'm just not seeing it.
_________________
Cheers.... Kev

Kev's Place - http://www.kevsplace.com

Powered by Abyss X1.
Back to top View user's profile Send private message Send e-mail Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Fri Apr 28, 2006 3:34 pm    Post subject: Reply with quote

Set error reporting to all, and see what it returns.
Code:
<?
error_reporting(E_ALL);
var_dump(mail("freddo_frog@westnet.com.au", "Error ".$_SERVER['REDIRECT_STATUS_CODE']." From Kev's Place", "User encountered a ".$_SERVER['REDIRECT_STATUS_CODE']." error", "From: System Generated\n"));
?>


I assume it's your mail server not liking it because of the from header.
Needs to be in the form of:
Code:
From: blah@internets
or
From: Dave Jones <blah@internets>

As far as I know.
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Fri Apr 28, 2006 4:32 pm    Post subject: Reply with quote

Thanks, MN - tried that and it returns "bool(false)". I have three other places on the site that I have email sent to me from and they all work as they should. The only difference with this one is that it is not being called by a HTML form (action="blah.php") and so is not dealing with POST data.

I tried what you suggested about the "From:" header and altered it to convential "blah@blah" format - still didn't work. The altered code now reads:
Code:
<?
$code=$_SERVER["REDIRECT_STATUS_CODE"];
$to = "freddo_frog@westnet.com.au";
$body= "User encountered a ".$code." error";
$email= "user@kevsplace.com.au";

mail($to, "Error Message", $body, "From: $email\n");
?>

A bit of a long winded way to do it but I tried ruling out any other errors. Still doesn't work! :(
_________________
Cheers.... Kev

Kev's Place - http://www.kevsplace.com

Powered by Abyss X1.
Back to top View user's profile Send private message Send e-mail Visit poster's website
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Sat Apr 29, 2006 6:41 am    Post subject: Reply with quote

Bump. Anyone?
_________________
Cheers.... Kev

Kev's Place - http://www.kevsplace.com

Powered by Abyss X1.
Back to top View user's profile Send private message Send e-mail Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sat Apr 29, 2006 11:37 am    Post subject: Reply with quote

You could check your SMTP server's log files to see why it wouldn't send.
I'll hazard a guess that it's your header paramater messing it up.
You only need to use a newline if you add more than one header. And when you do, it must be preceded by a carrage return.
Code:
<?
$code=$_SERVER["REDIRECT_STATUS_CODE"];
$to = "freddo_frog@westnet.com.au";
$body= "User encountered a ".$code." error";
$email= "user@kevsplace.com.au";

mail($to, "Error Message", $body, "From: $email\r\nCc: dave@internets\r\n");
?>

_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Sat Apr 29, 2006 1:58 pm    Post subject: Reply with quote

kev1952 wrote:
Bump. Anyone?

mail() returned FALSE because it was unable to run correctly. This could be because was unable to connect to the SMTP server or because the SMTP server denied receiving the email (this could occur if the From address is not validated or not recognized as an address from which the SMTP could accept emails).



Try sending a simple email with a PHP script:

Code:
<?php
var_dump(mail("freddo_frog@westnet.com.au", "Test", "This is a test",
    "From: user@kevsplace.com.au"));
?>


What is the result?

By the way, in MonkeyNation's code, there is an extra \r\n at the end of the last paramater that could cause problems (it should be removed).
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sat Apr 29, 2006 2:08 pm    Post subject: Reply with quote

aprelium wrote:
By the way, in MonkeyNation's code, there is an extra \r\n at the end of the last paramater that could cause problems (it should be removed).


Yeah, I php.net'ed that, seems to be valid, although I'm not too sure.
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Sat Apr 29, 2006 3:47 pm    Post subject: Reply with quote

aprelium wrote:
Try sending a simple email with a PHP script:

Code:
<?php
var_dump(mail("freddo_frog@westnet.com.au", "Test", "This is a test",
    "From: user@kevsplace.com.au"));
?>


What is the result?

Still came back false. I don't think it's a case of the SMTP server rejecting it as the three other scripts I have work just fine. As I stated earlier, the only difference between this script and the others is that the others are called via the FORM tag and get POST data. Perhaps you can spot the problem if I supply the context it's being used in:


Code:
<body>
<?
include "title.php";
?>
<hr>
<p class="p1"><font class="sh1">Oops!</font></p>
<p class="p1"><img src="images/confused.gif" border="2"></p>
<p class="p1">It would seem the page you requested<br>is not currently available (or you are not authorised to view it).<br>If the problem persists and the URL is correct<br>please contact me about it.</p>

<?
$code=$_SERVER["REDIRECT_STATUS_CODE"];
$to = "freddo_frog@westnet.com.au";
$body= "User encountered a ".$code." error";
$email= "user@kevsplace.com.au";
mail($to, "Error Message", $body, "From: $email\n");

echo '<p class="p1"><a href="contact.php?code=';
echo $code;
echo '">Contact Kev</a></p>';
?>
<hr>
<?
include "footer.php";
?>

</body>

This is the code that executes (as you probably realise it is the default error page). What I want is an email notification of the error occurring.
_________________
Cheers.... Kev

Kev's Place - http://www.kevsplace.com

Powered by Abyss X1.
Back to top View user's profile Send private message Send e-mail Visit poster's website
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Sat Apr 29, 2006 6:59 pm    Post subject: Reply with quote

as soon as i read the code i knew what was wrong...

the mail() function is similar to the header() function, in that it should be placed at the top of the script before anything is outputted to the browser.

This means that the code would be as follows:
Code:
<?php
$code = $_SERVER["REDIRECT_STATUS_CODE"];
$to = "freddo_frog@westnet.com.au";
$body = "User encountered a ".$code." error";
$email = "user@kevsplace.com.au";
mail($to, "Error Message", $body, "From: $email\n");
?>
<html>
<head>
[the rest of the page]

_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sat Apr 29, 2006 10:19 pm    Post subject: Reply with quote

Whoever told you that - an asshole.
Header() needs to be sent before output due to the way HTTP works, mail() is not related whatsoever, and has no requirment in terms of order.

Your best bet would be to check the mail server's logs. PHP throws a warning when it can't contact the mail server, so I assume the logfiles would have something to say.
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
kev1952
-


Joined: 08 Sep 2005
Posts: 105
Location: Townsville Australia

PostPosted: Sat Apr 29, 2006 10:58 pm    Post subject: Reply with quote

Thanks, Roganty - unfortunately that didn't solve it either. :(

MN - there may be something in the SMTP logs but where would I find such a beast?
_________________
Cheers.... Kev

Kev's Place - http://www.kevsplace.com

Powered by Abyss X1.
Back to top View user's profile Send private message Send e-mail Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Sun Apr 30, 2006 1:46 am    Post subject: Reply with quote

kev1952 wrote:
Thanks, Roganty - unfortunately that didn't solve it either. :(

MN - there may be something in the SMTP logs but where would I find such a beast?


Tell your server to log as much as it can firstly.
Then look for any SMTP conversations where the server refuses to send the email

Assuming your logfile is suitably small, you may email it to me or something and I'll check it out.
_________________
Back to top View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group