...

View Full Version : Resolved php mail problem



FWDrew
12-13-2008, 11:47 PM
Hi everyone,

Im having a small issue with what should be a simple mailing script. Here is the relevant code, that is returning a 500 server error when submitted:

Form:

<form action="send.php" method="post">
<p>Subject:</p>
<p><input type="text" name="subject" /></p>
<p>Mail Body:</p>
<p><textarea rows="10" cols="40" name="body"></textarea></p>
<p><input type="submit" name="submitMail" value="Send" /></p>
</form>

send.php

<?php require('conn.php');?>
<?php
if($_SESSION['loggedIn'] != 'LOGGEDIN') {
header('Location:index.php');
}
elseif($_POST['submitMail'] && !empty($_POST['subject']) && !empty($_POST['body'])) {
$subject = strip_tags($_POST['subject']);
$body = strip_tags($_POST['body'], '<p><a><b><i><quote>');
$mailHeaders = 'Email Newsletter';
//Setup query
$result = mysql_query("SELECT email FROM subscribers") or die(mysql_error());
while(mysql_fetch_array($result)){
$email = $row['email'];
mail($email, $subject, $body, $mailHeaders);
echo "<p>Email sent to the address of " .$row['email']. ".</p>";
}
mysql_free_result($result);
mysql_close($connection);
}
else {
header('Location:index.php');
}
?>

in case you need it here is conn.php but I am not getting any mysql errors:

<?php
session_start();
$host = "xxxxxxxxxx";
$user = "xxxxxxxxxx";
$pass = "xxxxxxxxxx";
$db = "xxxxxxxxx";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect to database!");
mysql_select_db($db) or die ("Unable to select database!");
?>

There are no reasons given with the 500 server error, just to contact the server administrator, any ideas?

Thanks much,

Drew

CFMaBiSmAd
12-14-2008, 12:35 AM
The posted code does not generate a fatal parse error (tested), so it is likely that you have a fatal runtime error. Add the following two lines immediately after your first opening <?php tag, before the require() statement -


ini_set ("display_errors", "1");
error_reporting(E_ALL);

FWDrew
12-14-2008, 01:00 AM
The posted code does not generate a fatal parse error (tested), so it is likely that you have a fatal runtime error. Add the following two lines immediately after your first opening <?php tag, before the require() statement -


ini_set ("display_errors", "1");
error_reporting(E_ALL);

Updated code:

<?php ini_set ("display_errors", "1"); error_reporting(E_ALL);require('conn.php');?>
<?php
if($_SESSION['loggedIn'] != 'LOGGEDIN') {
header('Location:index.php');
}
elseif($_POST['submitMail'] && !empty($_POST['subject']) && !empty($_POST['body'])) {
$subject = strip_tags($_POST['subject']);
$body = strip_tags($_POST['body'], '<p><a><b><i><quote>');
$mailHeaders = 'Email Newsletter from Lifecoach Sam Douglass';
//Setup query
$result = mysql_query("SELECT email FROM subscribers") or die(mysql_error());
while(mysql_fetch_array($result)){
$email = $row['email'];
mail($email, $subject, $body, $mailHeaders);
echo "<p>Email sent to the address of " .$email. ".</p>";
}
mysql_free_result($result);
mysql_close($connection);
}
else {
header('Location:index.php');
}
?>

Is now generating this result:

Notice: Undefined variable: row in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx on line 13
No recipient addresses found in header

Email sent to the address of .

repeats this error 4 times (4 rows in my db). I double check the email field for spelling and to make sure it isnt null and they are all filled and spelled correctly. I cant see what im doing wrong with $row['email'] which is what seems to be the error.

Thanks for helping me find the error messages, I'm still learning php, any advice now?

Drew

CFMaBiSmAd
12-14-2008, 01:09 AM
This line -


while(mysql_fetch_array($result)){

should be -


while($row = mysql_fetch_array($result)){

FWDrew
12-14-2008, 01:13 AM
This line -


while(mysql_fetch_array($result)){

should be -


while($row = mysql_fetch_array($result)){

Problem solved, thank you very much :)

I knew it had to be missing something simple. Appreciate all the help.

Drew



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum