only for RuBoard - do not distribute or recompile Previous Section Next Section

Sending Mail

Finally we come to sending mail. There are a few ways to do this from this script: The user can send a new message, reply to, or forward mail. Let's see how these work.

Sending a New Message

The user can choose this option by clicking the New Message button. This activates the "new-message" action, which executes the following code in index.php:

						
case 'new-message' :
{
  display_new_message_form($auth_user, $to, $cc, $subject, $body);
  break;
}

					

The new message form is just a form for sending mail. You can see what it looks like in Figure 27.8. This figure actually shows mail forwarding rather than new mail, but the form is the same. We'll look at forwarding and replies next.

Figure 27.8. Using mail forwarding, we can report the spammer.
graphics/27fig08.gif

Clicking the Send Message button invokes the "send-message" action, which executes the following code:

						
case 'send-message' :
{
  if(send_message($to, $cc, $subject, $message))
    echo "<p>Message sent.<br><br><br><br><br><br>";
  else
    echo "<p>Could not send message.<br><br><br><br><br><br>";
  break;
}

					

This code calls the send_message() function, which actually sends the mail. This function is shown in Listing 27.12.

Listing 27.12 send_message() Function from mail_fns.php—This Function Sends the Message that the User Has Typed In
function send_message($to, $cc, $subject, $message)
{
  //send one email via PHP

  global $auth_user;

  if (!db_connect())
  {
    return false;
  }
  $query = "select address from users where username='$auth_user'";

  $result = mysql_query($query);
  if (!$result)
  {
     return false;
  }
  else if (mysql_num_rows($result)==0)
  {
    return false;
  }
  else
  {
    $other = "From: ".mysql_result($result, 0, "address")."\r\ncc: $cc";
    if (mail($to, $subject, $message, $other))
      return true;
    else
    {
      return false;
    }
  }
}

As you can see, this function uses mail() to send the email. First, however, it loads the user's email address out of the database to use in the From field of the email.

Replying To or Forwarding Mail

The Reply, Reply All, and Forward functions all send mail in the same way that New Message does. The difference in how they work is that they fill in parts of the new message form before showing it to the user. Look back at Figure 27.8. The message we are forwarding has been indented with the > symbol, and the Subject line prefaced with To. Similarly, the Reply and Reply All options will fill in the recipients, subject line, and indented message.

The code to do this is activated in the body section of index.php, as follows:

						
case 'reply-all' :
{
  //set cc as old cc line
  if(!$imap)
    $imap = open_mailbox($auth_user, $selected_account);
  if($imap)
  {
    $header = imap_header($imap, $messageid);
    if($header->reply_toaddress)
      $to = $header->reply_toaddress;
    else
      $to = $header->fromaddress;
    $cc = $header->ccaddress;
    $subject = 'Re: '.$header->subject;
    $body = add_quoting(stripslashes(imap_body($imap, $messageid)));
    imap_close($imap);

    display_new_message_form($auth_user, $to, $cc, $subject, $body);
  }
  break;
}
case 'reply' :
{
  //set to address as reply-to or from of the current message
  if(!$imap)
    $imap = open_mailbox($auth_user, $selected_account);
  if($imap)
  {
    $header = imap_header($imap, $messageid);
    if($header->reply_toaddress)
      $to = $header->reply_toaddress;
    else
      $to = $header->fromaddress;
    $subject = 'Re: '.$header->subject;
    $body = add_quoting(stripslashes(imap_body($imap, $messageid)));
    imap_close($imap);

    display_new_message_form($auth_user, $to, $cc, $subject, $body);
  }

  break;        //note deliberately no 'break'
}
case 'forward' :
{
  //set message as quoted body of current message
  if(!$imap)
    $imap = open_mailbox($auth_user, $selected_account);
  if($imap)
  {
    $header = imap_header($imap, $messageid);
    $body = add_quoting(stripslashes(imap_body($imap, $messageid)));
    $subject = 'Fwd: '.$header->subject;
    imap_close($imap);

    display_new_message_form($auth_user, $to, $cc, $subject, $body);
  }
  break;
}

					

You can see that each of these options sets up the appropriate headers, applies formatting as necessary, and calls the display_new_message_form() function to set up the form.

That's the full set of functionality for our Web mail reader.

only for RuBoard - do not distribute or recompile Previous Section Next Section