* Merging all changes from TYPO3_4-0 branch back into HEAD
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_formmail.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@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 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
32 *
33 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 69: class t3lib_formmail extends t3lib_htmlmail
41 * 95: function start($V,$base64=false)
42 * 172: function addAttachment($file, $filename)
43 *
44 * TOTAL FUNCTIONS: 2
45 * (This index is automatically created/updated by the extension "extdeveval")
46 *
47 */
48
49
50
51
52
53
54
55
56
57
58
59
60
61 /**
62 * Formmail class, used by the TYPO3 "cms" extension (default frontend) to send email forms.
63 *
64 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
65 * @package TYPO3
66 * @subpackage t3lib
67 * @see tslib_fe::sendFormmail(), t3lib/formmail.php
68 */
69 class t3lib_formmail extends t3lib_htmlmail {
70 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';
71
72
73 /**
74 * Start function
75 * This class is able to generate a mail in formmail-style from the data in $V
76 * Fields:
77 *
78 * [recipient]: email-adress of the one to receive the mail. If array, then all values are expected to be recipients
79 * [attachment]: ....
80 *
81 * [subject]: The subject of the mail
82 * [from_email]: Sender email. If not set, [email] is used
83 * [from_name]: Sender name. If not set, [name] is used
84 * [replyto_email]: Reply-to email. If not set [from_email] is used
85 * [replyto_name]: Reply-to name. If not set [from_name] is used
86 * [organisation]: Organisation (header)
87 * [priority]: Priority, 1-5, default 3
88 * [html_enabled]: If mail is sent as html
89 * [use_base64]: If set, base64 encoding will be used instead of quoted-printable
90 *
91 * @param array Contains values for the field names listed above (with slashes removed if from POST input)
92 * @param boolean Whether to base64 encode the mail content
93 * @return void
94 */
95 function start($V,$base64=false) {
96 parent::start();
97
98 if ($base64 || $V['use_base64']) { $this->useBase64(); }
99
100 if ($GLOBALS['TSFE']->config['config']['formMailCharset']) { // Respect formMailCharset if it was set
101 $this->charset = trim($GLOBALS['TSFE']->config['config']['formMailCharset']);
102 }
103
104 if (isset($V['recipient'])) {
105 // Sets the message id
106 $this->messageid = md5(microtime()).'@domain.tld';
107
108 $this->subject = ($V['subject']) ? $V['subject'] : 'Formmail on '.t3lib_div::getIndpEnv('HTTP_HOST');
109 $this->from_email = ($V['from_email']) ? $V['from_email'] : (($V['email'])?$V['email']:'');
110 $this->from_name = ($V['from_name']) ? $V['from_name'] : (($V['name'])?$V['name']:'');
111 $this->replyto_email = ($V['replyto_email']) ? $V['replyto_email'] : $this->from_email;
112 $this->replyto_name = ($V['replyto_name']) ? $V['replyto_name'] : $this->from_name;
113 $this->organisation = ($V['organisation']) ? $V['organisation'] : '';
114 $this->priority = ($V['priority']) ? t3lib_div::intInRange($V['priority'],1,5) : 3;
115
116 // Auto responder.
117 $this->auto_respond_msg = (trim($V['auto_respond_msg']) && $this->from_email) ? trim($V['auto_respond_msg']) : '';
118
119 $Plain_content = '';
120 $HTML_content = '<table border="0" cellpadding="2" cellspacing="2">';
121
122 // Runs through $V and generates the mail
123 if (is_array($V)) {
124 reset($V);
125 while (list($key,$val)=each($V)) {
126 if (!t3lib_div::inList($this->reserved_names,$key)) {
127 $space = (strlen($val)>60)?chr(10):'';
128 $val = (is_array($val) ? implode($val,chr(10)) : $val);
129 $Plain_content.= strtoupper($key).': '.$space.$val."\n".$space;
130 $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>';
131 }
132 }
133 }
134 $HTML_content.= '</table>';
135
136
137 if ($V['html_enabled']) {
138 $this->setHTML($this->encodeMsg($HTML_content));
139 }
140 $this->addPlain($Plain_content);
141
142 for ($a=0;$a<10;$a++) {
143 $varname = 'attachment'.(($a)?$a:'');
144 $theFile = t3lib_div::upload_to_tempfile($_FILES[$varname]['tmp_name']);
145 $theName = $_FILES[$varname]['name'];
146
147 if ($theFile && @file_exists($theFile)) {
148 if (filesize($theFile) < 250000) {
149 $this->addAttachment($theFile, $theName);
150 }
151 }
152 t3lib_div::unlink_tempfile($theFile);
153 }
154
155 $this->setHeaders();
156 $this->setContent();
157 $this->setRecipient($V['recipient']);
158 if ($V['recipient_copy']) {
159 $this->recipient_copy = trim($V['recipient_copy']);
160 }
161 }
162 }
163
164 /**
165 * Adds an attachment to the mail
166 *
167 * @param string The absolute path to the file to add as attachment
168 * @param string The files original filename (not necessarily the same as the current since this could be uploaded files...)
169 * @return boolean True if the file existed and was added.
170 * @access private
171 */
172 function addAttachment($file, $filename) {
173 $content = $this->getURL($file); // We fetch the content and the mime-type
174 $fileInfo = $this->split_fileref($filename);
175 if ($fileInfo['fileext'] == 'gif') {$content_type = 'image/gif';}
176 if ($fileInfo['fileext'] == 'bmp') {$content_type = 'image/bmp';}
177 if ($fileInfo['fileext'] == 'jpg' || $fileInfo['fileext'] == 'jpeg') {$content_type = 'image/jpeg';}
178 if ($fileInfo['fileext'] == 'html' || $fileInfo['fileext'] == 'htm') {$content_type = 'text/html';}
179 if (!$content_type) {$content_type = 'application/octet-stream';}
180
181 if ($content) {
182 $theArr['content_type']= $content_type;
183 $theArr['content']= $content;
184 $theArr['filename']= $filename;
185 $this->theParts['attach'][]=$theArr;
186 return true;
187 } else { return false;}
188 }
189 }
190
191
192 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php']) {
193 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php']);
194 }
195 ?>