Location: PHPKode > projects > MyMail Webmail > send.php
<?php
include('global.php');

session_start();
if (!session_is_registered("SESSION"))
{
	header("Location: error.php?ec=2");
	exit;
}

// ensure that at least one field has values 
if (!$to && !$cc && !$bcc)
{
	header("Location: error.php?ec=6");
	exit;
}

// check for valid file if attachment exists
if ($attachment_name && $attachment_size <= 0)
{
	header("Location: error.php?ec=7");
	exit;
}

// append signature to body
$sig = "\r\n--\r\nMessage Sent Using MyMail.\r\n";
$body .= $sig;

// put all addresses into a single string
if($to) { $addresses .= $to . ","; }
if($cc) { $addresses .= $cc . ","; }
if($bcc) { $addresses .= $bcc . ","; }

// split address list into array
$all = split(",", $addresses);

// clean addresses
array_walk($all, "clean_address");

// validate each address further here
for($x=0; $x<sizeof($all); $x++)
{
	if($all[$x] == "")
	{
	continue;
	}

	if(!validate_email($all[$x]))
	{
	header("Location: error.php?ec=8");
	exit;
	}
}

// build message headers
$headers = "From: $from\r\n";

if($cc) { $headers .= "Cc: $cc\r\n"; }
if($bcc) { $headers .= "Bcc: $bcc\r\n"; }

// if attachments exist
if($attachment_name || sizeof($amsg) > 0)
{

// create a MIME boundary string
$boundary = "=====MYMAIL." . md5(uniqid(time())) . "=====";

// add MIME data to the message headers
$headers .= "MIME-Version:1.0\r\n";
$headers .= "Content-Type: multipart/mixed; \r\n\tboundary=\"$boundary\"\r\n\r\n";

// start building a MIME message
// first part is always the message body
// encode as 7-bit text
$str = "--" . $boundary . "\r\n";
$str .= "Content-Type: text/plain;\r\n\tcharset=\"us-ascii\"\r\n";
$str .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$str .= "$body\r\n\r\n";


	// if forwarded message, array $amsg[] will exist
	// and contain list of attachments to be forwarded
	if (sizeof($amsg) > 0)
	{
	// open message to be forwarded and parse it to find attachment
	$inbox = imap_open("{localhost:110/pop3/notls}INBOX",$_SESSION['SESSION_USER_NAME'], $_SESSION['SESSION_USER_PASS'], OP_HALFOPEN) or header("Location: error.php?ec=3");	
	$structure = imap_fetchstructure($inbox, $id);
	$sections = parse($structure);

		// go through attachment list and message section
		// if a match exists, create a MIME section and import that attachment into the message
		// do this as many times as there are attachments to be included
		for ($x=0; $x<sizeof($amsg); $x++)
		{
			for ($y=0; $y<sizeof($sections); $y++)
			{
				if($amsg[$x] == $sections[$y]["pid"])
				{
				$data = imap_fetchbody($inbox, $id, $sections[$y]["pid"]);
				$str .= "--" . $boundary . "\r\n";
				$str .= "Content-Type: " . $sections[$y]["type"] . ";\r\n\tname=\"" . $sections[$y]["name"] . "\"\r\n";
				$str .= "Content-Transfer-Encoding: base64\r\n";
				$str .= "Content-Disposition: attachment; \r\n\tfilename=\"" . $sections[$y]["name"] . "\"\r\n\r\n";
				$str .= $data . "\r\n";
				}
			}

		}
	}
	
	// if an uploaded attachment exists
	// encode it and attach it as a MIME-encoded section
	if ($attachment_name)
	{
	$fp = fopen($attachment, "rb");
	$data = fread($fp, filesize($attachment));
	$data = chunk_split(base64_encode($data));
	fclose($fp);
	
	#Ad MIME
	$str .= "--" . $boundary . "\r\n";
	$str .= "Content-Type: " . $attachment_type . ";\r\n\tname=\"" . $attachment_name . "\"\r\n";
	$str .= "Content-Transfer-Encoding: base64\r\n";
	$str .= "Content-Disposition: attachment; \r\n\tfilename=\"" . $attachment_name . "\"\r\n\r\n";
	$str .= $data . "\r\n";
	}
	
// all done
// add the final MIME boundary
$str .= "\r\n--$boundary--\r\n";

#assign the contents of $str to $body
#note that the original contents of $body will be lost
$body = $str;
}

