Initial revision
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_formmail.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skårhøj (kasper@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains a class for formmail
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 */
32 /**
33 * [CLASS/FUNCTION INDEX of SCRIPT]
34 *
35 *
36 *
37 * 66: class t3lib_formmail extends t3lib_htmlmail
38 * 92: function start($V,$base64=1)
39 * 166: function addAttachment($file, $filename)
40 *
41 * TOTAL FUNCTIONS: 2
42 * (This index is automatically created/updated by the extension "extdeveval")
43 *
44 */
45
46
47
48
49
50
51
52
53
54
55
56
57
58 /**
59 * Formmail class
60 *
61 * @author Kasper Skårhøj <kasper@typo3.com>
62 * @package TYPO3
63 * @subpackage t3lib
64 * @see tslib_fe::sendFormmail(), t3lib/formmail.php
65 */
66 class t3lib_formmail extends t3lib_htmlmail {
67 var $reserved_names = 'recipient,recipient_copy,auto_respond_msg,redirect,subject,attachment,from_email,from_name,replyto_email,replyto_name,organisation,priority,html_enabled,quoted_printable,submit_x,submit_y';
68
69
70 /**
71 * Start function
72 * This class is able to generate a mail in formmail-style from the data in $V
73 * Fields:
74 *
75 * [recipient]: email-adress of the one to receive the mail. If array, then all values are expected to be recipients
76 * [attachment]: ....
77 *
78 * [subject]: The subject of the mail
79 * [from_email]: Sender email. If not set, [email] is used
80 * [from_name]: Sender name. If not set, [name] is used
81 * [replyto_email]: Reply-to email. If not set [from_email] is used
82 * [replyto_name]: Reply-to name. If not set [from_name] is used
83 * [organisation]: Organisation (header)
84 * [priority]: Priority, 1-5, default 3
85 * [html_enabled]: If mail is sent as html
86 * [quoted_printable]: if set, quoted-printable will be used instead of base 64
87 *
88 * @param array Contains values for the field names listed above
89 * @param boolean Whether to base64 encode the mail content
90 * @return void
91 */
92 function start($V,$base64=1) {
93 if ($base64 && !$V['quoted_printable']) {$this->useBase64();}
94 if (is_array($V)) {
95 t3lib_div::stripSlashesOnArray($V);
96 }
97
98 if (isset($V['recipient'])) {
99 // Sets the message id
100 $this->messageid = '<'.md5(microtime()).'@domain.tld>';
101
102 $this->subject = ($V['subject']) ? $V['subject'] : 'Formmail on '.t3lib_div::getIndpEnv('HTTP_HOST');
103 $this->from_email = ($V['from_email']) ? $V['from_email'] : (($V['email'])?$V['email']:'');
104 $this->from_name = ($V['from_name']) ? $V['from_name'] : (($V['name'])?$V['name']:'');
105 $this->replyto_email = ($V['replyto_email']) ? $V['replyto_email'] : $this->from_email;
106 $this->replyto_name = ($V['replyto_name']) ? $V['replyto_name'] : $this->from_name;
107 $this->organisation = ($V['organisation']) ? $V['organisation'] : '';
108 $this->priority = ($V['priority']) ? t3lib_div::intInRange($V['priority'],1,5) : 3;
109
110 // Auto responder.
111 $this->auto_respond_msg = (trim($V['auto_respond_msg']) && $this->from_email) ? trim($V['auto_respond_msg']) : '';
112
113 $Plain_content = '';
114 $HTML_content = '<table border="0" cellpadding="2" cellspacing="2">';
115
116 // Runs through $V and generates the mail
117 if (is_array($V)) {
118 reset($V);
119 while (list($key,$val)=each($V)) {
120 if (!t3lib_div::inList($this->reserved_names,$key)) {
121 $space = (strlen($val)>60)?chr(10):'';
122 $val = (is_array($val) ? implode($val,chr(10)) : $val);
123 $Plain_content.= strtoupper($key).': '.$space.$val."\n".$space;
124 $HTML_content.='<tr><td bgcolor="#eeeeee"><font face="Verdana" size="1"><b>'.strtoupper($key).'</b></font></td><td bgcolor="#eeeeee"><font face="Verdana" size="1">'.nl2br(HTMLSpecialChars($val)).'&nbsp</font></td></tr>';
125 }
126 }
127 }
128 $HTML_content.= '</table>';
129
130
131 if ($V['html_enabled']) {
132 $this->setHTML($this->encodeMsg($HTML_content));
133 }
134 $this->addPlain($Plain_content);
135
136 for ($a=0;$a<10;$a++) {
137 $varname = 'attachment'.(($a)?$a:'');
138 $theFile = $GLOBALS['HTTP_POST_FILES'][$varname]['tmp_name'];
139 $theName = $GLOBALS['HTTP_POST_FILES'][$varname]['name'];
140
141 if ($theFile && @file_exists($theFile)) {
142 if (filesize($theFile) < 250000) {
143 $this->addAttachment($theFile, $theName);
144 }
145 // unlink($theFile);
146 }
147 }
148
149 $this->setHeaders();
150 $this->setContent();
151 $this->setRecipient($V['recipient']);
152 if ($V['recipient_copy']) {
153 $this->recipient_copy = trim($V['recipient_copy']);
154 }
155 }
156 }
157
158 /**
159 * Adds an attachment to the mail
160 *
161 * @param string The absolute path to the file to add as attachment
162 * @param string The files original filename (not necessarily the same as the current since this could be uploaded files...)
163 * @return boolean True if the file existed and was added.
164 * @access private
165 */
166 function addAttachment($file, $filename) {
167 $content = $this->getURL($file); // We fetch the content and the mime-type
168 $fileInfo = $this->split_fileref($filename);
169 if ($fileInfo['fileext'] == 'gif') {$content_type = 'image/gif';}
170 if ($fileInfo['fileext'] == 'bmp') {$content_type = 'image/bmp';}
171 if ($fileInfo['fileext'] == 'jpg' || $fileInfo['fileext'] == 'jpeg') {$content_type = 'image/jpeg';}
172 if ($fileInfo['fileext'] == 'html' || $fileInfo['fileext'] == 'htm') {$content_type = 'text/html';}
173 if (!$content_type) {$content_type = 'application/octet-stream';}
174
175 if ($content) {
176 $theArr['content_type']= $content_type;
177 $theArr['content']= $content;
178 $theArr['filename']= $filename;
179 $this->theParts['attach'][]=$theArr;
180 return true;
181 } else { return false;}
182 }
183 }
184
185
186 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php']) {
187 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php']);
188 }
189 ?>