Import/Core_email.php
[Pman.Core] / Import / Core_email.php
1 <?php
2
3
4
5 require_once 'Pman.php';
6
7 class Pman_Core_Import_Core_email extends Pman 
8 {
9     static $cli_desc = "Import an email into core_email template"; 
10     
11     static $cli_opts = array(
12         'file' => array(
13             'desc' => 'File to import',
14             'short' => 'f',
15             'min' => 1,
16             'max' => 1,
17             
18         ),
19         'master' => array(
20             'desc' => 'Master template (wrapper to body)',
21             'short' => 'm',
22             'default' => '',
23             'min' => 1,
24             'max' => 1,
25             
26         ),
27         'master' => array(
28             'desc' => 'Update template (deletes old version?)',
29             'short' => 'u',
30             'default' => '',
31             'min' => 1,
32             'max' => 1,  
33         ),
34     );
35     
36     function getAuth()
37     {
38         $ff = HTML_FlexyFramework::get();
39         
40         if (!$ff->cli) {
41             die("cli only");
42         }
43         
44     }
45
46     function get($part='', $opts){
47         
48        // DB_DataObject::debugLevel(1);
49         
50         $template_name = preg_replace('/\.[a-z]+$/i', '', basename($opts['file']));
51         
52         if (!file_exists($opts['file'])) {
53             $this->jerr("file does not exist : " . $opts['file']);
54         }
55         
56         
57         if (!empty($opts['master']) && !file_exists($opts['master'])) {
58             $this->jerr("master file does not exist : " . $opts['master']);
59         }
60         
61         
62          
63         $c = DB_dataObject::factory('core_email');
64         $ret = $c->get('name',$template_name);
65         if($ret ) {
66             $this->jerr("we do not support updating the files ... - especially if the user has changed them!!");
67         }
68         
69         $mailtext = file_get_contents($opts['file']);
70
71         require_once 'Mail/mimeDecode.php';
72         require_once 'Mail/RFC822.php';
73         
74         $decoder = new Mail_mimeDecode($mailtext);
75         $parts = $decoder->getSendArray();
76         if (is_a($parts,'PEAR_Error')) {
77             echo $parts->toString() . "\n";
78             exit;
79         }
80         
81         $headers = $parts[1];
82         $from = new Mail_RFC822();
83         $from_str = $from->parseAddressList($headers['From']);
84         
85         $from_name  = trim($from_str[0]->personal, '"');
86         
87         $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host;
88         
89         
90         $c->setFrom(array(
91             'from_name'     => $from_name,
92             'from_email'    => $from_email,
93             'subject'       => $parts[1]['Subject'],
94             'name'          => $template_name,
95             'bodytext'      => $parts[2],
96             'updated_dt'     => date('Y-m-d H:i:s'),
97             'created_dt'     => date('Y-m-d H:i:s'),
98         ));
99         $c->insert();
100         
101         die("done\n");
102     }
103 }