#Send Message
if(mail($to, $subject, $body, $headers))
{
	$status = "<br><br><div align=center>Your message has been sent!</div>.";
}
else
{
	$status = "We encountered an error while sending your message.";
}
?>
<html>
<head>
<title>MyMail :: Write New Message</title>
<style type="text/css">
<!--
.dir A:link {color: #142E56; text-decoration: underline;}
.dir A:visited {color: #142E56; text-decoration: underline;}
.dir A:active {color: #142E56; text-decoration: underline;}
.dir A:hover {background-color: #CCCCCC; text-decoration: underline;}

.top A:link {color: #FFFFFF; text-decoration: none;}
.top A:visited {color: #FFFFFF; text-decoration: none;}
.top A:active {color: #FFFFFF; text-decoration: none;}
.top A:hover {color: #FFD400; text-decoration: underline;}
-->
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000"  link="#FFFFFF"  alink="#FFFFFF"  vlink="#FFFFFF"  marginwidth="0" marginheight="0" leftmargin="0" rightmargin="0" bottommargin="0" topmargin="0">
<table width=100% cellpadding=0 cellspacing=0 border=0>
  <tr width=100% height=124>
    <td width=100% height=124 background="./bg_top.gif">
      <table width=100% height=124 border=0 cellpadding=0 cellspacing=0>
        <tr width=100% height=124>
          <td width=99% height=124 valign=top align=left background="">
            <!-- left segment -->
            <table width=100% height=124 border=0 cellpadding=0 cellspacing=0>
              <tr width=100% height=66>
                <td width=100% height=66 valign=middle align=left>
                  <table border=0 height=66 cellpadding=0 cellspacing=0  width=100%>
                    <tr>
                      <td valign=middle height=66><br>
                      <nobr></nobr></td>
                      <td valign=middle height=66><br>
                      </td>
                      <td valign=middle height=66 width=100% align=center></td>
                    </tr>
                  </table>
                </td>
              </tr>
              <tr width=100% height=21>
                <td width=100% height=21 valign=middle align=right>&nbsp;</td>
              </tr>
              <tr width=100% height=17>
                <td width=100% height=17 valign=middle align=right background=""> <font face="Verdana,arial,helvetica,sans-serif" size=2> <font face="arial, helvetica" size=1 color=white><center>
                <font face="arial, helvetica" size=1 color=white><a href="./list.php">Inbox</a> |
                <a href="./compose.php">Write Message </a>| <a href="./logout.php">Logout</a></font>                
                </center></font></td>
              </tr>
              <tr width=100% height=20>
                <td width=100% height=20 valign=middle align=left background="">&nbsp;</td>
              </tr>
            </table>
          </td>
          <!-- right segment -->
          <td width=1% valign=top align=right height=124>&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
  <tr width=100% height=100%>
    <td width=100% height=100% background="">
      <table width="974" border=0 cellpadding=0 cellspacing=0>
        <tr width=150>
          <td valign=top align=center>
            <table width=95% height="320" border=0 cellpadding=15 cellspacing=0>
              <tr width=100%>
                <td width=100% valign=top align=left class="dir"><p align="center">
                  <p align="center"><font size="2" face="Geneva, Arial, Helvetica, sans-serif">
<p><div align="center"><? echo $status; ?>
  <p><br>
        <font size="2" face="Geneva, Arial, Helvetica, sans-serif">You can now <a href="./list.php">return to your Inbox</a>, or <a href="./compose.php">create a new mail message.</a></font></p>
  </div>
                  </p>
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <td align=center> <font color=#666666 face="arial, helvetica, sans-serif" size=1>&copy; 2003
              Resiware, Inc.</font> </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Return current item: MyMail Webmail