5 require_once 'Pman.php';
7 class Pman_Core_Import_Core_email extends Pman
9 static $cli_desc = "Import an email into core_email template";
11 static $cli_opts = array(
13 'desc' => 'File to import',
20 'desc' => 'Master template (wrapper to body)',
28 'desc' => 'Update template (deletes old version?)',
35 'desc' => 'Force usage of file (so content is not editable in Management system)',
41 'raw_content' => array(
42 'desc' => 'Raw contents of email (used by API) - not by Command line',
53 $ff = HTML_FlexyFramework::get();
60 function get($part = '', $opts=array())
62 $this->updateOrCreateEmail($part, $opts, false);
65 function updateOrCreateEmail($part='', $opts= array(), $cm = false, $mapping = false){
67 // DB_DataObject::debugLevel(1);
70 if (empty($opts['raw_content'])) {
71 $template_name = preg_replace('/\.[a-z]+$/i', '', basename($opts['file']));
73 if (!file_exists($opts['file'])) {
74 $this->jerr("file does not exist : " . $opts['file']);
78 if (!empty($opts['master']) && !file_exists($opts['master'])) {
79 $this->jerr("master file does not exist : " . $opts['master']);
84 $cm = DB_dataObject::factory('core_email');
85 $ret = $cm->get('name',$template_name);
86 if($ret && empty($opts['update'])) {
87 $this->jerr("use --update to update the template..");
90 $mailtext = file_get_contents($opts['file']);
92 $template_name = $opts['name'];
93 $mailtext = $opts['raw_content'];
96 if (!empty($opts['master'])) {
98 $mailtext = file_get_contents($opts['master']);
99 $mailtext = str_replace('{outputBody():h}', $body, $mailtext);
103 foreach ($mapping as $k => $v) {
104 $mailtext = str_replace($k, $v, $mailtext);
108 require_once 'Mail/mimeDecode.php';
109 require_once 'Mail/RFC822.php';
111 $decoder = new Mail_mimeDecode($mailtext);
112 $parts = $decoder->getSendArray();
113 $structure = $decoder->decode(array(
114 'include_bodies' => true,
115 'decode_bodies' => true
117 if (is_a($parts,'PEAR_Error')) {
118 echo $parts->toString() . "\n";
122 $headers = $parts[1];
123 $from = new Mail_RFC822();
124 $from_str = $from->parseAddressList($headers['From']);
125 if (is_a($from_str,'PEAR_Error')) {
126 echo $from_str->toString() . "\n";
131 $from_name = trim($from_str[0]->personal, '"');
133 $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host;
138 if (empty($opts['use-file'])) {
140 switch($structure->ctype_primary .'/'. $structure->ctype_secondary ) {
141 case 'multipart/alternative':
142 foreach($structure->parts as $p) {
143 switch($p->ctype_primary .'/'. $p->ctype_secondary ) {
145 $bodytext = $p->body;
149 $bodyhtml = $p->body;
157 $bodytext = $parts[2];
162 $bodyhtml = $parts[2];
165 var_dump($structure->ctype_primary .'/'. $structure->ctype_secondary );
179 'bodytext' => $bodyhtml,
180 'plaintext' => $bodytext,
181 'updated_dt' => date('Y-m-d H:i:s'),
182 'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
189 'from_name' => $from_name,
190 'from_email' => $from_email,
191 'subject' => $headers['Subject'],
192 'name' => $template_name,
193 'bodytext' => $bodyhtml,
194 'plaintext' => $bodytext,
195 'updated_dt' => date('Y-m-d H:i:s'),
196 'created_dt' => date('Y-m-d H:i:s'),
197 'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',