mailing/messaging system on a website.

Hi guys, just wondering how I do the above, wondering if its possible using the mail function, all I want is for two that are logged to be able to send messages to each other, is it possible using there user names rather than email addresses usingthe mail function? Thx guys.

unless the usernames are their email addresses the answer is no. I assume you have a database with a ‘users’ table so they can log in. You need to have a form with the subject, the message and any other information you need, which uses POST to pass the values. Then you need to perform a SQL query to get the email address of the user you are sending the email to…

[php]
//isset checks to see if the form was submitted, if it was then the following is done
if(isset($_POST[‘message’])) {

//$_POST gets the data from the form, the name in the [’ '] should be the name of the tag in the form
//e.g. is got using $_POST[‘subject’];

$subject = $_POST['subject'];
$message = $_POST['message'];

//this queries the database for the users email address. The $sendTo variable is the username of the person you are sending the message to. I dont know how you acquire this in your script but it is needed.

$result=mysql_query("SELECT * FROM users WHERE username='$sendTo'");
$num = mysql_num_rows($result);
$row= mysql_fetch_array($result);

//checks that there is a user in the database with that username, if not then a message is shown
if($num == ‘0’) {

	echo "User Not Found, Cannot Send Email";

} else {

//the email address of the user is found in the database
$email = $row[‘email_address’];

//$user should be the username of the user who is sending the email
$headers = ‘From: $user’;

//the email is sent
mail($email, $subject, $message, $headers);
}
} else {

//you need to construct the form here, it must have an and the form must be enclosed in .

}
[/php]

I hope this helps, give it a go and see if you understand it. If you still need help give another shout.

Thx, all im try to do now is place the message in the database were the username and password match, using insert into with a were clause won’t work, tryed update but that places a blank message in the database.

Sponsor our Newsletter | Privacy Policy | Terms of Service