function applyFilters($q, $au, $roo)
{
- $c = DB_DataObject::factory($this->tableName());
- $c->get(5);
-
- $c->send(array());
-
- exit;
-
$tn = $this->tableName();
if(!empty($q['search']['nameortitle'])){
$i->update($ii);
}
-// $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
- $this->cachedMailWithOutImages(true, false);
+ $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
+// $this->cachedMailWithOutImages(true, false);
}
function onUpdate($old, $q,$roo)
{
- $this->cachedMailWithOutImages(true, false);
+ $this->cachedMailWithOutImages(true, (get_class($this) == 'Pman_Core_DataObjects_Core_email') ? false : true);
}
function processRelacements($replace_links = true)
{
+ $cfg = isset(HTML_FlexyFramework::get()->Pman_Crm) ? HTML_FlexyFramework::get()->Pman_Crm : false;
+
libxml_use_internal_errors (true);
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML('<?xml encoding="UTF-8">'.$this->bodytext);
}
}
+ $unsubscribe = false;
+
+ if(!empty($cfg)){
+ $unsubscribe = $cfg ['server_baseurl'] . '/Crm/Unsubscribe/' . $this->id . '/{person.id}';
+ }
+
+ foreach ($xpath->query('//a[@href]') as $a) {
+
+ $href = $a->getAttribute('href');
+
+ if(preg_match('/#unsubscribe/', $href) && !empty($unsubscribe)){
+ $a->setAttribute('href', $unsubscribe);
+ continue;
+ }
+
+ if(!preg_match('/^http(.*)/', $href)){
+ continue;
+ }
+ if (!$replace_links) {
+ continue;
+ }
+ $link = DB_DataObject::factory('crm_mailing_list_link');
+ $link->setFrom(array(
+ 'url' => $href
+ ));
+
+ if(!$link->find(true)){
+ $link->insert();
+ }
+
+ if(!$link->id){
+ continue;
+ }
+
+ $l = $cfg ['server_baseurl'] . '/Crm/Link/' .$this->id . '/' . $link->id . '/{person.id}.html';
+
+ $a->setAttribute('href', $l);
+
+ }
+
+ if(!empty($unsubscribe)){
+ $element = $doc->createElement('img');
+
+ $element->setAttribute('src', $cfg ['server_baseurl'] . '/Crm/Open/' . $this->id . '/{person.id}.html');
+ $element->setAttribute('width', '1');
+ $element->setAttribute('height', '1');
+
+ $html = $doc->getElementsByTagName('html');
+ $html->item(0)->appendChild($element);
+
+ $this->plaintext = str_replace("{unsubscribe_link}", $unsubscribe, $this->plaintext);
+ }
+
+
$this->bodytext = $doc->saveHTML();
libxml_use_internal_errors (false);
$this->bodytext = str_replace('%7B', '{', $this->bodytext ); // kludge as template is not interpretated as html.
$this->bodytext = str_replace('%7D', '}', $this->bodytext ); // kludge as template is not interpretated as html.
-
+
return;
}
- function send($obj)
- {
-
+ function send($obj, $force = true, $send = true)
+ {
$contents = (array)$obj;
- $this->cachedMailWithOutImages(true, false);
- exit;
- $contents['subject'] = $this->subject;
+ if(empty($this->id)){
+ $this->get('name', $contents['template']);
+ }
- require_once 'Pman/Core/Mailer.php';
+ if(empty($this->id)){
+ return PEAR::raiseError("template [{$contents['template']}]has not been set");
+ }
+
+ $contents['subject'] = $this->subject;
$ui = posix_getpwuid(posix_geteuid());
+ $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
+
+ if(!$this->isGenerated($cachePath)){
+ $this->cachedMailWithOutImages($force, false);
+ }
+
+ require_once 'Pman/Core/Mailer.php';
+
$templateDir = session_save_path() . '/email-cache-' . $ui['name'] ;
+
$r = new Pman_Core_Mailer(array(
'template'=> $this->tableName() . '-' . $this->id,
'templateDir' => $templateDir,
'contents' => $contents
));
- $ret = $r->toData();
+ $imageCache = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt';
- $ui = posix_getpwuid(posix_geteuid());
+ if(file_exists($imageCache) && filesize($imageCache)){
+ $images = json_decode(file_get_contents($imageCache), true);
+ $r->images = $images;
+ }
- $images = file_get_contents(session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '-images.txt');
+ $ret = $r->toData();
- $ret['body'] = str_replace('%Images%', $images, $ret['body']);
+ if(!$send){
+ return $ret;
+ }
return $r->send($ret);
}
function cachedMailWithOutImages($force = false, $replace_links = true)
{
- $random_hash = md5(date('r', time()));
- $this->cachedImages($random_hash);
$ui = posix_getpwuid(posix_geteuid());
$cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.txt';
- print_r($cachePath);exit;
+
if (!$force && $this->isGenerated($cachePath)) {
return;
}
mkdir(dirname($cachePath), 0700, true);
}
- $this->processRelacements($replace_links);
+ $random_hash = md5(date('r', time()));
+
+ $this->cachedImages();
$fh = fopen($cachePath, 'w');
Content-Transfer-Encoding: 7bit
{$this->plaintext}
-
---alt-{$random_hash}
-Content-Type: multipart/related; boundary=rel-{$random_hash}
-
---rel-{$random_hash}
-Content-Type: text/html; charset=utf-8
-Content-Transfer-Encoding: 7bit
-
-{$this->bodytext}
-
-");
- fwrite($fh,"%Images%
---rel-{$random_hash}--
-
---alt-{$random_hash}--
");
fclose($fh);
+ // cache body
+
+ $this->processRelacements($replace_links);
+
+ $cachePath = session_save_path() . '/email-cache-' . $ui['name'] . '/mail/' . $this->tableName() . '-' . $this->id . '.body.html';
+
+ if (!file_exists(dirname($cachePath))) {
+ mkdir(dirname($cachePath), 0700, true);
+ }
+
+ file_put_contents($cachePath, $this->bodytext);
+
}
- function cachedImages($random_hash)
+ function cachedImages()
{
$ui = posix_getpwuid(posix_geteuid());
$i->ontable = $this->tableName();
$i->whereAddIn('id', $ids, 'int');
$i->find();
+
+ $images = array();
+
+ require_once 'File/MimeType.php';
+ $y = new File_MimeType();
+
while ($i->fetch()){
if (!file_exists($i->getStoreName()) || !filesize($i->getStoreName())) {
continue;
}
- $out = chunk_split(base64_encode(file_get_contents($i->getStoreName())));
- if (empty($out)) {
- continue;
- }
- $imgfn = urlencode(preg_replace('/#.*$/i', '' , $i->filename));
- fwrite($fh, "--rel-{$random_hash}
-Content-Type: {$i->mimetype}; name={$imgfn}
-Content-Transfer-Encoding: base64
-Content-ID: <attachment-{$i->id}>
-Content-Disposition: inline; filename={$imgfn}
-
-" . $out . "");
-
- }
+
+ $images["attachment-$i->id"] = array(
+ 'file' => $i->getStoreName(),
+ 'mimetype' => $i->mimetype,
+ 'ext' => $y->toExt($i->mimetype),
+ 'contentid' => "attachment-$i->id"
+ );
+ }
+
+ file_put_contents($imageCache, json_encode($images));
}