<?php
require_once('email.class.php');
if ((empty($_SESSION['username'])) || (empty($_SESSION['password'])))
{
header('Location:index.php');
exit;
}
$attachedfiles=array();
$mailto='';
$subject='';
$message='';
if (isset($_GET['edit']))
{
}
if (isset($_GET['forward']))
{
$mbox=imap_open('{'.MAILHOST.'/norsh/notls}'.$mailbox,$_SESSION['username'],decryptpw($_SESSION['password']));
$header=imap_header($mbox,$_GET['forward']);
$body=imap_fetchbody($mbox,$_GET['forward'],1);
if (isset($header->from[0]->personal))
{
$sender=$header->from[0]->personal.' <'.$header->from[0]->mailbox.'@'.$header->from[0]->host.'>';
}
else
{
$sender=$header->from[0]->mailbox.'@'.$header->from[0]->host;
}
$subject='FW: '.$header->subject;
$message='Original message from '.$sender.":\n";
$message.=str_replace("\r\n","\n",$body);
imap_close($mbox);
}
if (isset($_GET['replyto']))
{
$mbox=imap_open('{'.MAILHOST.'/norsh/notls}'.$mailbox,$_SESSION['username'],decryptpw($_SESSION['password']));
$header=imap_header($mbox,$_GET['replyto']);
$body=imap_fetchbody($mbox,$_GET['replyto'],1);
if (isset($header->from[0]->personal))
{
$mailto=$header->from[0]->personal.' <'.$header->from[0]->mailbox.'@'.$header->from[0]->host.'>';
}
else
{
$mailto=$header->from[0]->mailbox.'@'.$header->from[0]->host;
}
$subject='RE: '.$header->subject;
$message="Original message:\n";
$message.=str_replace("\r\n","\n",$body);
imap_close($mbox);
}
if (!empty($_POST))
{
$keys=array_keys($_POST);
for ($count=0;$count<count($keys);$count++)
{
if (substr($keys[$count],0,10)=='attachment')
{
$attachedfiles[]=$_POST[$keys[$count]];
}
}
}
if ((isset($_POST['attachfile'])) || (isset($_POST['removeattachment'])))
{
if (!empty($_POST['mailto']))
{
$mailto=$_POST['mailto'];
}
if (!empty($_POST['subject']))
{
$subject=$_POST['subject'];
}
if (!empty($_POST['message']))
{
$message=$_POST['message'];
}
if (isset($_POST['attachfile']))
{
$tmpname=$_FILES['uploadfile']['tmp_name'];
$filename=$_FILES['uploadfile']['name'];
$filesize=$_FILES['uploadfile']['size'];
if ($filesize>0)
{
if (!file_exists('mailtemp/'.session_id()))
{
createtempdir();
}
storetempfile($tmpname,$filename);
$attachedfiles[]=$filename;
}
}
if (isset($_POST['removeattachment']))
{
for ($count=0;$count<count($keys);$count++)
{
if (substr($keys[$count],0,13)=='delattachment')
{
$filename=$_POST[$keys[$count]];
$delpositions[]=substr($keys[$count],13,1);
removetempfile($filename);
}
}
$newattachedfiles=array();
for ($count=0;$count<count($attachedfiles);$count++)
{
if (!in_array($count,$delpositions))
{
$newattachedfiles[]=$attachedfiles[$count];
}
}
$attachedfiles=$newattachedfiles;
unset($newattachedfiles);
}
}
if (((isset($_POST['sendmail'])) || (isset($_POST['savedraft']))) && ((!empty($_POST['mailto'])) && (!empty($_POST['subject'])) && (!empty($_POST['message']))))
{
$from=$_SESSION['username'];
if (strpos($from,'@')===false)
{
$from.='@'.DOMAIN;
}
$mailto=$_POST['mailto'];
if (strpos($mailto,'@')===false)
{
$mailto.='@'.DOMAIN;
}
$mail=new email($from,$mailto,$_POST['subject'],$_POST['message']);
for ($x=0;$x<count($attachedfiles);$x++)
{
$mail->addattachment('mailtemp/'.session_id().'/'.$attachedfiles[$x]);
}
if (isset($_POST['sendmail']))
{
$smtp=new smtpconnection(MAILHOST,SMTPAUTH,$_SESSION['username'],decryptpw($_SESSION['password']),SSLCONNECT);
$smtp->sendmail($mail->composemail());
unset($smtp);
}
if (((isset($_POST['sendmail'])) && (isset($_POST['savesent']))) || (isset($_POST['savedraft'])))
{
if ((isset($_POST['savedraft'])) && (getfolder('Drafts')!=false))
{
$mbox=imap_open('{'.MAILHOST.'/norsh/notls}'.getfolder('Drafts'),$_SESSION['username'],decryptpw($_SESSION['password']));
imap_append($mbox,'{'.MAILHOST.'}'.getfolder('Drafts'),$mail->composemail());
$mboxinfo=imap_mailboxmsginfo($mbox);
imap_setflag_full($mbox,$mboxinfo->Nmsgs,'\Seen \Draft');
imap_close($mbox);
}
elseif (getfolder('Sent')!=false)
{
$mbox=imap_open('{'.MAILHOST.'/norsh/notls}'.getfolder('Sent'),$_SESSION['username'],decryptpw($_SESSION['password']));
imap_append($mbox,'{'.MAILHOST.'}'.getfolder('Sent'),$mail->composemail());
$mboxinfo=imap_mailboxmsginfo($mbox);
imap_setflag_full($mbox,$mboxinfo->Nmsgs,'\Seen');
imap_close($mbox);
}
}
unset($mail);
for ($x=0;$x<count($attachedfiles);$x++)
{
removetempfile($attachedfiles[$x]);
}
if (file_exists('mailtemp/'.session_id()))
{
removetempdir();
}
header('Location:webmail.php?mailbox='.$mailbox.'&subsite=showmails.php');
exit;
}
echo '<html>';
echo '<body>';
echo '<form method="post" action="webmail.php?mailbox='.$mailbox.'&subsite=sendmail.php" enctype="multipart/form-data">';
echo gettext('Recipient').':<input type="text" name="mailto" value="'.$mailto.'"><br>';
echo gettext('Subject').':<input type="text" name="subject" value="'.$subject.'"><br>';
echo '<textarea name="message" cols="80" rows="15">'.$message.'</textarea><br>';
for ($attachment=0;$attachment<count($attachedfiles);$attachment++)
{
echo '<input type="hidden" name="attachment'.$attachment.'" value="'.$attachedfiles[$attachment].'">';
echo '<input type="checkbox" name="delattachment'.$attachment.'" value="'.$attachedfiles[$attachment].'">'.$attachedfiles[$attachment].'<br>';
}
if (count($attachedfiles)>0)
{
echo '<input type="submit" name="removeattachment" value="'.gettext('Remove selected attachments').'"><br>';
}
echo '<input type="file" name="uploadfile">';
echo '<input type="submit" name="attachfile" value="'.gettext('Attach file').'"><br>';
echo '<input type="checkbox" name="savesent" value="savesent" checked>'.gettext("Save copy in sent eMails folder").'<br>';
echo '<input type="submit" name="sendmail" value="'.gettext("Send eMail").'">';
echo '<input type="submit" name="savedraft" value="'.gettext("Save as Draft").'">';
echo '</form>';
echo '</body>';
echo '</html>';
?>