home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / email / send.php < prev    next >
PHP Script  |  2004-03-08  |  16KB  |  447 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. require("../../Group-Office.php");
  14. $GO_SECURITY->authenticate();
  15. $GO_MODULES->authenticate('email');
  16. require($GO_LANGUAGE->get_language_file('email'));
  17.  
  18. require($GO_CONFIG->class_path."phpmailer/class.phpmailer.php");
  19. require($GO_CONFIG->class_path."phpmailer/class.smtp.php");
  20. require($GO_CONFIG->class_path."html2text.class.inc");
  21. require($GO_CONFIG->class_path."email.class.inc");
  22. $email = new email();
  23.  
  24. if ($tp_plugin = $GO_MODULES->get_plugin('templates', 'addressbook'))
  25. {
  26.     require($GO_CONFIG->class_path.'templates.class.inc');
  27.     $tp = new templates();
  28. }
  29.  
  30. $html_mail_head = '<html><head><meta content="Group-Office '.$GO_CONFIG->version.'" name="GENERATOR"></head><body>';
  31. $html_mail_foot = '</body></html>';
  32.  
  33. $_SESSION['num_attach'] = isset($_SESSION['num_attach']) ? $_SESSION['num_attach'] : 0;
  34.  
  35. $mail_subject = isset($_REQUEST['mail_subject']) ? smartstrip($_REQUEST['mail_subject']) : '';
  36. $mail_body = isset($_REQUEST['mail_body']) ? smartstrip($_REQUEST['mail_body']) : '';
  37. $mail_from = isset($_REQUEST['mail_from']) ? $_REQUEST['mail_from'] : 0;
  38. $mail_to = isset($_REQUEST['mail_to']) ? $_REQUEST['mail_to'] : '';
  39. $mail_cc = isset($_REQUEST['mail_cc']) ? $_REQUEST['mail_cc'] : '';
  40. $mail_bcc = isset($_REQUEST['mail_bcc']) ? $_REQUEST['mail_bcc'] : '';
  41. $uid = isset($_REQUEST['uid']) ? $_REQUEST['uid'] : 0;
  42.  
  43. $mailing_group_id = isset($_REQUEST['mailing_group_id']) ? $_REQUEST['mailing_group_id'] : 0;
  44.  
  45. $browser = detect_browser();
  46. if ($browser['name'] == 'MSIE' && $browser['version'] >= 5.5 || $browser['name'] == 'MOZILLA' && $browser['version'] > 4.0)
  47. {
  48.     $wysiwyg = true;
  49.     $GO_MAIL_FORMAT = isset($_COOKIE['GO_MAIL_FORMAT']) ? $_COOKIE['GO_MAIL_FORMAT'] : 'text/HTML';
  50.     $content_type = isset($_POST['content_type']) ? $_POST['content_type'] : $GO_MAIL_FORMAT;
  51. }else
  52. {
  53.     $content_type = 'text/PLAIN';
  54.     $wysiwyg = false;
  55. }
  56.  
  57. $page_title = $ml_compose;
  58. $sendaction = isset($_REQUEST['sendaction']) ? $_REQUEST['sendaction'] : '';
  59. $attachments_size = 0;
  60.  
  61.  
  62. switch ($sendaction)
  63. {
  64.     case 'add':
  65.         //Adding the new file to the array
  66.         if (is_uploaded_file($_FILES['mail_att']['tmp_name']))
  67.         {
  68.             // Counting the attachments number in the array
  69.             if (isset($_SESSION['attach_array']))
  70.             {
  71.                 for($i=1;$i<=count($_SESSION['attach_array']);$i++)
  72.                 {
  73.                     $attachments_size += $_SESSION['attach_array'][$i]->file_size;
  74.                 }
  75.             }
  76.             $attachments_size += $_FILES['mail_att']['size'];
  77.             if ($attachments_size < $GO_CONFIG->max_attachment_size)
  78.             {
  79.                 $tmp_file = $GO_CONFIG->tmpdir.md5(uniqid(time()));
  80.                 copy($_FILES['mail_att']['tmp_name'], $tmp_file);
  81.                 $email->register_attachment($tmp_file, basename($_FILES['mail_att']['name']), $_FILES['mail_att']['size'], $_FILES['mail_att']['type']);
  82.             }else
  83.             {
  84.                 $feedback = '<script type="text/javascript">alert("'.$ml_file_too_big.format_size($GO_CONFIG->max_attachment_size).' ('.number_format($GO_CONFIG->max_attachment_size, 0, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']).' bytes)");</script>';
  85.             }
  86.         }else
  87.         {
  88.             $feedback = '<script type="text/javascript">alert("'.$ml_file_too_big.format_size($GO_CONFIG->max_attachment_size).' ('.number_format($GO_CONFIG->max_attachment_size, 0, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']).' bytes)");</script>';
  89.         }
  90.     break;
  91.  
  92.     case 'send':
  93.         if (!isset($_POST['mail_from']))
  94.         {
  95.             require($GO_CONFIG->class_path."users.class.inc");
  96.             $users = new users();
  97.             $profile = $users->get_user($GO_SECURITY->user_id);
  98.             $middle_name = $profile['middle_name'] == '' ? '' : $profile['middle_name'].' ';
  99.             $name = $profile['first_name'].' '.$middle_name.$profile['last_name'];
  100.         }else
  101.         {
  102.             $profile = $email->get_account($_POST['mail_from']);
  103.             $name = $profile["name"];
  104.         }
  105.  
  106.         $mail = new PHPMailer();
  107.         $mail->PluginDir = $GO_CONFIG->class_path.'phpmailer/';
  108.         $mail->SetLanguage($php_mailer_lang, $GO_CONFIG->class_path.'phpmailer/language/');
  109.  
  110.         if ($GO_CONFIG->smtp_server != '' && $GO_CONFIG->smtp_port != '')
  111.         {
  112.             $mail->IsSMTP();
  113.             $mail->Host = $GO_CONFIG->smtp_server;
  114.             $mail->Port = $GO_CONFIG->smtp_port;
  115.         }
  116.         $mail->Priority = $_POST['priority'];
  117.         $mail->From     = $profile["email"];
  118.         $mail->FromName = $name;
  119.         $mail->AddReplyTo($profile["email"],$name);
  120.         $mail->WordWrap = 50;
  121.  
  122.         if (isset($_POST['notification']))
  123.         {
  124.             $mail->ConfirmReadingTo = $profile["email"];
  125.         }
  126.         $html_message = $content_type == 'text/HTML' ? true : false;
  127.         $mail->IsHTML($html_message);
  128.         $mail->Subject = smartstrip(trim($mail_subject));
  129.  
  130.         if (isset($_SESSION['url_replacements']))
  131.         {
  132.             while($url_replacement = array_shift($_SESSION['url_replacements']))
  133.             {
  134.                 $_POST['mail_body']=str_replace($url_replacement['url'], "cid:".$url_replacement['id'], $_POST['mail_body']);
  135.             }
  136.             unset($_SESSION['url_replacements']);
  137.         }
  138.  
  139.         // Getting the attachments
  140.         if (isset($_SESSION['attach_array']))
  141.         {
  142.             for ($i=1;$i<=$_SESSION['num_attach'];$i++)
  143.             {
  144.                 // If the temporary file exists, attach it
  145.                 $tmp_file = stripslashes($_SESSION['attach_array'][$i]->tmp_file);
  146.                 if (file_exists($tmp_file))
  147.                 {
  148.                     if ($_SESSION['attach_array'][$i]->disposition == 'attachment' || strpos($_POST['mail_body'], $_SESSION['attach_array'][$i]->content_id))
  149.                     {
  150.                         if ($_SESSION['attach_array'][$i]->disposition == 'attachment')
  151.                         {
  152.                             $mail->AddAttachment($tmp_file, imap_qprint($_SESSION['attach_array'][$i]->file_name), 'base64',  $_SESSION['attach_array'][$i]->file_mime) ;
  153.                         }else
  154.                         {
  155.                             $mail->AddEmbeddedImage($tmp_file, $_SESSION['attach_array'][$i]->content_id, imap_qprint($_SESSION['attach_array'][$i]->file_name), 'base64',  $_SESSION['attach_array'][$i]->file_mime);
  156.                         }
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.  
  162.  
  163.         if ($mailing_group_id > 0)
  164.         {
  165.             $feedback = '';
  166.             $tp->get_contacts_from_mailing_group($mailing_group_id);
  167.             while($tp->next_record())
  168.             {
  169.                 $mail->ClearAllRecipients();
  170.                 //add the body
  171.                 $content = $tp->replace_data_fields($_POST['mail_body'], $tp->f('id'));
  172.                 if ($html_message)
  173.                 {
  174.                     $mail->Body = $html_mail_head.smartstrip($content).$html_mail_foot;
  175.                     $h2t =& new html2text($content);
  176.                     $mail->AltBody  = $h2t->get_text();
  177.                 }else
  178.                 {
  179.                     $mail->Body = smartstrip($content);
  180.                 }
  181.  
  182.                 $mail->AddAddress($tp->f('email'));
  183.  
  184.                 if(!$mail->Send())
  185.                 {
  186.                     $feedback .= $tp->f('email').': '.$mail->ErrorInfo.'<br />';
  187.                 }
  188.             }
  189.  
  190.             $tp->get_companies_from_mailing_group($mailing_group_id);
  191.             while($tp->next_record())
  192.             {
  193.                 $mail->ClearAllRecipients();
  194.  
  195.                 //add the body
  196.                 $content = $tp->replace_company_data_fields($_POST['mail_body'], $tp->f('id'));
  197.                 if ($html_message)
  198.                 {
  199.                     $mail->Body = $html_mail_head.smartstrip($content).$html_mail_foot;
  200.                     $h2t =& new html2text($content);
  201.                     $mail->AltBody  = $h2t->get_text();
  202.                 }else
  203.                 {
  204.                     $mail->Body = smartstrip($content);
  205.                 }
  206.  
  207.                 $mail->AddAddress($tp->f('email'));
  208.  
  209.                 if(!$mail->Send())
  210.                 {
  211.                     $feedback .= $tp->f('email').': '.$mail->ErrorInfo.'<br />';
  212.                 }
  213.             }
  214.  
  215.             if ($feedback != '')
  216.             {
  217.                 $feedback = '<p class="Error">'.$feedback.'</p>';
  218.             }else
  219.             {
  220.                 while($attachment = array_shift($_SESSION['attach_array']))
  221.                 {
  222.                     @unlink($attachment->tmp_file);
  223.                 }
  224.                 // We need to unregister the attachments array and num_attach
  225.                 unset($_SESSION['num_attach']);
  226.                 unset($_SESSION['attach_array']);
  227.  
  228.                 echo "<script type=\"text/javascript\">\r\nwindow.close();\r\n</script>\r\n";
  229.                 exit();
  230.             }
  231.         }else
  232.         {
  233.             $mail_to_array = cut_address(trim($mail_to), $charset);
  234.             $mail_cc_array = cut_address(trim($mail_cc), $charset);
  235.             $mail_bcc_array = cut_address(trim($mail_bcc), $charset);
  236.             while ($to_address = array_shift($mail_to_array))
  237.             {
  238.                 $mail->AddAddress($to_address);
  239.             }
  240.             while ($cc_address = array_shift($mail_cc_array))
  241.             {
  242.                 $mail->AddCC($cc_address);
  243.             }
  244.             while ($bcc_address = array_shift($mail_bcc_array))
  245.             {
  246.                 $mail->AddBCC($bc_address);
  247.             }
  248.  
  249.             if ($html_message)
  250.             {
  251.                 $mail->Body = $html_mail_head.smartstrip($_POST['mail_body']).$html_mail_foot;
  252.                 $h2t =& new html2text($_POST['mail_body']);
  253.                 $mail->AltBody  = $h2t->get_text();
  254.             }else
  255.             {
  256.                 $mail->Body = smartstrip($_POST['mail_body']);
  257.             }
  258.  
  259.             if(!$mime = $mail->Send())
  260.             {
  261.                 $feedback = '<p class="Error">'.$ml_send_error.' '.$mail->ErrorInfo.'</p>';
  262.  
  263.             }else
  264.             {
  265.                 while($attachment = array_shift($_SESSION['attach_array']))
  266.                 {
  267.                     @unlink($attachment->tmp_file);
  268.                 }
  269.                 // We need to unregister the attachments array and num_attach
  270.                 unset($_SESSION['num_attach']);
  271.                 unset($_SESSION['attach_array']);
  272.  
  273.                 if ($profile["type"] == "imap")
  274.                 {
  275.                     $sent_folder = $profile['sent'];
  276.                     if ($sent_folder != '')
  277.                     {
  278.                         require($GO_CONFIG->class_path."imap.class.inc");
  279.                         $imap_stream = new imap();
  280.                         if ($imap_stream->open($profile["host"], "imap", $profile["port"], $profile["username"], $GO_CRYPTO->decrypt($profile["password"]), $sent_folder))
  281.                         {
  282.                             if ($imap_stream->append_message($sent_folder, $mime,"\\Seen"))
  283.                             {
  284.                                 if (isset($_REQUEST['action']) && ($_REQUEST['action']== "reply" || $_REQUEST['action'] == "reply_all"))
  285.                                 {
  286.                                     $uid = array($_REQUEST['uid']);
  287.                                     $imap_stream->set_message_flag($_POST['mailbox'], $uid, "\\Answered");
  288.                                 }
  289.  
  290.                                 $imap_stream->close();
  291.                                 require($GO_THEME->theme_path."header.inc");
  292.                                 echo "<script type=\"text/javascript\">\r\nwindow.close();\r\n</script>\r\n";
  293.                                 require($GO_THEME->theme_path."footer.inc");
  294.                                 exit();
  295.                             }
  296.                         }
  297.                         require($GO_THEME->theme_path."header.inc");
  298.                         echo "<script type=\"text/javascript\">\r\nalert('".$ml_sent_items_fail."');\r\nwindow.close();\r\n</script>\r\n";
  299.                         require($GO_THEME->theme_path.'footer.inc');
  300.                         exit();
  301.                     }else
  302.                     {
  303.                         require($GO_THEME->theme_path."header.inc");
  304.                         echo "<script type=\"text/javascript\">\r\nwindow.close();\r\n</script>\r\n";
  305.                         require($GO_THEME->theme_path.'footer.inc');
  306.                         exit();
  307.                     }
  308.                 }else
  309.                 {
  310.                     require($GO_THEME->theme_path.'header.inc');
  311.                     echo "<script type=\"text/javascript\">\r\nwindow.close();\r\n</script>\r\n";
  312.                     require($GO_THEME->theme_path.'footer.inc');
  313.                     exit();
  314.                 }
  315.  
  316.             }
  317.         }
  318.  
  319.     break;
  320.  
  321.     case 'delete':
  322.         // Rebuilding the attachments array with only the files the user wants to keep
  323.         $tmp_array = array();
  324.         for ($i=$j=1;$i<=$_SESSION['num_attach'];$i++)
  325.         {
  326.             $thefile = 'file'.$i;
  327.             if (empty($_POST[$thefile]))
  328.             {
  329.                 $tmp_array[$j]->file_name = $_SESSION['attach_array'][$i]->file_name;
  330.                 $tmp_array[$j]->tmp_file = $_SESSION['attach_array'][$i]->tmp_file;
  331.                 $tmp_array[$j]->file_size = $_SESSION['attach_array'][$i]->file_size;
  332.                 $tmp_array[$j]->file_mime = $_SESSION['attach_array'][$i]->file_mime;
  333.                 $tmp_array[$j]->content_id = $_SESSION['attach_array'][$i]->content_id;
  334.                 $tmp_array[$j]->disposition = $_SESSION['attach_array'][$i]->disposition;
  335.                 $j++;
  336.             }else
  337.             {
  338.                 @unlink($_SESSION['attach_array'][$i]->tmp_file);
  339.             }
  340.         }
  341.  
  342.         // Removing the attachments array from the current session
  343.         unset($_SESSION['num_attach']);
  344.         unset($_SESSION['attach_array']);
  345.         $_SESSION['attach_array'] = $tmp_array;
  346.         $_SESSION['num_attach'] = ($j > 1 ? $j - 1 : 0);
  347.         break;
  348.  
  349.     case 'change_format':
  350.         SetCookie("GO_MAIL_FORMAT",$content_type,time()+3600*24*30,"/",'',0);
  351.     break;
  352. }
  353.  
  354. //check for the templates plugin
  355. //if a template id is given then process it
  356. $template_id = isset($_REQUEST['template_id']) ? $_REQUEST['template_id'] : 0;
  357. $contact_id = isset($_REQUEST['contact_id']) ? $_REQUEST['contact_id'] : 0;
  358.  
  359.  
  360. if($mailing_group_id > 0 && $tp->get_contacts_from_mailing_group($mailing_group_id) == 0 && $tp->get_companies_from_mailing_group($mailing_group_id) == 0)
  361. {
  362.     require($GO_THEME->theme_path."header.inc");
  363.     $tabtable = new tabtable('templates_tab', $ml_attention, '600', '120');
  364.     $tabtable->print_head();
  365.     echo '<p>'.$ml_no_contacts_in_mailing_group.'</p><br />';
  366.     $button = new button($cmdClose, "javascript:window.close();");
  367.     $tabtable->print_foot();
  368.     require($GO_THEME->theme_path."footer.inc");
  369.     exit();
  370. }
  371.  
  372. if ($tp_plugin)
  373. {
  374.     $template_count = $tp->get_subscribed_templates($GO_SECURITY->user_id, EMAIL_TEMPLATE);
  375. }
  376.  
  377. if ($_SERVER['REQUEST_METHOD'] != "POST" && $tp_plugin && $template_id == 0 && $template_count > 0)
  378. {
  379.     require($GO_THEME->theme_path."header.inc");
  380.     echo '<form name="sendform" method="post" action="'.$_SERVER['PHP_SELF'].'">';
  381.     if($uid > 0)
  382.     {
  383.         echo '<input type="hidden" name="account_id" value="'.$_REQUEST['account_id'].'" />';
  384.         echo '<input type="hidden" name="uid" value="'.$uid.'" />';
  385.         echo '<input type="hidden" name="mailbox" value="'.$_REQUEST['mailbox'].'" />';
  386.         echo '<input type="hidden" name="action" value="'.$_REQUEST['action'].'" />';
  387.     }
  388.     echo '<input type="hidden" name="mail_subject" value="'.$mail_subject.'" />';
  389.     echo '<input type="hidden" name="mail_body" value="'.stripslashes($mail_body).'" />';
  390.     echo '<input type="hidden" name="mail_to" value="'.$mail_to.'" />';
  391.     echo '<input type="hidden" name="mail_cc" value="'.$mail_cc.'" />';
  392.     echo '<input type="hidden" name="mail_bcc" value="'.$mail_bcc.'" />';
  393.     echo '<input type="hidden" name="mail_from" value="'.$mail_from.'" />';
  394.     echo '<input type="hidden" name="contact_id" value="'.$contact_id.'" />';
  395.     echo '<input type="hidden" name="template_id" />';
  396.     echo '<input type="hidden" name="mailing_group_id" value="'.$mailing_group_id.'" />';
  397.     echo '<input type="hidden" name="sendaction" value="load_template" />';
  398.  
  399.     //get the addressbook language file
  400.     require($GO_LANGUAGE->get_language_file('contacts'));
  401.     $tabtable = new tabtable('templates_tab', $ab_templates, '600', '400');
  402.     $tabtable->print_head();
  403.  
  404.     echo '<table border="0" cellpadding="10" cellspacing="0"><tr><td>';
  405.     echo $ab_select_template;
  406.     echo '<table border="0" cellpadding="2">';
  407.  
  408.     echo '<tr><td><a class="normal" href="javascript:document.forms[0].template_id.value=\'0\';document.forms[0].submit();">'.$ab_no_template.'</a></td></tr>';
  409.  
  410.     while($tp->next_record())
  411.     {
  412.         echo '<tr><td><a class="normal" href="javascript:document.forms[0].template_id.value=\''.$tp->f('id').'\';document.forms[0].submit();">'.$tp->f('name').'</a></td></tr>';
  413.     }
  414.  
  415.     echo '</table></td></tr></table>';
  416.     echo '<br />';
  417.     $button = new button($cmdClose, "javascript:window.close()");
  418.     $tabtable->print_foot();
  419.     echo '</form>';
  420. }else
  421. {
  422.     if ($content_type=='text/HTML')
  423.     {
  424.         //create htmlarea
  425.         $htmlarea = new htmlarea();
  426.         if ($fs_module = $GO_MODULES->get_module('filesystem'))
  427.         {
  428.             if ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $fs_module['acl_read']) || $GO_SECURITY->has_permission($GO_SECURITY->user_id, $fs_module['acl_write']))
  429.             {
  430.                 $htmlarea->add_button('go_image', $editorCmd['image'], $GO_CONFIG->control_url.'/htmlarea/images/ed_image.gif', 'false', "function insertGOimage()
  431.                 {
  432.                     popup('select_image.php','600','400');
  433.                 }");
  434.  
  435.             }
  436.         }
  437.         $page_style = '';//'body { background-color: #ffffff; font-family: verdana,sans-serif; } p { margin: 0px; }';
  438.         $GO_HEADER['head'] = $htmlarea->get_header('mail_body', -40, -250, 25, $page_style);
  439.         $GO_HEADER['body_arguments'] = 'onload="initEditor()"';
  440.     }
  441.  
  442.     require($GO_THEME->theme_path."header.inc");
  443.     require("compose.inc");
  444. }
  445. require($GO_THEME->theme_path."footer.inc");
  446. ?>
  447.