Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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.12 2005/08/29 15:14:09 vincentlascaux Exp $
29
 * @link       http://pear.php.net/package/File_Archive
30
 */
31
 
32
require_once "File/Archive/Writer.php";
33
require_once "Mail.php";
34
require_once "Mail/mime.php";
35
 
36
/**
37
 * Send the files attached to a mail.
38
 */
39
class 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
        if (substr($filename, -1) == '/') {
163
            $this->currentFilename = null;
164
        } else {
165
            $this->currentFilename = $filename;
166
            $this->currentMime = $mime;
167
        }
168
    }
169
    /**
170
     * @see File_Archive_Writer::newFileNeedsMIME()
171
     */
172
    function newFileNeedsMIME()
173
    {
174
        return true;
175
    }
176
 
177
    /**
178
     * @see File_Archive_Writer::close()
179
     */
180
    function close()
181
    {
182
        $error = parent::close();
183
        if (PEAR::isError($error)) {
184
            return $error;
185
        }
186
        $error = $this->addCurrentData();
187
        if (PEAR::isError($error)) {
188
            return $error;
189
        }
190
 
191
        $body = $this->mime->get();
192
        $headers = $this->mime->headers($this->headers);
193
 
194
        if (!$this->mail->send(
195
                $this->to,
196
                $headers,
197
                $body)
198
          ) {
199
            return PEAR::raiseError("Error sending mail");
200
        }
201
    }
202
}
203
 
204
?>