function checkmail($smarty, $module_name, $local_templates_dir, $arrConf, $pImap) { $jsonObject = new PaloSantoJSON(); $flag = false; //obtenemos la lista de mails y vemos si ahi algun cambio //obtenemos el mailbox que deseamos leer $mailbox = getParameter('folder'); $action = getParameter('action'); session_commit(); ini_set("session.use_cookies", "0"); //creamos la connección al mailbox $pImap->setMailbox($mailbox); $result = $pImap->login($_SESSION['elastix_user'], $_SESSION['elastix_pass2']); if ($result == false) { $jsonObject->set_status("ERROR"); $jsonObject->set_error($pImap->errMsg); $flag = true; } else { $result = $pImap->getRecentMessage(); if ($result === false) { $jsonObject->set_status("ERROR"); $jsonObject->set_error($pImap->errMsg); $flag = true; } else { if ($result == 0) { $jsonObject->set_status("NOCHANGED"); } else { $jsonObject->set_status("CHANGED"); $flag = true; } } } cleanAlertsImap(); $pImap->close_mail_connection(); return array('there_was_change' => $flag, 'data' => $jsonObject->createJSON()); }
function sendEmail($headers, $subject, $body, $draft = false) { //check de headers //to try { $this->phpMailer = new PHPMailerMime(true); //Elastix usa codificacion UTF8 $this->phpMailer->CharSet = 'UTF-8'; if (!isset($this->username)) { $this->errMsg = _tr("Invalid Sender"); return false; } if (!isset($headers['to'])) { $this->errMsg = _tr("Invalid Field 'TO'"); return false; } //TO: creamos una nueva instacia de paloListEmailAddress con los emails //que se encuentran en el campo 'TO' $to = new paloListEmailAddress(explode(",", $headers['to'])); //ninguna de las direcciones dadas es válida, no podemos continuar if ($to->getNumAddress() == 0) { $this->errMsg = _tr("Invalid Field 'TO'"); return false; } foreach ($to->getListAddress() as $address) { $this->phpMailer->AddAddress($address->getEmail(), $address->getName()); } //CC: if (isset($headers['cc'])) { $cc = new paloListEmailAddress(explode(",", $headers['cc'])); //ninguna de las direcciones dadas es válida, no podemos continuar foreach ($cc->getListAddress() as $address) { $this->phpMailer->AddCC($address->getEmail(), $address->getName()); } } //BCC: if (isset($headers['bcc'])) { $bcc = new paloListEmailAddress(explode(",", $headers['bcc'])); //ninguna de las direcciones dadas es válida, no podemos continuar foreach ($cc->getListAddress() as $address) { $this->phpMailer->AddBCC($address->getEmail(), $address->getName()); } } //REPLY_TO if (isset($headers['reply_to'])) { $replayTo = new paloListEmailAddress(explode(",", $headers['reply_to'])); //ninguna de las direcciones dadas es válida, no podemos continuar foreach ($replayTo->getListAddress() as $address) { $this->phpMailer->AddReplyTo($address->getEmail(), $address->getName()); } } //FROM $this->phpMailer->setFrom($this->username, $this->name); //SUBJECT $this->phpMailer->Subject = is_string($subject) ? $subject : ''; //esta funcion ademas de enviar el mensaje como html //también setea la priedad altBody que corresponde a la parte de mensaje en texto plano $this->phpMailer->paloMsgHTML($body, '/var/www/html', $this->attachments); $uploadedFiles = array(); //attachments if (is_array($this->attachments)) { foreach ($this->attachments as $key => $attach) { if ($attach['type'] == 'file') { if (is_string($attach['filename']) && $attach['filename'] != '') { $filename = basename($attach['filename']); $name = $filename; if (isset($attach['name'])) { if (is_string($attach['name']) && $attach['name'] != '') { $name = $attach['name']; } } if (is_file(PATH_UPLOAD_ATTACHS . "/{$filename}")) { $uploadedFiles[] = PATH_UPLOAD_ATTACHS . "/{$filename}"; $this->phpMailer->AddAttachment(PATH_UPLOAD_ATTACHS . "/{$filename}", $name, 'base64', $attach['mime']); } } } } } //si draft==true significa que se debe guardar una copia del archivo //en borradores y no debe ser enviado if ($draft) { cleanAlertsImap(); $pImap->close_mail_connection(); return true; } $this->phpMailer->Send(); //eliminamos los archivos subidos para los attachments foreach ($this->phpMailer->GetAttachments() as $attachment) { //if $attachment[5]=false significa que son archivos, // tambien ahi otros tipo de attachments que son strings que no se deben elminiar if ($attachment[5] == false) { if (in_array($attachment[0], $uploadedFiles)) { if (is_file($attachment[0])) { unlink($attachment[0]); } } } } //guardamos una copia del archivo enviado dentro de la carpeta Sent //1.- comprobamos que la carpeta Sent exista y sí no existe la creamos //2.- procedemos a crear la coneccion a dicha carpeta //3.- procedemo a agregar el archivo a la carpeta if ($this->paloImap->login($this->username, $this->pass)) { $sentFolder = 'Sent'; $arrFolders = $this->paloImap->getMailboxList($sentFolder); if (isset($arrFolders[$sentFolder])) { if (!$this->paloImap->createMailbox($sentFolder)) { $this->errMsg = $this->paloImap->getErrorMsg(); } } $this->paloImap->setMailbox('Sent'); if (!$this->paloImap->appendMessage($sentFolder, $this->phpMailer->getMiMeStringToAppendImap(), "\\Seen")) { $this->errMsg = $this->paloImap->getErrorMsg(); } } else { $this->errMsg = $this->paloImap->getErrorMsg(); } cleanAlertsImap(); $this->paloImap->close_mail_connection(); return true; } catch (phpmailerException $e) { $this->errMsg = $e->errorMessage(); return false; } catch (Exception $e) { $this->errMsg = $e->getMessage(); return false; } }