MessagePreview.php
[Pman.Core] / MessagePreview.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Core_MessagePreview extends Pman
6 {
7     var $masterTemplate = 'mail/MessagePreview.html';
8     
9     function getAuth()
10     {
11         if (HTML_FlexyFramework::get()->cli) {
12             return true;
13         }
14         $this->authUser = $this->getAuthUser();
15         if (!$this->authUser) {
16             return false;
17         }
18         return true;
19     }
20     
21     function get()
22     {
23         if(empty($_REQUEST['_id']) || empty($_REQUEST['_module'])){
24             $this->jerr('missing options!');
25         }
26         
27         
28         $mlq = DB_DataObject::factory('crm_mailing_list_message');
29         
30         $mlq->get($_REQUEST['_id']);
31         
32         $this->msg = $mlq;
33
34         $this->showHtml = isset($_REQUEST['_as_html']) ? true : false;
35         
36     }
37     
38     function post()
39     {
40         if(empty($_REQUEST['_id'])){
41             $this->jerr('id got error');
42         }
43         if(empty($_REQUEST['_action'])){
44             $this->jerr('preview type not set');
45         }
46         $mid = $_REQUEST['_id'];
47         
48         $cfg = HTML_FlexyFramework::get()->Pman_Crm;
49         
50        
51        
52         $q = DB_DataObject::factory('crm_mailing_list_queue');
53         $q->id = 'test-message-'. $mid;
54         $q->message_id = $mid;
55         $q->message_id_subject = $q->message()->subject;
56         $q->message_id_from_email = $cfg['from_email'];
57         $q->message_id_from_name = "Email Test"; 
58         
59         $q->cachedMailWithOutImages(true);
60         $r = $q->getMailerObject($this->authUser, false, false, true);
61         //print_r($r->toData());
62         $ret = $r->toData();
63         $images = file_get_contents(session_save_path() . '/email-cache-' . getenv('APACHE_RUN_USER') . '/mail/' . $q->id . '-images.txt');
64        // var_dump($images);exit;
65         
66         $ret['body'] = str_replace('%Images%', $images, $ret['body']);
67         
68         $sent = $r->send($ret);
69         if(!is_object($sent)){
70             $this->jok('SUCCESS');
71         }
72         $this->jerr('error!!:' . $sent->toString());
73         /*
74         $ret = $r->toData();
75         
76         //print_r($ret);exit;
77         $images = file_get_contents(session_save_path() . '/email-cache-' . get_current_user() . '/mail/' . $this->id . '-images.txt');
78        // var_dump($images);exit;
79         
80         $ret['body'] = str_replace('%Images%', $images, $ret['body']);
81         return $ret;
82         
83        
84        
85        
86        
87         $message = DB_DataObject::factory('crm_mailing_list_message');
88         if(!$message->get($mid)){
89             $this->jerr("Error occour on getting message!");
90         }
91         $mm = clone($message);
92         $message->processRelacements($this->authUser);
93
94         $ids = $mm->attachmentIds();
95         //$this->jerr(print_r($ids,true));
96         
97         
98         
99         
100         
101         
102         
103         
104         
105         
106          require_once 'Pman/Core/Mailer.php';
107         $i = DB_DataObject::factory('Images');
108         $i->onid = $mid;
109         $i->ontable = 'crm_mailing_list_message';
110         $i->whereAddIn('id', $ids, 'int');
111         $i->find();
112         $attachment = array();
113         while ($i->fetch()){
114             $ii = clone($i);
115             require_once 'File/MimeType.php';
116             $y = new File_MimeType();
117             $ii->ext = $y->toExt(trim((string) $ii->mimetype ));
118             $ii->file = chunk_split(base64_encode(file_get_contents($ii->getStoreName())));
119             $attachment[] = $ii;
120         }
121         
122         $cfg = HTML_FlexyFramework::get()->Pman_Crm;
123         
124         
125         $template = ($_REQUEST['_action'] == 'html') ? 'mixedMail' : 'plainMail';
126         
127         $random_hash = md5(date('r', time()));
128         $r = new Pman_Core_Mailer(array(
129             'template'=> $template,
130             'page' => $this,
131             'contents' => array(
132                 'random_hash' => $random_hash,
133                 'person' => $this->authUser,
134                 'data' => $message,
135                 'attach' => $attachment,
136                 'from' => '<'.$cfg['from_email'].'>'
137             )
138         ));
139         //print_R($r->toData());
140         $sent = $r->send();
141         if(!is_object($sent)){
142             $this->jok('SUCCESS');
143         }
144         $this->jerr('error!!:' . $sent->toString());
145         */
146         
147     }
148     
149 }