Forum Moderators: coopster

Message Too Old, No Replies

Stuck on php code

get the info, just don't know whats next

         

ajs83

10:59 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



I'm using the mail function and need the code to look up the session id [id field of the users table] of the user, then look for the user's email from the Users table and the email field so that the $email command works.

=============================
$row = mysql_fetch_array(mysql_query("SELECT email FROM users WHERE id=$sid"));
$to = '$email';
$subject = '$subject';
$message = '$message.';
$headers = 'From: blah@domain.com';

mail($to, $subject, $message, $headers);
===============================
This is what i have to open the db and get the info, but I'm having trouble getting the info to work as intended.

I know something has to go after the fetch line and before the mail function, but I am having trouble as all I've tried is not working. Which would be the best way to go?

dmmh

11:05 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



$row = mysql_fetch_array(mysql_query("SELECT email FROM users WHERE id=$sid"));
$to = $row['email'];
$subject = 'some subject';
$message = 'hello this is a test message';
$headers = 'From: blah@domain.com';

mail($to, $subject, $message, $headers);

ajs83

11:50 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Hmm, that didn't work either...

Thanks anyways...

jatar_k

12:00 am on Feb 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what do you end up with in the $to var?
Are you sure the query is working as intended?

also, as far as headers go you should terminate each line with \r\n, so they should be like this

$headers = "From: blah@domain.com\r\n";

you need the double quotes around them so they resolve into carriage return and linefeed instead of being treated as a string.

ajs83

12:16 am on Feb 24, 2005 (gmt 0)

10+ Year Member



I'm not getting anytype of error or notice, it's just not sending any emails. I did test it by defining a specific email in the to spot and it worked fine, but nothing when It's suppose to email a specific user.

Below is the code I am using
========================
$row = mysql_fetch_array(mysql_query("SELECT email FROM users WHERE id=$sid"));
$to = $row['email']\r\n";
$subject = 'Subject\r\n";
$message = 'Message';
$headers = 'From: blah@domain.com';

mail($to, $subject, $message, $headers);

jatar_k

12:27 am on Feb 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well let's try a little debugging then

your code should actually look like this

$row = mysql_fetch_array(mysql_query("SELECT email FROM users WHERE id=$sid"));
$to = $row['email'];
$subject = 'Subject';
$message = 'Message';
$headers = "From: blah@domain.com\r\n";
mail($to, $subject, $message, $headers);

ok, we have a few things to check before we start firing off emails so we will comment that line and add a few echo's to figure out if our vars are being set properly.

$row = mysql_fetch_array(mysql_query("SELECT email FROM users WHERE id=$sid"));

// dump the $row var to the screen to verify row data is retrieved
echo '<p><pre>row: ';
print_r($row);
echo '<pre>';

$to = $row['email'];
$subject = 'My Subject';
$message = 'My Message';
$headers = "From: blah@domain.com\r\n";

// echo all vars to make sure they are set properly
echo '<p>to: ',$to;
echo '<br>subject: ',$subject;
echo '<br>message: ',$message;
echo '<br>headers: ',$headers;

//mail($to, $subject, $message, $headers);

try the above and see if it helps you identify if there are any data problems.

ajs83

2:09 am on Feb 24, 2005 (gmt 0)

10+ Year Member



Well, I feel like an idiot now...

Looks like the email server was on a severe delay and the email just arrived so the code does work.

Thanks dmmh and jatar_k. Your help was greatly appreciated.

jatar_k

4:07 am on Feb 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



np, we all just love email ;)

email is a terrible thing to have to work with as their are so many outside factors that effect it. I have control of all the mail servers I send through and there is still the ISP in the mix so it only helps some. At least i an look at the mail queue to see if the message is there.