fields["itemtype"]."-".
$this->fields["items_id"].">");
$mmail->SetFrom($this->fields['sender'], $this->fields['sendername']);
if ($this->fields['replyto']) {
$mmail->AddReplyTo($this->fields['replyto'], $this->fields['replytoname']);
}
$mmail->Subject = $this->fields['name'];
if (empty($this->fields['body_html'])) {
$mmail->isHTML(false);
$mmail->Body = $this->fields['body_text'];
} else {
$mmail->isHTML(true);
$mmail->Body = '';
$this->fields['body_html'] = Html::entity_decode_deep($this->fields['body_html']);
$documents = importArrayFromDB($this->fields['documents']);
if (is_array($documents) && count($documents)) {
$doc = new Document();
foreach ($documents as $docID) {
$doc->getFromDB($docID);
// Add embeded image if tag present in ticket content
if (preg_match_all('/'.Document::getImageTag($doc->fields['tag']).'/',
$this->fields['body_html'], $matches, PREG_PATTERN_ORDER)) {
$mmail->AddEmbeddedImage(GLPI_DOC_DIR."/".$doc->fields['filepath'],
Document::getImageTag($doc->fields['tag']),
$doc->fields['filename'],
'base64',
$doc->fields['mime']);
}
}
}
$mmail->Body .= $this->fields['body_html'];
$mmail->AltBody = $this->fields['body_text'];
}
$mmail->AddAddress($this->fields['recipient'], $this->fields['recipientname']);
if (!empty($this->fields['messageid'])) {
$mmail->MessageID = "<".$this->fields['messageid'].">";
}
$messageerror = __('Error in sending the email');
if (!$mmail->Send()) {
Session::addMessageAfterRedirect($messageerror."
".$mmail->ErrorInfo, true);
//TODO: add translations string
Toolbox::logInFile(
'mail-error',
sprintf(
'%1$s: %2$s',
sprintf(
'Email not sent to %s',
$this->fields['recipient']
),
$this->fields['name']."\n"
)
);
$mmail->ClearAddresses();
$this->update(array('id' => $this->fields['id'],
'sent_try' => $this->fields['sent_try']+1));
return false;
} else {
//TRANS to be written in logs %1$s is the to email / %2$s is the subject of the mail
Toolbox::logInFile("mail",
sprintf(__('%1$s: %2$s'),
sprintf(__('An email was sent to %s'),
$this->fields['recipient']),
$this->fields['name']."\n"));
$mmail->ClearAddresses();
$this->update(array('id' => $this->fields['id'],
'sent_time' => $_SESSION['glpi_currenttime']));
$this->delete(array('id' => $this->fields['id']));
return true;
}
} else {
return false;
}
}
/**
* Give cron information
*
* @param $name : task's name
*
* @return arrray of information
**/
static function cronInfo($name) {
switch ($name) {
case 'queuedmail' :
return array('description' => __('Send mails in queue'),
'parameter' => __('Maximum emails to send at once'));
case 'queuedmailclean' :
return array('description' => __('Clean mail queue'),
'parameter' => __('Days to keep sent emails'));
}
return array();
}
/**
* Cron action on queued mails : send mails in queue
*
* @param $task for log, if NULL display (default NULL)
**/
static function cronQueuedMail($task=NULL) {
global $DB, $CFG_GLPI;
if (!$CFG_GLPI["use_mailing"]) {
return 0;
}
$cron_status = 0;
// Send mail at least 1 minute after adding in queue to be sure that process on it is finished
$send_time = date("Y-m-d H:i:s", strtotime("+1 minutes"));
$query = "SELECT `glpi_queuedmails`.*
FROM `glpi_queuedmails`
WHERE NOT `glpi_queuedmails`.`is_deleted`
AND `glpi_queuedmails`.`send_time` < '".$send_time."'
ORDER BY `glpi_queuedmails`.`send_time` ASC
LIMIT 0, ".$task->fields['param'];
$mail = new self();
foreach ($DB->request($query) as $data) {
if ($mail->sendMailById($data['id'])) {
$cron_status = 1;
if (!is_null($task)) {
$task->addVolume(1);
}
}
}
return $cron_status;
}
/**
* Cron action on queued mails : clean mail queue
*
* @param $task for log, if NULL display (default NULL)
**/
static function cronQueuedMailClean($task=NULL) {
global $DB;
$vol = 0;
// Expire mails in queue
if ($task->fields['param'] > 0) {
$secs = $task->fields['param'] * DAY_TIMESTAMP;
$send_time = date("U") - $secs;
$query_exp = "DELETE
FROM `glpi_queuedmails`
WHERE `glpi_queuedmails`.`is_deleted`
AND UNIX_TIMESTAMP(send_time) < '".$send_time."'";
$DB->query($query_exp);
$vol = $DB->affected_rows();
}
$task->setVolume($vol);
return ($vol > 0 ? 1 : 0);
}
/**
* Force sending all mails in queue for a specific item
*
* @param $itemtype item type
* @param $items_id id of the item
**/
static function forceSendFor($itemtype, $items_id) {
global $DB;
if (!empty($itemtype)
&& !empty($items_id)) {
// Send mail at least 1 minute after adding in queue to be sure that process on it is finished
$query = "SELECT `glpi_queuedmails`.*
FROM `glpi_queuedmails`
WHERE NOT `glpi_queuedmails`.`is_deleted`
AND `glpi_queuedmails`.`itemtype` = '$itemtype'
AND `glpi_queuedmails`.`items_id` = '$items_id'
AND `glpi_queuedmails`.`send_time` <= NOW()
ORDER BY `glpi_queuedmails`.`send_time` ASC";
$mail = new self();
foreach ($DB->request($query) as $data) {
$mail->sendMailById($data['id']);
}
}
}
/**
* Print the queued mail form
*
* @param $ID integer ID of the item
* @param $options array
*
* @return true if displayed false if item not found or not right to display
**/
function showForm($ID, $options=array()) {
global $CFG_GLPI;
if (!Session::haveRight("queuedmail", READ)) {
return false;
}
$this->check($ID, READ);
$options['canedit'] = false;
$this->showFormHeader($options);
echo "";
echo "| ".__('Type')." | ";
echo "";
if (!($item = getItemForItemtype($this->fields['itemtype']))) {
echo NOT_AVAILABLE;
echo " | ";
echo ""._n('Item', 'Items', 1)." | ";
echo "";
echo NOT_AVAILABLE;
} else {
echo $item->getType();
$item->getFromDB($this->fields['items_id']);
echo " | ";
echo ""._n('Item', 'Items', 1)." | ";
echo "";
echo $item->getLink();
}
echo " |
";
echo "";
echo "| "._n('Notification template', 'Notification templates', 1)." | ";
echo "";
echo Dropdown::getDropdownName('glpi_notificationtemplates',
$this->fields['notificationtemplates_id']);
echo " | ";
echo " | ";
echo " | ";
echo "
";
echo "";
echo "| ".__('Creation date')." | ";
echo "";
echo Html::convDateTime($this->fields['create_time']);
echo " | ".__('Expected send date')." | ";
echo "".Html::convDateTime($this->fields['send_time'])." | ";
echo "
";
echo "";
echo "| ".__('Send date')." | ";
echo "".Html::convDateTime($this->fields['sent_time'])." | ";
echo "".__('Number of tries of sent')." | ";
echo "".$this->fields['sent_try']." | ";
echo "
";
echo "| ".__('Email')." |
";
echo "";
echo "| ".__('Sender email')." | ";
echo "".$this->fields['sender']." | ";
echo "".__('Sender name')." | ";
echo "".$this->fields['sendername']." | ";
echo "
";
echo "";
echo "| ".__('Recipient email')." | ";
echo "".$this->fields['recipient']." | ";
echo "".__('Recipient name')." | ";
echo "".$this->fields['recipientname']." | ";
echo "
";
echo "";
echo "| ".__('Reply-to email')." | ";
echo "".$this->fields['replyto']." | ";
echo "".__('Reply-to name')." | ";
echo "".$this->fields['replytoname']." | ";
echo "
";
echo "";
echo "| ".__('Message ID')." | ";
echo "".$this->fields['messageid']." | ";
echo "".__('Additional headers')." | ";
echo "".self::getSpecificValueToDisplay('headers', $this->fields)." | ";
echo "
";
echo "";
echo "| ".__('Subject')." | ";
echo "".$this->fields['name']." | ";
echo "
";
echo "| ".__('Email HTML body')." | ";
echo "".__('Email text body')." | ";
echo "
";
echo "";
echo "| ".self::cleanHtml($this->fields['body_html'])." | ";
echo "".nl2br($this->fields['body_text'], false)." | ";
echo "
";
$this->showFormButtons($options);
return true;
}
/**
* @since version 0.85
*
* @param $string
**/
static function cleanHtml($string) {
$begin_strip = -1;
$end_strip = -1;
$begin_match = "//";
$end_match = "/<\/body>/";
$content = explode("\n", $string);
$newstring = '';
foreach ($content as $ID => $val) {
// Get last tag for end
if ($begin_strip >= 0) {
if (preg_match($end_match,$val)) {
$end_strip = $ID;
continue;
}
}
if (($begin_strip >= 0) && ($end_strip < 0)) {
$newstring .= $val;
}
// Get first tag for begin
if ($begin_strip < 0) {
if (preg_match($begin_match,$val)) {
$begin_strip = $ID;
}
}
}
return nl2br($newstring,false);
return preg_replace($patterns, $replacements, $string);
}
}
?>