Quick Search:
Oracle PL/SQL Code Library
Find Or Post Oracle Jobs
Oracle Discussion & Chat
 PHP Code: Mini private messaging system Jump to:  
Category: >> PHP Code >> Mini private messaging system Bookmark and Share

<< lastnext >>

Snippet Name: Mini private messaging system

Description: Email a user without exposing email address.

Keeping emails hidden is an increasing concern with all the spam floating around. Here is a way using MySQL and a contact form to allow a group of users on a site to contact each other safely.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

//select the email address' from your db 
CREATE TABLE `contacts` ( 
  `id` int(10) NOT NULL auto_increment, 
  `first_name` varchar(30) NOT NULL default '', 
  `last_name` varchar(50) NOT NULL default '', 
  `email` varchar(75) default NULL, 
  `contact_status` tinyint(1) NOT NULL default '0', 
  PRIMARY KEY  (`id`) 
IF (ISSET($_POST['submit'])) 
//submit button pushed call the send_email function 
//nothing has been pushed so show the form 
}//end if 
send_mail function 
FUNCTION send_email() 
        //default values for elements 
        $subject    = ''; 
        $id         = ''; 
        $message    = ''; 
        $your_name  = ''; 
        $your_email = ''; 
        $err_msg    = ''; 
        $headers    = ''; 
        //get the values from the form handle any errors 
          $subject = $_POST['subject']; 
          $id = $_POST['email']; 
          $message = $_POST['message']; 
          $your_name = $_POST['your_name']; 
          $your_email = $_POST['your_email']; 
    IF ($id == "" ) 
      $err_msg = "No person chosen."; 
        //sender's email 
        IF(!EREGI("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $your_email)) 
          $err_msg .= "Your email is not valid. Please re-enter it<br />"; 
          $your_email = ''; 
        //check to see if the other elements have values 
          $err_msg .= "No message set. Please enter a message.<br />"; 
          $err_msg .= "No subject set. Please enter a subject.<br />"; 
          $err_msg .= "No sender name set. Please enter a your name.<br />"; 
        //check the err_msg to see if there are any messages 
        IF ($err_msg != ''){ 
        //there is an error so build the data into a pipe delimited string and pass it back to the form 
        $data = "$id|$subject|$message|$your_email|$your_name"; 
        }//end if 
        //get the email from the db and send it 
        $sql = "select email from contacts where id = $id"; 
        $result = conn($sql); 
          $row   = MYSQL_FETCH_ARRAY($result); 
          $email = $row['email']; 
        //send the email 
        //build the headers 
        $headers .= "MIME-Version: 1.0\n"; 
        $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
        $headers .= "X-Priority: 1\n"; 
        $headers .= "X-MSMail-Priority: High\n"; 
        $headers .= "X-Mailer: PHP\n"; 
        $headers .= "From: \"".$your_name."\" <".$your_email.">\n"; 
        IF (!MAIL($email, $subject, $message, $headers)) 
          ECHO "Email failed!\nTry again"; 
          ECHO "<script language='javascript'>alert('Mail sent');</script>"; 
          ECHO "<script language='javascript'>window.location='thankyou.htm';</script>"; 
        }//end if 
}//end function 
show_form function 
FUNCTION show_form($data='',$msg='') 
        //show the form for the email 
        //the $data='' and $msg='' constructs allow for no information to be passed to the function 
        //set defaults for function 
        $subject     = ''; 
        $email       = ''; 
        $message     = ''; 
        $your_name   = ''; 
        $your_email  = ''; 
        //explode the string passed back from the send_mail function if there is an error 
        IF (($data !="" )&&($msg != "")) 
                $elements    = EXPLODE("|",$data); 
                $email       = $elements[0]; 
                $subject     = $elements[1]; 
                $message     = $elements[2]; 
                $your_email  = $elements[3]; 
                $your_name   = $elements[4]; 
        }//end if 
        optional where clauses could include: 
        1. only active users 
        2. check to see if the user wants to allow contact from others on the site 
        3. only new users (signed up within a certain date) 
        $sql = "SELECT * FROM contacts";//[optional where clause to show only people who choose to allow contact] 
    $result = conn($sql); 
        ECHO "<head><title>Make A Friend</title></head><body>\n"; 
        ECHO "<table width='100%' cellpadding='0' cellspacing='0'>\n"; 
        ECHO "<form name='email' action='".$_SERVER['PHP_SELF']."' method='post'>\n"; 
        ECHO "<tr><td colspan='2' align='center'><h2>Email A Friend</h2></td></tr>\n"; 
        ECHO "<tr><td colspan='2' align='center'> </td></tr>\n"; 
        ECHO "<tr><td colspan='2' align='center' style='color:red; font-weight:bold;'>$msg</td></tr>\n"; 
        //produce the email drop down 
        IF (MYSQL_NUM_ROWS($result)>0){ 
          //produce the drop down list 
          ECHO "<tr><td width='50%' align='right'>Name: </td><td><select name='email'>\n"; //optionally add MULTIPLE to allow sending to multiple addresses 
          ECHO "<option value=''>Choose One</option>\n"; 
          WHILE ($rows = MYSQL_FETCH_ARRAY($result)){ 
            ECHO "<option value='".$rows['id']."'>".$rows['first_name'].' '.$rows['last_name']."</option>\n"; 
          }//end while 
          ECHO "</select></td></tr>"; 
        //if there is a problem, have the user manually enter the email address 
        ECHO "<tr><td colspan='2' align='center' style='color:red; font-weight:bold;'><br />Currently unable to locate email addresses. There maybe a problem with the database.</td></tr>"; 
        ECHO "<tr><td colspan='2'> </td></tr>"; 
        ECHO "<tr><td colspan='2' align='center' style='color:red; font-weight:bold;'>Please enter the email address manually</td></tr>\n"; 
        ECHO "<tr><td width='40%' align='right'>Email Address: </td><td><input type='text' name='email' size='50' value='$email'></td></tr>\n"; 
        }//end if 
        ECHO "<tr><td align='right'>Subject: </td> <td><input type='text' name='subject' size='50' value='$subject'></td></tr>\n"; 
        ECHO "<tr><td align='right' valign='top'>Message: </td> <td><textarea name='message' cols='25' rows='10'>$message</txtarea></td></tr>\n"; 
        ECHO "<tr><td align='right'>Your name: </td> <td><input type='text' name='your_name' cols='25' rows='10' value='$your_name' ></td></tr>\n"; 
        ECHO "<tr><td align='right'>Your Email: </td> <td><input type='text' name='your_email' cols='25' rows='10' value='$your_email'></td></tr>\n"; 
        ECHO "<tr><td colspan='2' align='center'><input type='submit' value='Send Email' name='submit'><input type='reset' value='Reset'></td></tr>\n"; 
        ECHO "</form></table></body>\n"; 
}//end function 
               db connection function 
FUNCTION conn($sql) 
$host = "localhost"; 
$user = "user"; 
$pass = "pass"; 
$db     = "my_db"; 
    //echo "commnecing connection to local db<br>"; 
    IF (!($conn=MYSQL_CONNECT($host, $user, $pass)))  { 
        PRINTF("error connecting to DB by user = $user and pwd=$pass"); 
    $db3=MYSQL_SELECT_DB($db,$conn) or DIE("Unable to connect to local database"); 
    $result = MYSQL_QUERY($sql) or DIE ("Can't run query because ". MYSQL_ERROR()); 
    RETURN $result; 
}//end function      

Oracle Magazine
and Oracle White Papers

SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 36 users online    © 2009 psoug.org

Forgot your password?