/** * Attaches files to mail * * @param CMIMEMail $mailer * @param string $body * @param string|array $types * @return string */ function nc_mail_attachment_attach(CMIMEMail $mailer, $body, $types) { $nc_core = nc_Core::get_object(); $db = $nc_core->db; $types_escaped = array(); if (!is_array($types)) { $types = array($types); } foreach ($types as $type) { $types_escaped[] = '\'' . $db->escape($type) . '\''; } $sql = "SELECT `Filename`, `Path`, `Content_Type`, `Extension` FROM `Mail_Attachment` WHERE `Type` IN (" . implode(',', $types_escaped) . ")"; $attachments = (array) $db->get_results($sql, ARRAY_A); while (preg_match('/\\%FILE_([-_a-z0-9]+)/i', $body, $match)) { $filename = $match[1]; $file = false; foreach ($attachments as $index => $attachment) { if (strtolower($attachment['Filename']) == strtolower($filename)) { $file = $attachment; unset($attachments[$index]); break; } } $replace = ''; if ($file) { $absolute_path = $nc_core->DOCUMENT_ROOT . $file['Path']; $replace = 'cid:' . $mailer->attachFileEmbed($absolute_path, $filename . '.' . $file['Extension'], $file['Content_Type']); } $body = preg_replace('/\\%FILE_' . preg_quote($filename) . '/', $replace, $body); } foreach ($attachments as $attachment) { $absolute_path = $nc_core->DOCUMENT_ROOT . $attachment['Path']; $mailer->attachFileEmbed($absolute_path, $attachment['Filename'] . '.' . $attachment['Extension'], $attachment['Content_Type']); } return $body; }