<?php
if (!defined('sugarEntry') || !sugarEntry)
    die('Not A Valid Entry Point');
if(!isset($_REQUEST['uid']))
    die('Nie wybrano kontrahentów');

$accounts=explode(",",$_REQUEST['uid']);
$account_list=array();
$emails;
$error=0;

foreach ($accounts as $account){
    $ac= new Account();
    $ac->retrieve($account);
    if($ac->id=='')continue;
    $addTmp=array();
    $addTmp['name']=$ac->name;
    
    $sea = new SugarEmailAddress();
    $addresses = $sea->getAddressesByGUID($ac->id, 'Accounts');
    foreach ($addresses as $address) {

        if ($address['email_address'] != '' && $address['opt_out'] == 1) {
            $emails[]=$address['email_address'];
            $addTmp['emails'][]= $address['email_address'];
        }
    }
    $account_list[]=$addTmp;
}
$success='';
if(isset($_POST['submit'])){
    if($_POST['body']!='' && $_POST['title']!='' && $_POST['uid']!=''){
        require_once 'include/ECM/EcmSendPdfButton/EcmSendPdfButton.inc';
        $t = new EcmSendPdfButton('Users', $usr_id,$itemail,$type);
        foreach ($emails as $email){
            $t->setBcc($email);
        }
        $t->setSubject($_REQUEST['title']);
        $t->setBody($_REQUEST['body']);
        $success = 0;
        $success= $t->sendEmail();
        
        
    } else {
        $error=1;
    }
}
$ss = new Sugar_Smarty();
$ss->assign('uid',$_REQUEST['uid']);
$ss->assign('body',$_REQUEST['body']);
$ss->assign('error',$error);
$ss->assign('success',$success);
$ss->assign('title',$_REQUEST['title']);
$ss->assign('account_list',$account_list);
$content = $ss->fetch('modules/Accounts/tpls/SendEmail.tpl');


echo $content;
?>