Mailer.php
[Pman.Core] / Mailer.php
1 <?php
2
3 /**
4  *
5  *  code that used to be in Pman (sendTemplate / emailTemplate)
6  *
7  *  usage:
8  *
9  *
10  *  $x= new Pman_Core_Mailer($opts)
11  *
12  *  $opts[
13        page => 
14        contents
15        template
16        replaceImages => true|false
17     ]
18  *
19  *  $x->asData(); // returns data needed for notify?? - notify should really
20  *                  // just use this to pass around later..
21  *
22  *  $x->send();
23  *
24  */
25
26 class Pman_Core_Mailer {
27     
28     var $page           = false;
29     var $contents       = false;
30     var $template       = false;
31     var $replaceImages  = false;
32     
33     function Pman_Core_Mailer($args) {
34         foreach($args as $k=>$v) {
35             // a bit trusting..
36             $this->$k =  $v;
37         }
38     }
39      
40     /**
41      * ---------------- Global Tools ---------------   
42      */
43     
44     function toData()
45     {
46     
47         $templateFile = $this->template;
48         $args = $this->contents;
49         
50         $content  = clone($page);
51         
52         foreach((array)$args as $k=>$v) {
53             $content->$k = $v;
54         }
55         
56         $content->msgid = empty($content->msgid ) ? md5(time() . rand()) : $content->msgid ;
57         
58         $ff = HTML_FlexyFramework::get();
59         $http_host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : 'pman.HTTP_HOST.not.set';
60         if (isset($ff->Pman['HTTP_HOST'])) {
61             $http_host  = $ff->Pman['HTTP_HOST'];
62         }
63         
64         
65         $content->HTTP_HOST = $http_host;
66         
67         
68         
69         // this should be done by having multiple template sources...!!!
70         
71         require_once 'HTML/Template/Flexy.php';
72         
73         $htmlbody = false;
74         
75         if (is_string($template->resolvePath('mail/'.$template.'.body.html')) ) {
76             // then we have a multi-part email...
77             
78             
79             $htmltemplate = new HTML_Template_Flexy(  );
80             $htmltemplate->compile('mail/'. $templateFile.'.body.html');
81             $htmlbody =  $template->bufferedOutputObject($content);
82             
83             // for the html body, we may want to convert the attachments to images.
84             
85             
86              
87         } 
88         $template = new HTML_Template_Flexy( array(
89                 'nonHTML' => true,
90         ));
91         
92         $template->compile('mail/'. $templateFile.'.txt');
93         
94         /* use variables from this object to ouput data. */
95         $mailtext = $template->bufferedOutputObject($content);
96         
97         
98         
99         
100         
101         
102         
103         
104         
105         
106         //echo "<PRE>";print_R($mailtext);
107         
108         /* With the output try and send an email, using a few tricks in Mail_MimeDecode. */
109         require_once 'Mail/mimeDecode.php';
110         require_once 'Mail.php';
111         
112         $decoder = new Mail_mimeDecode($mailtext);
113         $parts = $decoder->getSendArray();
114         if (PEAR::isError($parts)) {
115             return $parts;
116             //echo "PROBLEM: {$parts->message}";
117             //exit;
118         } 
119         
120         
121         
122         
123         $parts[1]['Message-Id'] = '<' .   $content->msgid   .
124                                      '@' . $content->HTTP_HOST .'>';
125         
126         
127        // list($recipents,$headers,$body) = $parts;
128         return array(
129             'recipents' => $parts[0],
130             'headers' => $parts[1],
131             'body' => $parts[2]
132         );
133     }
134     function send()
135     {
136         
137         $email = $this->toData();
138         if (is_a($email, 'PEAR_Error')) {
139             return $email;
140         }
141         ///$recipents = array($this->email);
142         $mailOptions = PEAR::getStaticProperty('Mail','options');
143         $mail = Mail::factory("SMTP",$mailOptions);
144         $headers['Date'] = date('r');
145         if (PEAR::isError($mail)) {
146             return $mail;
147         } 
148         $oe = error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT);
149         $ret = $mail->send($email['recipents'],$email['headers'],$email['body']);
150         error_reporting($oe);
151        
152         return $ret;
153     
154 }