Page 1 of 1

Undefined Variables in PHP4.3.2

Posted: Fri Apr 16, 2004 9:18 am
by Catherine
I am new to using PHP and am receiving the following errors:

1)Notice: Undefined variable: subject in c:\program files\apache group\apache\htdocs\sendnewslettercode.php on line 16

2)Notice: Undefined variable: body in c:\program files\apache group\apache\htdocs\sendnewslettercode.php on line 16

3)Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in c:\program files\apache group\apache\htdocs\sendnewslettercode.php on line 16

the following is the code

Code: Select all

<?php
//make a connection
$connection = mysql_connect("localhost") or die("cannot make connection");
//select the database
$db = mysql_select_db("email" , $connection) or die("cannot find database");
$sql_query = "SELECT email FROM email";
$result = mysql_query($sql_query) or die("cannot execute query");
$header = "From : me@localhost.com";
//loop through rows
while ($row = mysql_fetch_array($result))
{
$address = $row[0];
//mail function requires email address($address)
//The subject($subject) , the message ($body) and
//the $header info
mail($address ,$subject , $body , $header);
//display a message confirming email has been sent
echo ("newsletter sent to : $address<br>");
}
echo ("Completed sending emails");

?>

Any help with this problem would be greatly appreciated
Thanks
Catherine

[Edit: Added PHP tags around code for eyecandy. --JAM]

Posted: Fri Apr 16, 2004 9:34 am
by magicrobotmonkey
If that's your whole code, you're getting the first two errors because you never define $body for the body of the email or $subject for the subject. the line mail($address ,$subject , $body , $header); calls four variables, only two of which have been defined.

Posted: Fri Apr 16, 2004 9:38 am
by JAM
Issues 1 & 2:

You need to assign values to the $subject and $body variables before they are being used. Simple example of non working:

Code: Select all

<?php
 echo $foo;
?>
Example of working:

Code: Select all

<?php
 $foo = 'bar';
 echo $foo;
?>
Issue 3:
The Windows implementation of mail() differs in many ways from the Unix implementation.
Read more about it here: http://www.php.net/manual/en/function.mail.php

You need to edit your information in the php.ini and add the correct values of your email provider, ie. change "localhost" to "your.isp.com" as example.

Hope it gave you more ideas.

Posted: Fri Apr 16, 2004 9:43 am
by Catherine
This is the form where the body and the subject are written and then the previous code is executed:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action = "sendnewslettercode.php" method = "post">
<table>
<tr>
<td>Subject : </td>
<td>
<input type = "text" name = "subject" size="70">
</td>
</tr><tr>
<td>Body : </td>
<td><textarea name = "body" cols = "50" rows = "20" wrap = virtual></textarea></td>
</tr><tr><td>
<input type = "submit" value = "create newsletter">
</td><td>
<input type = "reset">
</td></tr>
</table>
</form>

</body>
</html>

I assume that the are declared in here.
Thanks again for your help
Catherine

Posted: Fri Apr 16, 2004 9:53 am
by magicrobotmonkey
on the second page, change $body and $subject to $_POST['body'] and $_POST['subject'], respectivly. That should take care of the first two errors, but you still need to figure out what your mail server is.
look here for more info about the undefined variables problem:

http://us2.php.net/register_globals

Posted: Fri Apr 16, 2004 10:03 am
by Catherine
It worked .
Thank you soooooooooooooo much. I really appreciate it and you will never know how much.

Catherine

Posted: Fri Apr 16, 2004 10:07 am
by magicrobotmonkey
never??