Click to See Complete Forum and Search --> : Mail problem...pretty simple ;)


invision
03-11-2006, 09:47 AM
I'm sure this is quite a simple problem, but here goes.
I want it to say Dear 'username' in the email. Right now it just is 'Dear ,'

Any ideas ?


<?php # - mail_out.php
// This is the mail_out page for Pictures of Scotland admin to email members if they are 'subscribed' to the mailing list.

// Inclyde the configuration file for error management again
require_once ('../includes/config.inc.php');

require_once('../../mysql_connect.php');

$page_title = "Send a Mail Out!";

include ('../includes/aheader.html');

if (($_SESSION['user_id']=='1')) {

if (isset($_POST['submitted'])) { // Handle the form.

$query = "SELECT users.username, users.email, pid FROM users, profiles WHERE profiles.maillist='Y' AND users.user_id = pid";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
$row = mysql_fetch_array($result);

if (mysql_num_rows($result) != 0) {
// if it ran OK
$usern = $row['username'];
// Send the email.
$body = "Dear: ".$usern.",\n\n";
$body.= $_POST['mail_out'];
while ($row = mysql_fetch_array($result)) {
if (empty($_POST['mail_out'])) {
echo '<a href="javascript:history.go(-1)">Please insert your message to those on the mailing list, Thank you.</a>';
} else {
mail($row['email'], 'Pictures of Scotland Newsletter', stripslashes($body), 'From:Pictures of Scotland<mm021135@student.paisley.ac.uk>');
}
// Finish the page
echo '<H3>Your mail-out has been sent successfully to those on the Mailing List.</h3>';
include ('../includes/afooter.html'); // Include the HTML footer.
exit();

}

mysql_close(); // Close the connection with the Database
}
} // End of the main Submit conditional.

?> <h2>Mailing List</h2>
<p>Type in a message below to be sent to everyone on the mailing list.</p><form action="mail_out.php" method="post"><p><b>Enter Mail Out here :</b><br><textarea name="mail_out" COLS="60" ROWS="8"></textarea></p>
<div align="center"><input type="submit" class="submit" name="submit" value="Send Mail Out" /></div>
<input type="hidden" name="submitted" value="TRUE"/>
</form>

<?php
}
else {

echo '<p>Sorry, you need to be admin to enter this area.<br />';
echo '<b>Options :</b>';
echo '<ul><li><a href="javascript:history.go(-1)">Go back to the previous page</a></li>';
echo '<li><a href="login.php">Login</a></li>';
echo '<li><a href="register.php">Register</a></li>';
echo '<li><a href="forgot_password.php">Forgot Password</a></li>';
}

include ('../includes/afooter.html');
?>


Many thanks once more for taking a look-see.

bathurst_guy
03-11-2006, 09:56 AM
Looks fine. Are you sure there is a username in the table?

invision
03-11-2006, 09:58 AM
Yes, definitely a 'username' in the users table.

I think I see the problem now.

*runs off to check*

bathurst_guy
03-11-2006, 10:03 AM
Do you want this to keep going through the emails it retrieves from the db and send out to each, because at the moment it doesn't do that.
You have things outside of the loop that should be in it and its just in an illogical format

invision
03-11-2006, 10:10 AM
Hmm...

It does send to everyone on the list, just doesn't loop through there names in the 'Dear...' part.

bathurst_guy
03-11-2006, 10:11 AM
<?php # - mail_out.php
// This is the mail_out page for Pictures of Scotland admin to email members if they are 'subscribed' to the mailing list.

// Inclyde the configuration file for error management again
require_once ('../includes/config.inc.php');

require_once('../../mysql_connect.php');

$page_title = "Send a Mail Out!";

include ('../includes/aheader.html');

if (($_SESSION['user_id']=='1')) {

if (isset($_POST['submit'])) { // Handle the form.
if (empty($_POST['mail_out'])) { # Check that something was entered into the form
echo '<a href="javascript:history.go(-1)">Please insert your message to those on the mailing list, Thank you.</a>';
} else {
# Grab all the users info
$query = "SELECT users.username, users.email, pid FROM users, profiles WHERE profiles.maillist='Y' AND users.user_id = pid";
$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());

while ($row = mysql_fetch_array($result)) { # Run through all the subscribed members and send
$body = "Dear: $row['username'],\n\n";
$body.= $_POST['mail_out'];
mail($row['email'], 'Pictures of Scotland Newsletter', stripslashes($body), 'From:Pictures of Scotland<mm021135@student.paisley.ac.uk>');
}

// Finish the page
echo '<H3>Your mail-out has been sent successfully to those on the Mailing List.</h3>';
include ('../includes/afooter.html'); // Include the HTML footer.

mysql_close(); // Close the connection with the Database

exit();
}
}

?> <h2>Mailing List</h2>
<p>Type in a message below to be sent to everyone on the mailing list.</p><form action="mail_out.php" method="post"><p><b>Enter Mail Out here :</b><br><textarea name="mail_out" COLS="60" ROWS="8"></textarea></p>
<div align="center"><input type="submit" class="submit" name="submit" value="Send Mail Out" /></div>
</form>

<?php
}
else {

echo '<p>Sorry, you need to be admin to enter this area.<br />';
echo '<b>Options :</b>';
echo '<ul><li><a href="javascript:history.go(-1)">Go back to the previous page</a></li>';
echo '<li><a href="login.php">Login</a></li>';
echo '<li><a href="register.php">Register</a></li>';
echo '<li><a href="forgot_password.php">Forgot Password</a></li>';
}

include ('../includes/afooter.html');
?>

invision
03-11-2006, 10:16 AM
An error :

$body = "Dear:' . $row['username'] . ',\n\n";

Thanks for helping out.

bathurst_guy
03-11-2006, 10:18 AM
or just do
$body = "Dear: $row[username],\n\n";

invision
03-11-2006, 10:20 AM
YES!

Thanks , it worked, I copied it exactly last time, but it found an error, heh.

Thanks again.