1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Send the files attached to a mail.
6 *
7 * PHP versions 4 and 5
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
22 *
23 * @category   File Formats
24 * @package    File_Archive
25 * @author     Vincent Lascaux <vincentlascaux@php.net>
26 * @copyright  1997-2005 The PHP Group
27 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL
28 * @version    CVS: $Id: Mail.php,v 1.11 2005/06/02 12:24:43 vincentlascaux Exp $
29 * @link       http://pear.php.net/package/File_Archive
30 */
31
32require_once "File/Archive/Writer.php";
33require_once "Mail.php";
34require_once "Mail/mime.php";
35
36/**
37 * Send the files attached to a mail.
38 */
39class File_Archive_Writer_Mail extends File_Archive_Writer
40{
41    /**
42     * @var Mail_mime object
43     * @access private
44     */
45    var $mime;
46
47    /**
48     * @var Mail object used to send email (built thanks to the factory)
49     * @access private
50     */
51    var $mail;
52
53    /**
54     * @var Array or String An array or a string with comma separated recipients
55     * @access private
56     */
57    var $to;
58
59    /**
60     * @var Array The headers that will be passed to the Mail_mime object
61     * @access private
62     */
63    var $headers;
64
65    /**
66     * @var String Data read from the current file so far
67     * @access private
68     */
69    var $currentData = null;
70
71    /**
72     * @var String Name of the file being attached
73     * @access private
74     */
75    var $currentFilename = null;
76
77    /**
78     * @var String MIME of the file being attached
79     * @access private
80     */
81    var $currentMime = null;
82
83    /**
84     * @param Mail $mail Object used to send mail (see Mail::factory)
85     * @param array or string $to An array or a string with comma separated
86     *        recipients
87     * @param array $headers The headers that will be passed to the Mail_mime
88     *        object
89     * @param string $message Text body of the mail
90     */
91    function File_Archive_Writer_Mail($to, $headers, $message, &$mail)
92    {
93        $this->mime = new Mail_mime();
94        $this->mime->setTXTBody($message);
95        if (!empty($htmlMessage)) {
96            $this->mime->setHTMLBody($htmlMessage);
97        }
98
99        if ($mail === null)
100            $this->mail = Mail::factory("mail");
101        else
102            $this->mail =& $mail;
103
104        $this->to = $to;
105        $this->headers = $headers;
106    }
107
108    /**
109     * @see Mail_Mime::setHTMLBody()
110     */
111    function setHTMLBody($data, $isfile = false)
112    {
113        return $this->mime->setHTMLBody($data, $isfile);
114    }
115    /**
116     * @see Mail_Mime::addHTMLImage()
117     */
118    function addHTMLImage($file, $c_type = 'application/octet-stream',
119                          $name = '', $isfile = true)
120    {
121        return $this->mime->addHTMLImage($file, $c_type, $name, $isfile);
122    }
123
124    /**
125     * @see File_Archive_Writer::writeData()
126     *
127     * This function just put the data in $currentData until the end of file
128     * At that time, addCurrentData is called to attach $currentData to the mail
129     * and to clear $currentData for a new file
130     */
131    function writeData($data)
132    {
133        $this->currentData .= $data;
134    }
135    /**
136     * Called when a file is finished and must be added as attachment to the mail
137     */
138    function addCurrentData()
139    {
140        if ($this->currentFilename === null) {
141            return;
142        }
143
144        $error = $this->mime->addAttachment(
145                        $this->currentData,
146                        $this->currentMime,
147                        $this->currentFilename,
148                        false);
149        $this->currentData = "";
150        return $error;
151    }
152    /**
153     * @see File_Archive_Writer::newFile()
154     */
155    function newFile($filename, $stat, $mime = "application/octet-stream")
156    {
157        $error = $this->addCurrentData();
158        if (PEAR::isError($error)) {
159            return $error;
160        }
161
162        $this->currentFilename = $filename;
163        $this->currentMime = $mime;
164    }
165    /**
166     * @see File_Archive_Writer::newFileNeedsMIME()
167     */
168    function newFileNeedsMIME()
169    {
170        return true;
171    }
172
173    /**
174     * @see File_Archive_Writer::close()
175     */
176    function close()
177    {
178        $error = parent::close();
179        if (PEAR::isError($error)) {
180            return $error;
181        }
182        $error = $this->addCurrentData();
183        if (PEAR::isError($error)) {
184            return $error;
185        }
186
187        $body = $this->mime->get();
188        $headers = $this->mime->headers($this->headers);
189
190        if (!$this->mail->send(
191                $this->to,
192                $headers,
193                $body)
194          ) {
195            return PEAR::raiseError("Error sending mail");
196        }
197    }
198}
199
200?>