It belongs to the Workflow Designer and must not be distributed without complete extension **/ require_once(realpath(dirname(__FILE__).'/../autoload_wf.php')); require_once('modules/Emails/mail.php'); /* vt6 ready 2014/04/09 */ class WfTaskSendmail extends \Workflow\Task { protected $_envSettings = array("sendmail_result", 'email_crm_id'); private $_mailRecord = null; public function init() { $this->addPreset("Attachments", "attachments", array( 'module' => $this->getModuleName() )); } public function beforeGetTaskform($viewer) { global $adb; $connected = $this->getConnectedObjects("Absender"); if(count($connected) > 0) { $viewer->assign("from", array( "from_mail" => $connected[0]->get("email1"), "from_name" => trim($connected[0]->get("first_name")." ".$connected[0]->get("last_name")), "from_readonly" => true, )); } $smtpServer = \Workflow\ConnectionProvider::getAvailableConfigurations('smtp'); $viewer->assign('SMTPSERVER', $smtpServer); $connected = $this->getConnectedObjects("BCC"); $bccs = $connected->get("email1"); $viewer->assign("bccs", $bccs); $from_email = $this->get("from_mail"); if($from_email === -1) { global $current_user; $from_email = $current_user->column_fields["email1"]; $from_name = $current_user->column_fields["first_name"]." ".$current_user->column_fields["last_name"]; $this->set("from_mail", $from_email); $this->set("from_name", $from_name); } if($this->notEmpty('mailtemplate')) { $mailtemplate = $this->get("mailtemplate"); if(is_numeric($mailtemplate)) { $this->set('mailtemplate', 's#V2#\\Workflow\\Plugins\\EmailTemplate\\Core-'.$mailtemplate); } else { $parts = explode('#', $mailtemplate); switch($parts[1]) { case 'emailmaker': $this->set('mailtemplate', 's#V2#\\Workflow\\Plugins\\EmailTemplate\\Emailmaker-'.$parts[2]); break; } } } /* if(vtlib_isModuleActive('Emails')) { $availableAttachments = \Workflow\InterfaceFiles::getAvailableFiles($this->getModuleName()); } else { throw new \Exception('To use this task, you need to activate the "Emails" module.'); } $jsList = array(); foreach($availableAttachments as $title => $group) { foreach($group as $index => $value) { $jsList[$index] = $value; } } $viewer->assign("jsAttachmentsList", $jsList); $viewer->assign("available_attachments", $availableAttachments); if($this->get("attachments") == -1) { $this->set("attachments", '{}'); } if($this->get("attachments") == "") { $this->set("attachments", '{}'); } */ $attachments = $this->get("attachments"); if($this->notEmpty('provider')) { /** * @var $smtpServer \Workflow\Plugins\ConnectionProvider\SMTP */ $smtpServer = \Workflow\ConnectionProvider::getConnection($this->get('provider')); if(empty($smtpServer)) { $query = "select from_email_field from vtiger_systems where server_type=?"; $params = array('email'); $result = $adb->pquery($query,$params); $from_email_field = $adb->query_result($result,0,'from_email_field'); if(!empty($from_email_field)) { $viewer->assign('show_emailfrom_checkbox', true); } else { $viewer->assign('show_emailfrom_checkbox', false); } } else { $senderMail = $smtpServer->get('sender_mail'); if (!empty($senderMail)) { $viewer->assign('show_emailfrom_checkbox', true); } else { $viewer->assign('show_emailfrom_checkbox', false); } } } else { $query = "select from_email_field from vtiger_systems where server_type=?"; $params = array('email'); $result = $adb->pquery($query,$params); $from_email_field = $adb->query_result($result,0,'from_email_field'); if(!empty($from_email_field)) { $viewer->assign('show_emailfrom_checkbox', true); } else { $viewer->assign('show_emailfrom_checkbox', false); } } if(strpos($attachments, '"pdfmaker') !== false) { $attachments = json_decode($this->get("attachments"), true); if(is_array($attachments) && count($attachments) > 0) { \Vtiger_Loader::autoLoad('PDFMaker_PDFMaker_Model'); $PDFMaker = new \PDFMaker_PDFMaker_Model(); if (method_exists($PDFMaker, "GetAvailableTemplates")) { $templates = $PDFMaker->GetAvailableTemplates($this->getModuleName()); foreach ($templates as $index => $value) { $pdfmaker[$index] = 'PDFMaker - ' . $value["templatename"]; } foreach($attachments as $id => $attachment) { //{"s#external#pdfmaker#1":["PDFMaker - Invoice","",{"val":"pdfmaker#1"}] $parts = explode('#', $id); if($parts[0] == 'pdfmaker') { unset($attachments[$id]); $attachments['s#external#pdfmaker#'.$parts[1]] = array($pdfmaker[$parts[1]], "", array("val" => "pdfmaker#".$parts[1])); } } $this->set('attachments', json_encode($attachments)); } } } $obj = new \Workflow\Emailtemplates(); $mailtemplates = $obj->getAllTemplates($this->getModuleName()); /*$sql = "SELECT * FROM vtiger_emailtemplates WHERE deleted = 0"; $result = $adb->query($sql); $mailtemplates = array(); while($row = $adb->fetchByAssoc($result)) { $mailtemplates['Email Templates'][$row["templateid"]] = $row["templatename"]; }*/ /* if(vtlib_isModuleActive('EMAILMaker') && class_exists('EMAILMaker_Module_Model')) { } */ /* if(vtlib_isModuleActive('SWBeeFree') && class_exists('SWBeeFree_Module_Model')) { $beefree = new \SWBeeFree_Module_Model(); $templates = $beefree->getTemplatesForModule($this->getModuleName()); foreach($templates as $template) { $mailtemplates['BeeFree']['s#beefree#'.$template['id']] = $template['name']; } } */ $viewer->assign("MAIL_TEMPLATES", $mailtemplates); $viewer->assign("fields", \VtUtils::getFieldsWithBlocksForModule($this->getModuleName(), true)); if(defined("WF_DEMO_MODE") && constant("WF_DEMO_MODE") == true) { echo "
The sendmail Task won't work on demo.stefanwarnat.de
"; } } /** * @param $context \Workflow\VTEntity * @return mixed */ public function handleTask(&$context) { global $adb, $current_user; global $current_language; if(defined("WF_DEMO_MODE") && constant("WF_DEMO_MODE") == true) { return "yes"; } /* if(!class_exists("Workflow_PHPMailer")) { require_once("modules/Workflow2/phpmailer/class.phpmailer.php"); }*/ #$result = $adb->query("select user_name, email1, email2 from vtiger_users where id=1"); #$from_email = "swarnat@praktika.de"; #$from_name = "Stefan Warnat"; $module = $context->getModuleName(); if($this->notEmpty('interactivemail')) { $blockReqKey = '_sendmail_' . $this->getBlockId(); // var_dump($context); if ($this->getWorkflow()->hasRequestValues($blockReqKey)) { $data = $context->getEnvironment($blockReqKey); // $this->getWorkflow()-> if(!empty($data['emailid'])) { $record = \Vtiger_Record_Model::getInstanceById($data['emailid']); $record->delete(); } if(strpos($data['recepient'], ';#;') !== false) { $data['recepient'] = explode(';#;', $data['recepient']); } else { $data['recepient'] = array($data['recepient']); } $this->set('from_mail', $data['from_mail']); $this->set('from_name', $data['from_name']); $this->set('recepient', implode(',', $data['recepient'])); $this->set('subject', $data['subject']); $this->set('content', $data['content']); } } $recepient = $this->get('recepient', $context); if(strpos($recepient, ';#;') !== false) { $recepient = str_replace(';#;', ',', $recepient); } if(strpos($recepient, ',') !== false) { $recepients = explode(',', $recepient); } else { $recepients = array($recepient); } if($this->notEmpty('storeid')) { $storeid = explode(',', $this->get('storeid')); } else { $storeid = array(); } $recepient = array(); foreach($recepients as $receiver) { if(substr($receiver, 0, 5) == 'crm##') { $recieverParts = explode('##', $receiver); $recepient[] = $recieverParts[2]; $storeid[] = $recieverParts[1]; } elseif(substr($receiver, 0, 5) == 'raw##') { $recieverParts = explode('##', $receiver); $recepient[] = $recieverParts[2]; } else { $recepient[] = $receiver; } } // var_dump($recepient); if(!empty($storeid)) { $this->set('storeid', '$crmid,'.implode(',',$storeid)); } $this->set('recepient', implode(',', $recepient)); if(!$this->notEmpty('recepient')) { return 'yes'; } $et = new \Workflow\VTTemplate($context); $to_email = $et->render(trim($this->get("recepient")),","); # $connected = $this->getConnectedObjects("Absender"); if(count($connected) > 0) { $from_name = trim($connected[0]->get("first_name")." ".$connected[0]->get("last_name")); $from_email = $connected[0]->get("email1"); } else { $from_name = $et->render(trim($this->get("from_name")),","); # $from_email = $et->render(trim($this->get("from_mail")),","); # } $cc = $et->render(trim($this->get("emailcc")),","); # $bcc = $et->render(trim($this->get("emailbcc")),","); # /* if($this->get('use_mailserver_from') == '1') { $query = "select from_email_field from vtiger_systems where server_type=?"; $params = array('email'); $result = $adb->pquery($query, $params); $from_email_field = $adb->query_result($result, 0, 'from_email_field'); if (!empty($from_email_field)) { $from_email = $from_email_field; } } */ /** * Connected BCC Objects * @var $connected */ $connected = $this->getConnectedObjects("BCC"); $bccs = $connected->get("email1"); if(count($bccs) > 0) { $bcc = array($bcc); foreach($bccs as $bccTMP) { $bcc[] = $bccTMP; } $bcc = trim(implode(",",$bcc),","); } if(strlen(trim($to_email, " \t\n,")) == 0 && strlen(trim($cc, " \t\n,")) == 0 && strlen(trim($bcc, " \t\n,")) == 0) { return "yes"; } $storeid = trim($this->get("storeid", $context)); if(empty($storeid) || $storeid == -1 || (!is_numeric($storeid) && strpos($storeid, ',') === false)) { $storeid = $context->getId(); } $embeddedImages = null; $unlinkAfterSending = array(); $content = $this->get("content"); $subject = $this->get("subject"); $content = preg_replace( '/[\x{200B}-\x{200D}]/u', '', $content ); $subject = preg_replace( '/[\x{200B}-\x{200D}]/u', '', $subject ); #$subject = utf8_decode($subject); #$content = utf8_encode($content); $content = html_entity_decode(str_replace(" ", " ", $content), ENT_QUOTES, "UTF-8"); #$subject = html_entity_decode(str_replace(" ", " ", $subject), ENT_QUOTES, "UTF-8"); $mailtemplate = $this->get("mailtemplate"); if(!empty($mailtemplate) && $mailtemplate != -1) { if(is_numeric($mailtemplate)) { $obj = new \Workflow\Emailtemplates(); $mailtemplate = $obj->getTemplate('\\Workflow\\Plugins\\EmailTemplate\\Core-'.$mailtemplate, $context); $content = str_replace('$mailtext', $content, $mailtemplate["content"]); if(empty($subject)) { $subject = $mailtemplate["subject"]; } if(!empty($mailtemplate['images'])) { $embeddedImages = $mailtemplate['images']; } } else { $parts = explode('#', $mailtemplate); switch($parts[1]) { case 'emailmaker': $obj = new \Workflow\Emailtemplates(); $mailtemplate = $obj->getTemplate('\\Workflow\\Plugins\\EmailTemplate\\Emailmaker-'.$parts[2], $context); $content = str_replace('$mailtext', $content, $mailtemplate["content"]); if(empty($subject)) { $subject = $mailtemplate["subject"]; } if(!empty($mailtemplate['images'])) { $embeddedImages = $mailtemplate['images']; } break; case 'beefree': $obj = new \Workflow\Emailtemplates(); $mailtemplate = $obj->getTemplate('\\Workflow\\Plugins\\EmailTemplate\\Beefree-'.$parts[2], $context); $content = str_replace('$mailtext', $content, $mailtemplate["content"]); if(!empty($mailtemplate['images'])) { $embeddedImages = $mailtemplate['images']; } break; case 'V2': $obj = new \Workflow\Emailtemplates(); $mailtemplate = $obj->getTemplate($parts[2], $context); $content = str_replace('$mailtext', $content, $mailtemplate["content"]); if(empty($subject)) { $subject = $mailtemplate["subject"]; } if(!empty($mailtemplate['images'])) { $embeddedImages = $mailtemplate['images']; } break; } } } $subject = $et->render(trim($subject)); $content = $et->render(trim($content)); #$content = htmlentities($content, ENT_NOQUOTES, "UTF-8"); if(DEMO_MODE == false) { if ($this->notEmpty('provider')) { /** * @var $smtpServer \Workflow\Plugins\ConnectionProvider\SMTP */ $smtpServer = \Workflow\ConnectionProvider::getConnection($this->get('provider')); $mail = $smtpServer->getPHPMailer(); } else { $mail = \Workflow\Plugins\ConnectionProvider\SMTP::getDefaultMailer(); } if($this->get('use_mailserver_from') == '1') { $from_email = $mail->From; } } if(getTabid('Emails') && vtlib_isModuleActive('Emails')) { $storeIdParts = array(); if(strpos($storeid, ',') !== false) { $storeIdParts = explode(',', $storeid); $storeid = array_shift($storeIdParts); } require_once('modules/Emails/Emails.php'); $focus = new Emails(); $focus->column_fields["assigned_user_id"] = \Workflow\VTEntity::getUser()->id; $focus->column_fields["activitytype"] = "Emails"; $focus->column_fields["date_start"] = date("Y-m-d"); $focus->column_fields["time_start"] = date("H:i:s"); $focus->column_fields["parent_id"] = $storeid; $focus->column_fields["email_flag"] = "SAVED"; $focus->column_fields["subject"] = $subject; $focus->column_fields["description"] = $content; $focus->column_fields["from_email"] = $from_email; $focus->column_fields["saved_toid"] = '["'.str_replace(',','","',trim($to_email,",")).'"]'; $focus->column_fields["ccmail"] = $cc; $focus->column_fields["bccmail"] = $bcc; $focus->save("Emails"); $this->_mailRecord = $focus; #error_log("eMail:".$emailID); $emailID = $focus->id; if(!empty($storeIdParts)) { foreach($storeIdParts as $id) { $sql = 'INSERT INTO vtiger_seactivityrel SET crmid = ?, activityid = ?'; $adb->pquery($sql, array($id, $emailID)); } } } else { $emailID = ""; } $attachments = json_decode($this->get("attachments"), true); if(is_array($attachments) && count($attachments) > 0) { // Module greifen auf Datenbank zurück. Daher vorher speichern! $context->save(); //$this->addStat($attachments); foreach($attachments as $key => $value) { if($value == false) { continue; } if(is_string($value)) { $value = array($value, false, array()); } // legacy check if(strpos($key, 'document#') === 0) { $key = 's#'.$key; } if(strpos($key, 's#') === 0) { $tmpParts = explode('#', $key, 2); $specialAttachments = \Workflow\Attachment::getAttachments($tmpParts[1], $value, $context, \Workflow\Attachment::MODE_NOT_ADD_NEW_ATTACHMENTS); //$this->addStat('Add Attachment 1'); //$this->addStat($specialAttachments); foreach($specialAttachments as $attachment) { $this->addStat($attachment[2]); $this->addStat(filesize($attachment[1]).' Bytes'); if($attachment[0] === 'ID') { $this->attachByAttachmentId($attachment[1]); } elseif($attachment[0] === 'PATH') { $this->attachFile($attachment[1], $attachment[2], $attachment[3]); } } } else { $file = \Workflow\InterfaceFiles::getFile($key, $this->getModuleName(), $context->getId()); //$this->addStat('Add Attachment 2'); //$this->addStat($file['path']); //$this->addStat(filesize($file)); $this->attachFile($file['path'], $value[1] != false ? $value[1] : $file['name'], $file['type']); } } } if($_SERVER['REMOTE_ADDR'] == '87.134.26.231') { ini_set('display_errors', 1); error_reporting(-1); } if($embeddedImages === null && $this->get('attachImages') == '1') { if(!function_exists('str_get_html')) { require_once('include/simplehtmldom/simple_html_dom.php'); } $html = str_get_html($content); if(count($html->find('img')) > 0) { $tmppath = realpath(vglobal('root_directory') . '/modules/Workflow2/tmp'); $embeddedImages = array(); $done = array(); $index = 50; foreach ($html->find('img') as $img) { $src = $img->src; if(isset($done[$src])) { $newCID = $done[$src]; } else { $done[$src] = 'image'.$index; $parts = explode('.', $src); $fileSuffix = end($parts); $tmpFile = tempnam($tmppath,'Embedd'); $unlinkAfterSending[] = $tmpFile; try { $content = \Workflow\VtUtils::getContentFromUrl($src); } catch (\Exception $exp) { $this->addStat('[SKIP] Error during download Image '.$src.': Code '.$exp->getCode()); $this->addStat(substr(htmlentities($exp->getMessage()), 0, 200)); continue; }; if(empty($content)) { continue; } file_put_contents($tmpFile, $content); $embeddedImages['image'.$index] = array( "name" => 'image'.$index.'.'.$fileSuffix, "path" => $tmpFile ); $newCID = 'image'.$index; $index++; } $img->src = 'cid:'.$newCID; } } $content = $html->save(); } $receiver = explode(",", $to_email); foreach($receiver as $to_email) { $to_email = trim($to_email); if(empty($to_email)) continue; $mail->ClearAddresses(); if(DEMO_MODE == false) { if(is_array($embeddedImages)) { foreach ($embeddedImages AS $cid => $cdata) { $mail->AddEmbeddedImage($cdata["path"], $cid, $cdata["name"]); } } $to_email = trim($to_email,","); $mail->FromName = $from_name; if($this->get('use_mailserver_from') != '1') { $mail->From = $from_email; } $this->addStat("From: ".$from_name." <".$from_email.">"); if($this->get('trackAccess') == '1') { //Including email tracking details global $site_URL, $application_unique_key; $counterUrl = $site_URL.'/modules/Emails/actions/TrackAccess.php?parentId='.$storeid.'&record='.$focus->id.'&applicationKey='.$application_unique_key; $counterHeight = 1; $counterWidth = 1; if(defined('TRACKING_IMG_HEIGHT')) { $counterHeight = TRACKING_IMG_HEIGHT; } if(defined('TRACKING_IMG_WIDTH')) { $counterWidth = TRACKING_IMG_WIDTH; } $content = $content."